MCPcopy
hub / github.com/tinylib/msgp / TestUnmarshalJSON

Function TestUnmarshalJSON

msgp/json_bytes_test.go:10–78  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

8)
9
10func TestUnmarshalJSON(t *testing.T) {
11 var buf bytes.Buffer
12 enc := NewWriter(&buf)
13 enc.WriteMapHeader(5)
14
15 enc.WriteString("thing_1")
16 enc.WriteString("a string object")
17
18 enc.WriteString("a_map")
19 enc.WriteMapHeader(2)
20
21 // INNER
22 enc.WriteString("cmplx")
23 enc.WriteComplex64(complex(1.0, 1.0))
24 enc.WriteString("int_b")
25 enc.WriteInt64(-100)
26
27 enc.WriteString("an extension")
28 enc.WriteExtension(&RawExtension{Type: 1, Data: []byte("blaaahhh")})
29
30 enc.WriteString("some bytes")
31 enc.WriteBytes([]byte("here are some bytes"))
32
33 enc.WriteString("now")
34 enc.WriteTime(time.Now())
35
36 enc.Flush()
37
38 var js bytes.Buffer
39 _, err := UnmarshalAsJSON(&js, buf.Bytes())
40 if err != nil {
41 t.Logf("%s", js.Bytes())
42 t.Fatal(err)
43 }
44 mp := make(map[string]any)
45 err = json.Unmarshal(js.Bytes(), &mp)
46 if err != nil {
47 t.Log(js.String())
48 t.Fatalf("Error unmarshaling: %s", err)
49 }
50
51 if len(mp) != 5 {
52 t.Errorf("map length should be %d, not %d", 5, len(mp))
53 }
54
55 so, ok := mp["thing_1"]
56 if !ok || so != "a string object" {
57 t.Errorf("expected %q; got %q", "a string object", so)
58 }
59
60 if _, ok := mp["now"]; !ok {
61 t.Error(`"now" field doesn't exist`)
62 }
63
64 c, ok := mp["a_map"]
65 if !ok {
66 t.Error(`"a_map" field doesn't exist`)
67 } else {

Callers

nothing calls this directly

Calls 12

WriteMapHeaderMethod · 0.95
WriteStringMethod · 0.95
WriteComplex64Method · 0.95
WriteInt64Method · 0.95
WriteExtensionMethod · 0.95
WriteBytesMethod · 0.95
WriteTimeMethod · 0.95
FlushMethod · 0.95
NewWriterFunction · 0.85
UnmarshalAsJSONFunction · 0.85
StringMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…