| 8 | ) |
| 9 | |
| 10 | func TestEncoder(t *testing.T) { |
| 11 | encoder := json.NewEncoder() |
| 12 | |
| 13 | object := encoder.Object() |
| 14 | |
| 15 | object.Key("stringKey").String("stringValue") |
| 16 | object.Key("integerKey").Long(1024) |
| 17 | object.Key("floatKey").Double(3.14) |
| 18 | |
| 19 | subObj := object.Key("foo").Object() |
| 20 | |
| 21 | subObj.Key("byteSlice").Base64EncodeBytes([]byte("foo bar")) |
| 22 | subObj.Close() |
| 23 | |
| 24 | object.Close() |
| 25 | |
| 26 | e := []byte(`{"stringKey":"stringValue","integerKey":1024,"floatKey":3.14,"foo":{"byteSlice":"Zm9vIGJhcg=="}}`) |
| 27 | if a := encoder.Bytes(); bytes.Compare(e, a) != 0 { |
| 28 | t.Errorf("expected %+q, but got %+q", e, a) |
| 29 | } |
| 30 | |
| 31 | if a := encoder.String(); string(e) != a { |
| 32 | t.Errorf("expected %s, but got %s", e, a) |
| 33 | } |
| 34 | } |