github.com/chrislusf/greenpack@v3.7.1-0.20170911073826-ad5bd10b7c47+incompatible/msgp/json_bytes_test.go (about)

     1  package msgp
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func 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]interface{})
    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 {
    68  		if m, ok := c.(map[string]interface{}); ok {
    69  			if _, ok := m["cmplx"]; !ok {
    70  				t.Error(`"a_map.cmplx" doesn't exist`)
    71  			}
    72  		} else {
    73  			t.Error(`can't type-assert "c" to map[string]interface{}`)
    74  		}
    75  
    76  	}
    77  
    78  	t.Logf("JSON: %s", js.Bytes())
    79  }
    80  
    81  func BenchmarkUnmarshalAsJSON(b *testing.B) {
    82  	var buf bytes.Buffer
    83  	enc := NewWriter(&buf)
    84  	enc.WriteMapHeader(4)
    85  
    86  	enc.WriteString("thing_1")
    87  	enc.WriteString("a string object")
    88  
    89  	enc.WriteString("a_first_map")
    90  	enc.WriteMapHeader(2)
    91  	enc.WriteString("float_a")
    92  	enc.WriteFloat32(1.0)
    93  	enc.WriteString("int_b")
    94  	enc.WriteInt64(-100)
    95  
    96  	enc.WriteString("an array")
    97  	enc.WriteArrayHeader(2)
    98  	enc.WriteBool(true)
    99  	enc.WriteUint(2089)
   100  
   101  	enc.WriteString("a_second_map")
   102  	enc.WriteMapStrStr(map[string]string{
   103  		"internal_one": "blah",
   104  		"internal_two": "blahhh...",
   105  	})
   106  	enc.Flush()
   107  
   108  	var js bytes.Buffer
   109  	bts := buf.Bytes()
   110  	_, err := UnmarshalAsJSON(&js, bts)
   111  	if err != nil {
   112  		b.Fatal(err)
   113  	}
   114  	b.SetBytes(int64(len(js.Bytes())))
   115  	b.ResetTimer()
   116  	b.ReportAllocs()
   117  	for i := 0; i < b.N; i++ {
   118  		js.Reset()
   119  		UnmarshalAsJSON(&js, bts)
   120  	}
   121  }