1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
package msgp
import (
"bytes"
"encoding/json"
"testing"
"time"
)
func TestUnmarshalJSON(t *testing.T) {
var buf bytes.Buffer
enc := NewWriter(&buf)
enc.WriteMapHeader(5)
enc.WriteString("thing_1")
enc.WriteString("a string object")
enc.WriteString("a_map")
enc.WriteMapHeader(2)
// INNER
enc.WriteString("cmplx")
enc.WriteComplex64(complex(1.0, 1.0))
enc.WriteString("int_b")
enc.WriteInt64(-100)
enc.WriteString("an extension")
enc.WriteExtension(&RawExtension{Type: 1, Data: []byte("blaaahhh")})
enc.WriteString("some bytes")
enc.WriteBytes([]byte("here are some bytes"))
enc.WriteString("now")
enc.WriteTime(time.Now())
enc.Flush()
var js bytes.Buffer
_, err := UnmarshalAsJSON(&js, buf.Bytes())
if err != nil {
t.Logf("%s", js.Bytes())
t.Fatal(err)
}
mp := make(map[string]interface{})
err = json.Unmarshal(js.Bytes(), &mp)
if err != nil {
t.Log(js.String())
t.Fatalf("Error unmarshaling: %s", err)
}
if len(mp) != 5 {
t.Errorf("map length should be %d, not %d", 5, len(mp))
}
so, ok := mp["thing_1"]
if !ok || so != "a string object" {
t.Errorf("expected %q; got %q", "a string object", so)
}
if _, ok := mp["now"]; !ok {
t.Error(`"now" field doesn't exist`)
}
c, ok := mp["a_map"]
if !ok {
t.Error(`"a_map" field doesn't exist`)
} else {
if m, ok := c.(map[string]interface{}); ok {
if _, ok := m["cmplx"]; !ok {
t.Error(`"a_map.cmplx" doesn't exist`)
}
} else {
t.Error(`can't type-assert "c" to map[string]interface{}`)
}
}
t.Logf("JSON: %s", js.Bytes())
}
func BenchmarkUnmarshalAsJSON(b *testing.B) {
var buf bytes.Buffer
enc := NewWriter(&buf)
enc.WriteMapHeader(4)
enc.WriteString("thing_1")
enc.WriteString("a string object")
enc.WriteString("a_first_map")
enc.WriteMapHeader(2)
enc.WriteString("float_a")
enc.WriteFloat32(1.0)
enc.WriteString("int_b")
enc.WriteInt64(-100)
enc.WriteString("an array")
enc.WriteArrayHeader(2)
enc.WriteBool(true)
enc.WriteUint(2089)
enc.WriteString("a_second_map")
enc.WriteMapStrStr(map[string]string{
"internal_one": "blah",
"internal_two": "blahhh...",
})
enc.Flush()
var js bytes.Buffer
bts := buf.Bytes()
_, err := UnmarshalAsJSON(&js, bts)
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(js.Bytes())))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
js.Reset()
UnmarshalAsJSON(&js, bts)
}
}
|