gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

commit 0353576c09fce13a46aa90817b690abaf5516763
parent 9925951c87287380fbfbce341b7bba9c48851d7d
Author: francoispqt <francois@parquet.ninja>
Date:   Tue, 15 May 2018 08:33:09 +0800

rename interfaces for v1

Diffstat:
MREADME.md | 100++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mbenchmarks/benchmarks_large.go | 24++++++++++++------------
Mbenchmarks/benchmarks_medium.go | 28++++++++++++++--------------
Mbenchmarks/benchmarks_small.go | 4++--
Mbenchmarks/decoder/decoder_bench_large_test.go | 4++--
Mbenchmarks/decoder/decoder_bench_medium_test.go | 4++--
Mbenchmarks/decoder/decoder_bench_small_test.go | 4++--
Mbenchmarks/decoder/decoder_large_test.go | 2+-
Mbenchmarks/encoder/encoder_bench_large_test.go | 4++--
Mbenchmarks/encoder/encoder_bench_medium_test.go | 4++--
Mbenchmarks/encoder/encoder_bench_small_test.go | 6+++---
Mdecode.go | 50+++++++++++++++++++++++++-------------------------
Mdecode_array.go | 8++++----
Mdecode_array_test.go | 24++++++++++++------------
Mdecode_embedded_json_test.go | 2+-
Mdecode_object.go | 14+++++++-------
Mdecode_object_test.go | 12++++++------
Mdecode_test.go | 10+++++-----
Mdecode_unsafe.go | 8++++----
Mdecode_unsafe_test.go | 8++++----
Mencode.go | 32++++++++++++++++----------------
Mencode_array.go | 26+++++++++++++-------------
Mencode_array_test.go | 24++++++++++++------------
Mencode_embedded_json_test.go | 6+++---
Mencode_interface.go | 16++++++++--------
Mencode_object.go | 36++++++++++++++++++------------------
Mencode_object_test.go | 20++++++++++----------
Mencode_stream.go | 12++++++------
Mexamples/encode-decode-map/main.go | 6+++---
Mexamples/http-json/main.go | 4++--
Mexamples/websocket/comm/comm.go | 10+++++-----
31 files changed, 256 insertions(+), 256 deletions(-)

diff --git a/README.md b/README.md @@ -50,8 +50,8 @@ type user struct { name string email string } -// implement UnmarshalerObject -func (u *user) UnmarshalObject(dec *gojay.Decoder, key string) error { +// implement UnmarshalerJSONObject +func (u *user) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "id": return dec.AddInt(&u.id) @@ -69,7 +69,7 @@ func (u *user) NKeys() int { func main() { u := &user{} d := []byte(`{"id":1,"name":"gojay","email":"gojay@email.com"}`) - err := gojay.UnmarshalObject(d, u) + err := gojay.UnmarshalJSONObject(d, u) if err != nil { log.Fatal(err) } @@ -102,14 +102,14 @@ Unmarshal API comes with three functions: func Unmarshal(data []byte, v interface{}) error ``` -* UnmarshalObject +* UnmarshalJSONObject ```go -func UnmarshalObject(data []byte, v UnmarshalerObject) error +func UnmarshalJSONObject(data []byte, v UnmarshalerJSONObject) error ``` -* UnmarshalArray +* UnmarshalJSONArray ```go -func UnmarshalArray(data []byte, v UnmarshalerArray) error +func UnmarshalJSONArray(data []byte, v UnmarshalerJSONArray) error ``` @@ -149,11 +149,11 @@ func (dec *Decoder) Decode(v interface{}) error ``` * DecodeObject ```go -func (dec *Decoder) DecodeObject(v UnmarshalerObject) error +func (dec *Decoder) DecodeObject(v UnmarshalerJSONObject) error ``` * DecodeArray ```go -func (dec *Decoder) DecodeArray(v UnmarshalerArray) error +func (dec *Decoder) DecodeArray(v UnmarshalerJSONArray) error ``` * DecodeInt ```go @@ -170,16 +170,16 @@ func (dec *Decoder) DecodeString(v *string) error ### Structs and Maps -#### UnmarshalerObject Interface +#### UnmarshalerJSONObject Interface -To unmarshal a JSON object to a structure, the structure must implement the UnmarshalerObject interface: +To unmarshal a JSON object to a structure, the structure must implement the UnmarshalerJSONObject interface: ```go -type UnmarshalerObject interface { - UnmarshalObject(*Decoder, string) error +type UnmarshalerJSONObject interface { + UnmarshalJSONObject(*Decoder, string) error NKeys() int } ``` -UnmarshalObject method takes two arguments, the first one is a pointer to the Decoder (*gojay.Decoder) and the second one is the string value of the current key being parsed. If the JSON data is not an object, the UnmarshalObject method will never be called. +UnmarshalJSONObject method takes two arguments, the first one is a pointer to the Decoder (*gojay.Decoder) and the second one is the string value of the current key being parsed. If the JSON data is not an object, the UnmarshalJSONObject method will never be called. NKeys method must return the number of keys to Unmarshal in the JSON object or 0. If zero is returned, all keys will be parsed. @@ -190,8 +190,8 @@ type user struct { name string email string } -// implement UnmarshalerObject -func (u *user) UnmarshalObject(dec *gojay.Decoder, key string) error { +// implement UnmarshalerJSONObject +func (u *user) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch k { case "id": return dec.AddInt(&u.id) @@ -209,11 +209,11 @@ func (u *user) NKeys() int { Example of implementation for a `map[string]string`: ```go -// define our custom map type implementing UnmarshalerObject +// define our custom map type implementing UnmarshalerJSONObject type message map[string]string // Implementing Unmarshaler -func (m message) UnmarshalObject(dec *gojay.Decoder, k string) error { +func (m message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { str := "" err := dec.AddString(&str) if err != nil { @@ -231,19 +231,19 @@ func (m message) NKeys() int { ### Arrays, Slices and Channels -To unmarshal a JSON object to a slice an array or a channel, it must implement the UnmarshalerArray interface: +To unmarshal a JSON object to a slice an array or a channel, it must implement the UnmarshalerJSONArray interface: ```go -type UnmarshalerArray interface { - UnmarshalArray(*Decoder) error +type UnmarshalerJSONArray interface { + UnmarshalJSONArray(*Decoder) error } ``` -UnmarshalArray method takes one argument, a pointer to the Decoder (*gojay.Decoder). If the JSON data is not an array, the Unmarshal method will never be called. +UnmarshalJSONArray method takes one argument, a pointer to the Decoder (*gojay.Decoder). If the JSON data is not an array, the Unmarshal method will never be called. Example of implementation with a slice: ```go type testSlice []string -// implement UnmarshalerArray -func (t *testStringArr) UnmarshalArray(dec *gojay.Decoder) error { +// implement UnmarshalerJSONArray +func (t *testStringArr) UnmarshalJSONArray(dec *gojay.Decoder) error { str := "" if err := dec.AddString(&str); err != nil { return err @@ -256,8 +256,8 @@ func (t *testStringArr) UnmarshalArray(dec *gojay.Decoder) error { Example of implementation with a channel: ```go type ChannelString chan string -// implement UnmarshalerArray -func (c ChannelArray) UnmarshalArray(dec *gojay.Decoder) error { +// implement UnmarshalerJSONArray +func (c ChannelArray) UnmarshalJSONArray(dec *gojay.Decoder) error { str := "" if err := dec.AddString(&str); err != nil { return err @@ -298,8 +298,8 @@ type user struct { name string email string } -// implement MarshalerObject -func (u *user) MarshalObject(enc *gojay.Encoder) { +// implement MarshalerJSONObject +func (u *user) MarshalJSONObject(enc *gojay.Encoder) { enc.AddIntKey("id", u.id) enc.AddStringKey("name", u.name) enc.AddStringKey("email", u.email) @@ -310,7 +310,7 @@ func (u *user) IsNil() bool { func main() { u := &user{1, "gojay", "gojay@email.com"} - b, err := gojay.MarshalObject(u) + b, err := gojay.MarshalJSONObject(u) if err != nil { log.Fatal(err) } @@ -346,14 +346,14 @@ Marshal API comes with three functions: func Marshal(v interface{}) ([]byte, error) ``` -* MarshalObject +* MarshalJSONObject ```go -func MarshalObject(v MarshalerObject) ([]byte, error) +func MarshalJSONObject(v MarshalerJSONObject) ([]byte, error) ``` -* MarshalArray +* MarshalJSONArray ```go -func MarshalArray(v MarshalerArray) ([]byte, error) +func MarshalJSONArray(v MarshalerJSONArray) ([]byte, error) ``` ### Encode API @@ -394,11 +394,11 @@ func (enc *Encoder) Encode(v interface{}) error ``` * EncodeObject ```go -func (enc *Encoder) EncodeObject(v MarshalerObject) error +func (enc *Encoder) EncodeObject(v MarshalerJSONObject) error ``` * EncodeArray ```go -func (enc *Encoder) EncodeArray(v MarshalerArray) error +func (enc *Encoder) EncodeArray(v MarshalerJSONArray) error ``` * EncodeInt ```go @@ -423,14 +423,14 @@ func (enc *Encoder) EncodeString(s string) error ### Structs and Maps -To encode a structure, the structure must implement the MarshalerObject interface: +To encode a structure, the structure must implement the MarshalerJSONObject interface: ```go -type MarshalerObject interface { - MarshalObject(enc *Encoder) +type MarshalerJSONObject interface { + MarshalJSONObject(enc *Encoder) IsNil() bool } ``` -MarshalObject method takes one argument, a pointer to the Encoder (*gojay.Encoder). The method must add all the keys in the JSON Object by calling Decoder's methods. +MarshalJSONObject method takes one argument, a pointer to the Encoder (*gojay.Encoder). The method must add all the keys in the JSON Object by calling Decoder's methods. IsNil method returns a boolean indicating if the interface underlying value is nil or not. It is used to safely ensure that the underlying value is not nil without using Reflection. @@ -441,8 +441,8 @@ type user struct { name string email string } -// implement MarshalerObject -func (u *user) MarshalObject(dec *gojay.Decoder, key string) { +// implement MarshalerJSONObject +func (u *user) MarshalJSONObject(dec *gojay.Decoder, key string) { dec.AddIntKey("id", u.id) dec.AddStringKey("name", u.name) dec.AddStringKey("email", u.email) @@ -454,11 +454,11 @@ func (u *user) IsNil() bool { Example of implementation for a `map[string]string`: ```go -// define our custom map type implementing MarshalerObject +// define our custom map type implementing MarshalerJSONObject type message map[string]string // Implementing Marshaler -func (m message) MarshalObject(enc *gojay.Encoder) { +func (m message) MarshalJSONObject(enc *gojay.Encoder) { for k, v := range m { enc.AddStringKey(k, v) } @@ -470,22 +470,22 @@ func (m message) IsNil() bool { ``` ### Arrays and Slices -To encode an array or a slice, the slice/array must implement the MarshalerArray interface: +To encode an array or a slice, the slice/array must implement the MarshalerJSONArray interface: ```go -type MarshalerArray interface { - MarshalArray(enc *Encoder) +type MarshalerJSONArray interface { + MarshalJSONArray(enc *Encoder) IsNil() bool } ``` -MarshalArray method takes one argument, a pointer to the Encoder (*gojay.Encoder). The method must add all element in the JSON Array by calling Decoder's methods. +MarshalJSONArray method takes one argument, a pointer to the Encoder (*gojay.Encoder). The method must add all element in the JSON Array by calling Decoder's methods. IsNil method returns a boolean indicating if the interface underlying value is nil(empty) or not. It is used to safely ensure that the underlying value is not nil without using Reflection and also to in `OmitEmpty` feature. Example of implementation: ```go type users []*user -// implement MarshalerArray -func (u *users) MarshalArray(dec *Decoder) { +// implement MarshalerJSONArray +func (u *users) MarshalJSONArray(dec *Decoder) { for _, e := range u { enc.AddObject(e) } @@ -591,7 +591,7 @@ type user struct { email string } -func (u *user) MarshalObject(enc *gojay.Encoder) { +func (u *user) MarshalJSONObject(enc *gojay.Encoder) { enc.AddIntKey("id", u.id) enc.AddStringKey("name", u.name) enc.AddStringKey("email", u.email) diff --git a/benchmarks/benchmarks_large.go b/benchmarks/benchmarks_large.go @@ -10,7 +10,7 @@ type DSUser struct { Username string } -func (m *DSUser) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (m *DSUser) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "username": return dec.AddString(&m.Username) @@ -23,7 +23,7 @@ func (m *DSUser) NKeys() int { func (m *DSUser) IsNil() bool { return m == nil } -func (m *DSUser) MarshalObject(enc *gojay.Encoder) { +func (m *DSUser) MarshalJSONObject(enc *gojay.Encoder) { enc.AddStringKey("username", m.Username) } @@ -32,7 +32,7 @@ type DSTopic struct { Slug string } -func (m *DSTopic) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (m *DSTopic) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "id": return dec.AddInt(&m.Id) @@ -47,20 +47,20 @@ func (m *DSTopic) NKeys() int { func (m *DSTopic) IsNil() bool { return m == nil } -func (m *DSTopic) MarshalObject(enc *gojay.Encoder) { +func (m *DSTopic) MarshalJSONObject(enc *gojay.Encoder) { enc.AddIntKey("id", m.Id) enc.AddStringKey("slug", m.Slug) } type DSTopics []*DSTopic -func (t *DSTopics) UnmarshalArray(dec *gojay.Decoder) error { +func (t *DSTopics) UnmarshalJSONArray(dec *gojay.Decoder) error { dsTopic := &DSTopic{} *t = append(*t, dsTopic) return dec.AddObject(dsTopic) } -func (m *DSTopics) MarshalArray(enc *gojay.Encoder) { +func (m *DSTopics) MarshalJSONArray(enc *gojay.Encoder) { for _, e := range *m { enc.AddObject(e) } @@ -74,7 +74,7 @@ type DSTopicsList struct { MoreTopicsUrl string } -func (m *DSTopicsList) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (m *DSTopicsList) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "topics": m.Topics = DSTopics{} @@ -92,20 +92,20 @@ func (m *DSTopicsList) IsNil() bool { return m == nil } -func (m *DSTopicsList) MarshalObject(enc *gojay.Encoder) { +func (m *DSTopicsList) MarshalJSONObject(enc *gojay.Encoder) { enc.AddArrayKey("users", &m.Topics) enc.AddStringKey("more_topics_url", m.MoreTopicsUrl) } type DSUsers []*DSUser -func (t *DSUsers) UnmarshalArray(dec *gojay.Decoder) error { +func (t *DSUsers) UnmarshalJSONArray(dec *gojay.Decoder) error { dsUser := DSUser{} *t = append(*t, &dsUser) return dec.AddObject(&dsUser) } -func (m *DSUsers) MarshalArray(enc *gojay.Encoder) { +func (m *DSUsers) MarshalJSONArray(enc *gojay.Encoder) { for _, e := range *m { enc.AddObject(e) } @@ -119,7 +119,7 @@ type LargePayload struct { Topics *DSTopicsList } -func (m *LargePayload) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (m *LargePayload) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "users": return dec.AddArray(&m.Users) @@ -135,7 +135,7 @@ func (m *LargePayload) NKeys() int { } //easyjson:json -func (m *LargePayload) MarshalObject(enc *gojay.Encoder) { +func (m *LargePayload) MarshalJSONObject(enc *gojay.Encoder) { enc.AddArrayKey("users", &m.Users) enc.AddObjectKey("topics", m.Topics) } diff --git a/benchmarks/benchmarks_medium.go b/benchmarks/benchmarks_medium.go @@ -101,7 +101,7 @@ type CBAvatar struct { Url string } -func (m *CBAvatar) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (m *CBAvatar) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "avatars": return dec.AddString(&m.Url) @@ -112,7 +112,7 @@ func (m *CBAvatar) NKeys() int { return 1 } -func (m *CBAvatar) MarshalObject(enc *gojay.Encoder) { +func (m *CBAvatar) MarshalJSONObject(enc *gojay.Encoder) { enc.AddStringKey("url", m.Url) } @@ -122,13 +122,13 @@ func (m *CBAvatar) IsNil() bool { type Avatars []*CBAvatar -func (t *Avatars) UnmarshalArray(dec *gojay.Decoder) error { +func (t *Avatars) UnmarshalJSONArray(dec *gojay.Decoder) error { avatar := CBAvatar{} *t = append(*t, &avatar) return dec.AddObject(&avatar) } -func (m *Avatars) MarshalArray(enc *gojay.Encoder) { +func (m *Avatars) MarshalJSONArray(enc *gojay.Encoder) { for _, e := range *m { enc.AddObject(e) } @@ -141,7 +141,7 @@ type CBGravatar struct { Avatars Avatars } -func (m *CBGravatar) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (m *CBGravatar) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "avatars": return dec.AddArray(&m.Avatars) @@ -152,7 +152,7 @@ func (m *CBGravatar) NKeys() int { return 1 } -func (m *CBGravatar) MarshalObject(enc *gojay.Encoder) { +func (m *CBGravatar) MarshalJSONObject(enc *gojay.Encoder) { enc.AddArrayKey("avatars", &m.Avatars) } @@ -164,7 +164,7 @@ type CBGithub struct { Followers int } -func (m *CBGithub) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (m *CBGithub) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "followers": return dec.AddInt(&m.Followers) @@ -176,7 +176,7 @@ func (m *CBGithub) NKeys() int { return 1 } -func (m *CBGithub) MarshalObject(enc *gojay.Encoder) { +func (m *CBGithub) MarshalJSONObject(enc *gojay.Encoder) { enc.AddIntKey("followers", m.Followers) } @@ -188,7 +188,7 @@ type CBName struct { FullName string `json:"fullName"` } -func (m *CBName) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (m *CBName) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "fullName": return dec.AddString(&m.FullName) @@ -200,7 +200,7 @@ func (m *CBName) NKeys() int { return 1 } -func (m *CBName) MarshalObject(enc *gojay.Encoder) { +func (m *CBName) MarshalJSONObject(enc *gojay.Encoder) { enc.AddStringKey("fullName", m.FullName) } @@ -214,7 +214,7 @@ type CBPerson struct { Gravatar *CBGravatar } -func (m *CBPerson) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (m *CBPerson) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "name": m.Name = &CBName{} @@ -233,7 +233,7 @@ func (m *CBPerson) NKeys() int { return 3 } -func (m *CBPerson) MarshalObject(enc *gojay.Encoder) { +func (m *CBPerson) MarshalJSONObject(enc *gojay.Encoder) { enc.AddObjectKey("name", m.Name) enc.AddObjectKey("github", m.Github) enc.AddObjectKey("gravatar", m.Gravatar) @@ -249,7 +249,7 @@ type MediumPayload struct { Company string `json:"company"` } -func (m *MediumPayload) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (m *MediumPayload) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "person": m.Person = &CBPerson{} @@ -264,7 +264,7 @@ func (m *MediumPayload) NKeys() int { return 2 } -func (m *MediumPayload) MarshalObject(enc *gojay.Encoder) { +func (m *MediumPayload) MarshalJSONObject(enc *gojay.Encoder) { enc.AddObjectKey("person", m.Person) // enc.AddStringKey("company", m.Company) } diff --git a/benchmarks/benchmarks_small.go b/benchmarks/benchmarks_small.go @@ -17,7 +17,7 @@ type SmallPayload struct { V int } -func (t *SmallPayload) MarshalObject(enc *gojay.Encoder) { +func (t *SmallPayload) MarshalJSONObject(enc *gojay.Encoder) { enc.AddIntKey("st", t.St) enc.AddIntKey("sid", t.Sid) enc.AddStringKey("tt", t.Tt) @@ -33,7 +33,7 @@ func (t *SmallPayload) IsNil() bool { return t == nil } -func (t *SmallPayload) UnmarshalObject(dec *gojay.Decoder, key string) error { +func (t *SmallPayload) UnmarshalJSONObject(dec *gojay.Decoder, key string) error { switch key { case "st": return dec.AddInt(&t.St) diff --git a/benchmarks/decoder/decoder_bench_large_test.go b/benchmarks/decoder/decoder_bench_large_test.go @@ -46,7 +46,7 @@ func BenchmarkGoJayDecodeObjLarge(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { result := benchmarks.LargePayload{} - gojay.UnmarshalObject(benchmarks.LargeFixture, &result) + gojay.UnmarshalJSONObject(benchmarks.LargeFixture, &result) } } @@ -54,6 +54,6 @@ func BenchmarkGoJayUnsafeDecodeObjLarge(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { result := benchmarks.LargePayload{} - gojay.Unsafe.UnmarshalObject(benchmarks.LargeFixture, &result) + gojay.Unsafe.UnmarshalJSONObject(benchmarks.LargeFixture, &result) } } diff --git a/benchmarks/decoder/decoder_bench_medium_test.go b/benchmarks/decoder/decoder_bench_medium_test.go @@ -54,7 +54,7 @@ func BenchmarkGoJayDecodeObjMedium(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { result := benchmarks.MediumPayload{} - err := gojay.UnmarshalObject(benchmarks.MediumFixture, &result) + err := gojay.UnmarshalJSONObject(benchmarks.MediumFixture, &result) if err != nil { b.Error(err) } @@ -64,7 +64,7 @@ func BenchmarkGoJayUnsafeDecodeObjMedium(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { result := benchmarks.MediumPayload{} - err := gojay.Unsafe.UnmarshalObject(benchmarks.MediumFixture, &result) + err := gojay.Unsafe.UnmarshalJSONObject(benchmarks.MediumFixture, &result) if err != nil { b.Error(err) } diff --git a/benchmarks/decoder/decoder_bench_small_test.go b/benchmarks/decoder/decoder_bench_small_test.go @@ -56,7 +56,7 @@ func BenchmarkGoJayDecodeObjSmall(b *testing.B) { b.ReportAllocs() for n := 0; n < b.N; n++ { result := benchmarks.SmallPayload{} - gojay.UnmarshalObject(benchmarks.SmallFixture, &result) + gojay.UnmarshalJSONObject(benchmarks.SmallFixture, &result) } } @@ -64,6 +64,6 @@ func BenchmarkGoJayUnsafeDecodeObjSmall(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { result := benchmarks.SmallPayload{} - gojay.Unsafe.UnmarshalObject(benchmarks.SmallFixture, &result) + gojay.Unsafe.UnmarshalJSONObject(benchmarks.SmallFixture, &result) } } diff --git a/benchmarks/decoder/decoder_large_test.go b/benchmarks/decoder/decoder_large_test.go @@ -11,7 +11,7 @@ import ( func TestGoJayDecodeObjLarge(t *testing.T) { result := benchmarks.LargePayload{} - err := gojay.UnmarshalObject(benchmarks.LargeFixture, &result) + err := gojay.UnmarshalJSONObject(benchmarks.LargeFixture, &result) assert.Nil(t, err, "err should be nil") assert.Len(t, result.Users, 32, "Len of users should be 32") for _, u := range result.Users { diff --git a/benchmarks/encoder/encoder_bench_large_test.go b/benchmarks/encoder/encoder_bench_large_test.go @@ -41,14 +41,14 @@ func BenchmarkEasyJsonEncodeObjLarge(b *testing.B) { func BenchmarkGoJayEncodeLargeStruct(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { - if _, err := gojay.MarshalObject(benchmarks.NewLargePayload()); err != nil { + if _, err := gojay.MarshalJSONObject(benchmarks.NewLargePayload()); err != nil { b.Fatal(err) } } } func TestGoJayEncodeLargeStruct(t *testing.T) { - if output, err := gojay.MarshalObject(benchmarks.NewLargePayload()); err != nil { + if output, err := gojay.MarshalJSONObject(benchmarks.NewLargePayload()); err != nil { t.Fatal(err) } else { log.Print(string(output)) diff --git a/benchmarks/encoder/encoder_bench_medium_test.go b/benchmarks/encoder/encoder_bench_medium_test.go @@ -42,14 +42,14 @@ func BenchmarkEasyJsonEncodeObjMedium(b *testing.B) { func BenchmarkGoJayEncodeMediumStruct(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { - if _, err := gojay.MarshalObject(benchmarks.NewMediumPayload()); err != nil { + if _, err := gojay.MarshalJSONObject(benchmarks.NewMediumPayload()); err != nil { b.Fatal(err) } } } func TestGoJayEncodeMediumStruct(t *testing.T) { - if output, err := gojay.MarshalObject(benchmarks.NewMediumPayload()); err != nil { + if output, err := gojay.MarshalJSONObject(benchmarks.NewMediumPayload()); err != nil { t.Fatal(err) } else { log.Print(string(output)) diff --git a/benchmarks/encoder/encoder_bench_small_test.go b/benchmarks/encoder/encoder_bench_small_test.go @@ -42,7 +42,7 @@ func BenchmarkJsonIterEncodeSmallStruct(b *testing.B) { func BenchmarkGoJayEncodeSmallStruct(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { - if _, err := gojay.MarshalObject(benchmarks.NewSmallPayload()); err != nil { + if _, err := gojay.MarshalJSONObject(benchmarks.NewSmallPayload()); err != nil { b.Fatal(err) } } @@ -51,7 +51,7 @@ func BenchmarkGoJayEncodeSmallStruct(b *testing.B) { func BenchmarkGoJayEncodeSmallFunc(b *testing.B) { b.ReportAllocs() for i := 0; i < b.N; i++ { - if _, err := gojay.MarshalObject(gojay.EncodeObjectFunc(func(enc *gojay.Encoder) { + if _, err := gojay.MarshalJSONObject(gojay.EncodeObjectFunc(func(enc *gojay.Encoder) { enc.AddIntKey("st", 1) enc.AddIntKey("sid", 1) enc.AddStringKey("tt", "test") @@ -68,7 +68,7 @@ func BenchmarkGoJayEncodeSmallFunc(b *testing.B) { } func TestGoJayEncodeSmallStruct(t *testing.T) { - if output, err := gojay.MarshalObject(benchmarks.NewSmallPayload()); err != nil { + if output, err := gojay.MarshalJSONObject(benchmarks.NewSmallPayload()); err != nil { t.Fatal(err) } else { log.Print(output) diff --git a/decode.go b/decode.go @@ -6,13 +6,13 @@ import ( "reflect" ) -// UnmarshalArray parses the JSON-encoded data and stores the result in the value pointed to by v. +// UnmarshalJSONArray parses the JSON-encoded data and stores the result in the value pointed to by v. // -// v must implement UnmarshalerArray. +// v must implement UnmarshalerJSONArray. // // If a JSON value is not appropriate for a given target type, or if a JSON number -// overflows the target type, UnmarshalArray skips that field and completes the unmarshaling as best it can. -func UnmarshalArray(data []byte, v UnmarshalerArray) error { +// overflows the target type, UnmarshalJSONArray skips that field and completes the unmarshaling as best it can. +func UnmarshalJSONArray(data []byte, v UnmarshalerJSONArray) error { dec := borrowDecoder(nil, 0) defer dec.Release() dec.data = make([]byte, len(data)) @@ -28,13 +28,13 @@ func UnmarshalArray(data []byte, v UnmarshalerArray) error { return nil } -// UnmarshalObject parses the JSON-encoded data and stores the result in the value pointed to by v. +// UnmarshalJSONObject parses the JSON-encoded data and stores the result in the value pointed to by v. // -// v must implement UnmarshalerObject. +// v must implement UnmarshalerJSONObject. // // If a JSON value is not appropriate for a given target type, or if a JSON number -// overflows the target type, UnmarshalObject skips that field and completes the unmarshaling as best it can. -func UnmarshalObject(data []byte, v UnmarshalerObject) error { +// overflows the target type, UnmarshalJSONObject skips that field and completes the unmarshaling as best it can. +func UnmarshalJSONObject(data []byte, v UnmarshalerJSONObject) error { dec := borrowDecoder(nil, 0) defer dec.Release() dec.data = make([]byte, len(data)) @@ -51,7 +51,7 @@ func UnmarshalObject(data []byte, v UnmarshalerObject) error { } // Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. -// If v is nil, not a pointer, or not an implementation of UnmarshalerObject or UnmarshalerArray +// If v is nil, not a pointer, or not an implementation of UnmarshalerJSONObject or UnmarshalerJSONArray // Unmarshal returns an InvalidUnmarshalError. // // Unmarshal uses the inverse of the encodings that Marshal uses, allocating maps, slices, and pointers as necessary, with the following additional rules: @@ -60,9 +60,9 @@ func UnmarshalObject(data []byte, v UnmarshalerObject) error { // Otherwise, Unmarshal unmarshals the JSON into the value pointed at by the pointer. // If the pointer is nil, Unmarshal allocates a new value for it to point to. // -// To Unmarshal JSON into a struct, Unmarshal requires the struct to implement UnmarshalerObject. +// To Unmarshal JSON into a struct, Unmarshal requires the struct to implement UnmarshalerJSONObject. // -// To unmarshal a JSON array into a slice, Unmarshal requires the slice to implement UnmarshalerArray. +// To unmarshal a JSON array into a slice, Unmarshal requires the slice to implement UnmarshalerJSONArray. // // Unmarshal JSON does not allow yet to unmarshall an interface value // If a JSON value is not appropriate for a given target type, or if a JSON number @@ -112,13 +112,13 @@ func Unmarshal(data []byte, v interface{}) error { dec.length = len(data) dec.data = data err = dec.decodeBool(vt) - case UnmarshalerObject: + case UnmarshalerJSONObject: dec = borrowDecoder(nil, 0) dec.length = len(data) dec.data = make([]byte, len(data)) copy(dec.data, data) _, err = dec.decodeObject(vt) - case UnmarshalerArray: + case UnmarshalerJSONArray: dec = borrowDecoder(nil, 0) dec.length = len(data) dec.data = make([]byte, len(data)) @@ -134,17 +134,17 @@ func Unmarshal(data []byte, v interface{}) error { return dec.err } -// UnmarshalerObject is the interface to implement for a struct to be +// UnmarshalerJSONObject is the interface to implement for a struct to be // decoded -type UnmarshalerObject interface { - UnmarshalObject(*Decoder, string) error +type UnmarshalerJSONObject interface { + UnmarshalJSONObject(*Decoder, string) error NKeys() int } -// UnmarshalerArray is the interface to implement for a slice or an array to be +// UnmarshalerJSONArray is the interface to implement for a slice or an array to be // decoded -type UnmarshalerArray interface { - UnmarshalArray(*Decoder) error +type UnmarshalerJSONArray interface { + UnmarshalJSONArray(*Decoder) error } // A Decoder reads and decodes JSON values from an input stream. @@ -184,10 +184,10 @@ func (dec *Decoder) Decode(v interface{}) error { return dec.decodeFloat64(vt) case *bool: return dec.decodeBool(vt) - case UnmarshalerObject: + case UnmarshalerJSONObject: _, err := dec.decodeObject(vt) return err - case UnmarshalerArray: + case UnmarshalerJSONArray: _, err := dec.decodeArray(vt) return err case *EmbeddedJSON: @@ -244,8 +244,8 @@ func (dec *Decoder) AddString(v *string) error { return nil } -// AddObject decodes the next key to a UnmarshalerObject. -func (dec *Decoder) AddObject(value UnmarshalerObject) error { +// AddObject decodes the next key to a UnmarshalerJSONObject. +func (dec *Decoder) AddObject(value UnmarshalerJSONObject) error { initialKeysDone := dec.keysDone initialChild := dec.child dec.keysDone = 0 @@ -262,8 +262,8 @@ func (dec *Decoder) AddObject(value UnmarshalerObject) error { return nil } -// AddArray decodes the next key to a UnmarshalerArray. -func (dec *Decoder) AddArray(value UnmarshalerArray) error { +// AddArray decodes the next key to a UnmarshalerJSONArray. +func (dec *Decoder) AddArray(value UnmarshalerJSONArray) error { newCursor, err := dec.decodeArray(value) if err != nil { return err diff --git a/decode_array.go b/decode_array.go @@ -6,17 +6,17 @@ import ( // DecodeArray reads the next JSON-encoded value from its input and stores it in the value pointed to by v. // -// v must implement UnmarshalerArray. +// v must implement UnmarshalerJSONArray. // // See the documentation for Unmarshal for details about the conversion of JSON into a Go value. -func (dec *Decoder) DecodeArray(arr UnmarshalerArray) error { +func (dec *Decoder) DecodeArray(arr UnmarshalerJSONArray) error { if dec.isPooled == 1 { panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder")) } _, err := dec.decodeArray(arr) return err } -func (dec *Decoder) decodeArray(arr UnmarshalerArray) (int, error) { +func (dec *Decoder) decodeArray(arr UnmarshalerJSONArray) (int, error) { // not an array not an error, but do not know what to do // do not check syntax for ; dec.cursor < dec.length || dec.read(); dec.cursor++ { @@ -34,7 +34,7 @@ func (dec *Decoder) decodeArray(arr UnmarshalerArray) (int, error) { return dec.cursor, nil } // calling unmarshall function for each element of the slice - err := arr.UnmarshalArray(dec) + err := arr.UnmarshalJSONArray(dec) if err != nil { return 0, err } diff --git a/decode_array_test.go b/decode_array_test.go @@ -9,7 +9,7 @@ import ( type testSliceStrings []string -func (t *testSliceStrings) UnmarshalArray(dec *Decoder) error { +func (t *testSliceStrings) UnmarshalJSONArray(dec *Decoder) error { str := "" if err := dec.AddString(&str); err != nil { return err @@ -20,7 +20,7 @@ func (t *testSliceStrings) UnmarshalArray(dec *Decoder) error { type testSliceInts []*int -func (t *testSliceInts) UnmarshalArray(dec *Decoder) error { +func (t *testSliceInts) UnmarshalJSONArray(dec *Decoder) error { i := 0 ptr := &i *t = append(*t, ptr) @@ -29,7 +29,7 @@ func (t *testSliceInts) UnmarshalArray(dec *Decoder) error { type testSliceObj []*TestObj -func (t *testSliceObj) UnmarshalArray(dec *Decoder) error { +func (t *testSliceObj) UnmarshalJSONArray(dec *Decoder) error { obj := &TestObj{} *t = append(*t, obj) return dec.AddObject(obj) @@ -37,7 +37,7 @@ func (t *testSliceObj) UnmarshalArray(dec *Decoder) error { type testChannelArray chan *TestObj -func (c *testChannelArray) UnmarshalArray(dec *Decoder) error { +func (c *testChannelArray) UnmarshalJSONArray(dec *Decoder) error { obj := &TestObj{} if err := dec.AddObject(obj); err != nil { return err @@ -70,7 +70,7 @@ func TestDecoderSliceArrayOfIntsBasic(t *testing.T) { 2 ]`) testArr := testSliceInts{} - err := UnmarshalArray(json, &testArr) + err := UnmarshalJSONArray(json, &testArr) assert.Nil(t, err, "Err must be nil") assert.Len(t, testArr, 2, "testArr should be of len 2") assert.Equal(t, 1, *testArr[0], "testArr[0] should be 1") @@ -83,7 +83,7 @@ func TestDecoderSliceArrayOfIntsBigInts(t *testing.T) { 545344023293232032 ]`) testArr := testSliceInts{} - err := UnmarshalArray(json, &testArr) + err := UnmarshalJSONArray(json, &testArr) assert.Nil(t, err, "Err must be nil") assert.Len(t, testArr, 2, "testArr should be of len 2") assert.Equal(t, 789034384533530523, *testArr[0], "testArr[0] should be 789034384533530523") @@ -125,7 +125,7 @@ func TestDecoderSliceOfObjectsBasic(t *testing.T) { func TestDecodeSliceInvalidType(t *testing.T) { result := testSliceObj{} - err := UnmarshalArray([]byte(`{}`), &result) + err := UnmarshalJSONArray([]byte(`{}`), &result) assert.NotNil(t, err, "err should not be nil") assert.IsType(t, InvalidTypeError(""), err, "err should be of type InvalidTypeError") assert.Equal(t, "Cannot unmarshall to array, wrong char '{' found at pos 0", err.Error(), "err should not be nil") @@ -150,7 +150,7 @@ func TestDecoderChannelOfObjectsBasic(t *testing.T) { } ]`) testChan := testChannelArray(make(chan *TestObj, 3)) - err := UnmarshalArray(json, &testChan) + err := UnmarshalJSONArray(json, &testChan) assert.Nil(t, err, "Err must be nil") ct := 0 l := len(testChan) @@ -166,7 +166,7 @@ func TestDecoderChannelOfObjectsBasic(t *testing.T) { func TestDecoderSliceInvalidJSON(t *testing.T) { json := []byte(`hello`) testArr := testSliceInts{} - err := UnmarshalArray(json, &testArr) + err := UnmarshalJSONArray(json, &testArr) assert.NotNil(t, err, "Err must not be nil as JSON is invalid") assert.IsType(t, InvalidJSONError(""), err, "err message must be 'Invalid JSON'") } @@ -190,10 +190,10 @@ func TestDecoderSliceDecoderAPIError(t *testing.T) { assert.IsType(t, InvalidJSONError(""), err, "err message must be 'Invalid JSON'") } -func TestUnmarshalArrays(t *testing.T) { +func TestUnmarshalJSONArrays(t *testing.T) { testCases := []struct { name string - v UnmarshalerArray + v UnmarshalerJSONArray d []byte expectations func(err error, v interface{}, t *testing.T) }{ @@ -235,7 +235,7 @@ func TestUnmarshalArrays(t *testing.T) { for _, testCase := range testCases { testCase := testCase t.Run(testCase.name, func(*testing.T) { - err := UnmarshalArray(testCase.d, testCase.v) + err := UnmarshalJSONArray(testCase.d, testCase.v) testCase.expectations(err, testCase.v, t) }) } diff --git a/decode_embedded_json_test.go b/decode_embedded_json_test.go @@ -15,7 +15,7 @@ type Request struct { more int } -func (r *Request) UnmarshalObject(dec *Decoder, key string) error { +func (r *Request) UnmarshalJSONObject(dec *Decoder, key string) error { switch key { case "id": return dec.AddString(&r.id) diff --git a/decode_object.go b/decode_object.go @@ -7,17 +7,17 @@ import ( // DecodeObject reads the next JSON-encoded value from its input and stores it in the value pointed to by v. // -// v must implement UnmarshalerObject. +// v must implement UnmarshalerJSONObject. // // See the documentation for Unmarshal for details about the conversion of JSON into a Go value. -func (dec *Decoder) DecodeObject(j UnmarshalerObject) error { +func (dec *Decoder) DecodeObject(j UnmarshalerJSONObject) error { if dec.isPooled == 1 { panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder")) } _, err := dec.decodeObject(j) return err } -func (dec *Decoder) decodeObject(j UnmarshalerObject) (int, error) { +func (dec *Decoder) decodeObject(j UnmarshalerJSONObject) (int, error) { keys := j.NKeys() for ; dec.cursor < dec.length || dec.read(); dec.cursor++ { switch dec.data[dec.cursor] { @@ -34,7 +34,7 @@ func (dec *Decoder) decodeObject(j UnmarshalerObject) (int, error) { } else if done { return dec.cursor, nil } - err = j.UnmarshalObject(dec, k) + err = j.UnmarshalJSONObject(dec, k) if err != nil { return 0, err } else if dec.called&1 == 0 { @@ -55,7 +55,7 @@ func (dec *Decoder) decodeObject(j UnmarshalerObject) (int, error) { } else if done { return dec.cursor, nil } - err = j.UnmarshalObject(dec, k) + err = j.UnmarshalJSONObject(dec, k) if err != nil { return 0, err } else if dec.called&1 == 0 { @@ -247,8 +247,8 @@ func (dec *Decoder) skipData() error { // })) type DecodeObjectFunc func(*Decoder, string) error -// UnmarshalObject implements UnarshalerObject. -func (f DecodeObjectFunc) UnmarshalObject(dec *Decoder, k string) error { +// UnmarshalJSONObject implements UnarshalerObject. +func (f DecodeObjectFunc) UnmarshalJSONObject(dec *Decoder, k string) error { return f(dec, k) } diff --git a/decode_object_test.go b/decode_object_test.go @@ -27,7 +27,7 @@ type TestSubObj struct { testSubSubObj2 *TestSubObj } -func (t *TestSubObj) UnmarshalObject(dec *Decoder, key string) error { +func (t *TestSubObj) UnmarshalJSONObject(dec *Decoder, key string) error { switch key { case "test": return dec.AddInt(&t.test3) @@ -49,7 +49,7 @@ func (t *TestSubObj) NKeys() int { return 0 } -func (t *TestObj) UnmarshalObject(dec *Decoder, key string) error { +func (t *TestObj) UnmarshalJSONObject(dec *Decoder, key string) error { switch key { case "test": return dec.AddInt(&t.test) @@ -196,7 +196,7 @@ type jsonObjectComplex struct { testObjInvalidType *jsonObjectComplex } -func (j *jsonObjectComplex) UnmarshalObject(dec *Decoder, key string) error { +func (j *jsonObjectComplex) UnmarshalJSONObject(dec *Decoder, key string) error { switch key { case "test": return dec.AddString(&j.Test) @@ -222,7 +222,7 @@ func (j *jsonObjectComplex) NKeys() int { func TestDecodeObjComplex(t *testing.T) { result := jsonObjectComplex{} - err := UnmarshalObject(jsonComplex, &result) + err := UnmarshalJSONObject(jsonComplex, &result) assert.NotNil(t, err, "err should not be as invalid type as been encountered nil") assert.Equal(t, `Cannot unmarshal to struct, wrong char '"' found at pos 639`, err.Error(), "err should not be as invalid type as been encountered nil") assert.Equal(t, `{"test":"1","test1":2}`, result.Test, "result.Test is not expected value") @@ -239,7 +239,7 @@ type jsonDecodePartial struct { Test2 string } -func (j *jsonDecodePartial) UnmarshalObject(dec *Decoder, key string) error { +func (j *jsonDecodePartial) UnmarshalJSONObject(dec *Decoder, key string) error { switch key { case "test": return dec.AddString(&j.Test) @@ -285,7 +285,7 @@ func TestDecoderObjectInvalidJSON(t *testing.T) { type myMap map[string]string -func (m myMap) UnmarshalObject(dec *Decoder, k string) error { +func (m myMap) UnmarshalJSONObject(dec *Decoder, k string) error { str := "" err := dec.AddString(&str) if err != nil { diff --git a/decode_test.go b/decode_test.go @@ -13,7 +13,7 @@ type testDecodeObj struct { test string } -func (t *testDecodeObj) UnmarshalObject(dec *Decoder, key string) error { +func (t *testDecodeObj) UnmarshalJSONObject(dec *Decoder, key string) error { switch key { case "test": return dec.AddString(&t.test) @@ -26,7 +26,7 @@ func (t *testDecodeObj) NKeys() int { type testDecodeSlice []*testDecodeObj -func (t *testDecodeSlice) UnmarshalArray(dec *Decoder) error { +func (t *testDecodeSlice) UnmarshalJSONArray(dec *Decoder) error { obj := &testDecodeObj{} if err := dec.AddObject(obj); err != nil { return err @@ -497,10 +497,10 @@ func TestDecodeAllTypes(t *testing.T) { } } -func TestUnmarshalObjects(t *testing.T) { +func TestUnmarshalJSONObjects(t *testing.T) { testCases := []struct { name string - v UnmarshalerObject + v UnmarshalerJSONObject d []byte expectations func(err error, v interface{}, t *testing.T) }{ @@ -547,7 +547,7 @@ func TestUnmarshalObjects(t *testing.T) { for _, testCase := range testCases { testCase := testCase t.Run(testCase.name, func(*testing.T) { - err := UnmarshalObject(testCase.d, testCase.v) + err := UnmarshalJSONObject(testCase.d, testCase.v) testCase.expectations(err, testCase.v, t) }) } diff --git a/decode_unsafe.go b/decode_unsafe.go @@ -13,7 +13,7 @@ var Unsafe = decUnsafe{} type decUnsafe struct{} -func (u decUnsafe) UnmarshalArray(data []byte, v UnmarshalerArray) error { +func (u decUnsafe) UnmarshalJSONArray(data []byte, v UnmarshalerJSONArray) error { dec := borrowDecoder(nil, 0) defer dec.Release() dec.data = data @@ -28,7 +28,7 @@ func (u decUnsafe) UnmarshalArray(data []byte, v UnmarshalerArray) error { return nil } -func (u decUnsafe) UnmarshalObject(data []byte, v UnmarshalerObject) error { +func (u decUnsafe) UnmarshalJSONObject(data []byte, v UnmarshalerJSONObject) error { dec := borrowDecoder(nil, 0) defer dec.Release() dec.data = data @@ -87,12 +87,12 @@ func (u decUnsafe) Unmarshal(data []byte, v interface{}) error { dec.length = len(data) dec.data = data err = dec.decodeBool(vt) - case UnmarshalerObject: + case UnmarshalerJSONObject: dec = borrowDecoder(nil, 0) dec.length = len(data) dec.data = data _, err = dec.decodeObject(vt) - case UnmarshalerArray: + case UnmarshalerJSONArray: dec = borrowDecoder(nil, 0) dec.length = len(data) dec.data = data diff --git a/decode_unsafe_test.go b/decode_unsafe_test.go @@ -233,7 +233,7 @@ func TestUnmarshalUnsafeAllTypes(t *testing.T) { func TestUnmarshalUnsafeObjects(t *testing.T) { testCases := []struct { name string - v UnmarshalerObject + v UnmarshalerJSONObject d []byte expectations func(err error, v interface{}, t *testing.T) }{ @@ -280,7 +280,7 @@ func TestUnmarshalUnsafeObjects(t *testing.T) { for _, testCase := range testCases { testCase := testCase t.Run(testCase.name, func(*testing.T) { - err := Unsafe.UnmarshalObject(testCase.d, testCase.v) + err := Unsafe.UnmarshalJSONObject(testCase.d, testCase.v) testCase.expectations(err, testCase.v, t) }) } @@ -289,7 +289,7 @@ func TestUnmarshalUnsafeObjects(t *testing.T) { func TestUnmarshalUnsafeArrays(t *testing.T) { testCases := []struct { name string - v UnmarshalerArray + v UnmarshalerJSONArray d []byte expectations func(err error, v interface{}, t *testing.T) }{ @@ -331,7 +331,7 @@ func TestUnmarshalUnsafeArrays(t *testing.T) { for _, testCase := range testCases { testCase := testCase t.Run(testCase.name, func(*testing.T) { - err := Unsafe.UnmarshalArray(testCase.d, testCase.v) + err := Unsafe.UnmarshalJSONArray(testCase.d, testCase.v) testCase.expectations(err, testCase.v, t) }) } diff --git a/encode.go b/encode.go @@ -6,7 +6,7 @@ import ( "reflect" ) -// MarshalObject returns the JSON encoding of v. +// MarshalJSONObject returns the JSON encoding of v. // // It takes a struct implementing Marshaler to a JSON slice of byte // it returns a slice of bytes and an error. @@ -14,7 +14,7 @@ import ( // type TestStruct struct { // id int // } -// func (s *TestStruct) MarshalObject(enc *gojay.Encoder) { +// func (s *TestStruct) MarshalJSONObject(enc *gojay.Encoder) { // enc.AddIntKey("id", s.id) // } // func (s *TestStruct) IsNil() bool { @@ -28,21 +28,21 @@ import ( // b, _ := gojay.Marshal(test) // fmt.Println(b) // {"id":123456} // } -func MarshalObject(v MarshalerObject) ([]byte, error) { +func MarshalJSONObject(v MarshalerJSONObject) ([]byte, error) { enc := BorrowEncoder(nil) enc.grow(512) defer enc.Release() return enc.encodeObject(v) } -// MarshalArray returns the JSON encoding of v. +// MarshalJSONArray returns the JSON encoding of v. // // It takes an array or a slice implementing Marshaler to a JSON slice of byte // it returns a slice of bytes and an error. // Example with an Marshaler: // type TestSlice []*TestStruct // -// func (t TestSlice) MarshalArray(enc *Encoder) { +// func (t TestSlice) MarshalJSONArray(enc *Encoder) { // for _, e := range t { // enc.AddObject(e) // } @@ -56,11 +56,11 @@ func MarshalObject(v MarshalerObject) ([]byte, error) { // b, _ := Marshal(test) // fmt.Println(b) // [{"id":123456},{"id":7890}] // } -func MarshalArray(v MarshalerArray) ([]byte, error) { +func MarshalJSONArray(v MarshalerJSONArray) ([]byte, error) { enc := BorrowEncoder(nil) enc.grow(512) enc.writeByte('[') - v.(MarshalerArray).MarshalArray(enc) + v.(MarshalerJSONArray).MarshalJSONArray(enc) enc.writeByte(']') defer enc.Release() return enc.buf, nil @@ -82,7 +82,7 @@ func MarshalArray(v MarshalerArray) ([]byte, error) { // type TestStruct struct { // id int // } -// func (s *TestStruct) MarshalObject(enc *gojay.Encoder) { +// func (s *TestStruct) MarshalJSONObject(enc *gojay.Encoder) { // enc.AddIntKey("id", s.id) // } // func (s *TestStruct) IsNil() bool { @@ -98,11 +98,11 @@ func MarshalArray(v MarshalerArray) ([]byte, error) { // } func Marshal(v interface{}) ([]byte, error) { switch vt := v.(type) { - case MarshalerObject: + case MarshalerJSONObject: enc := BorrowEncoder(nil) defer enc.Release() return enc.encodeObject(vt) - case MarshalerArray: + case MarshalerJSONArray: enc := BorrowEncoder(nil) defer enc.Release() return enc.encodeArray(vt) @@ -167,16 +167,16 @@ func Marshal(v interface{}) ([]byte, error) { } } -// MarshalerObject is the interface to implement for struct to be encoded -type MarshalerObject interface { - MarshalObject(enc *Encoder) +// MarshalerJSONObject is the interface to implement for struct to be encoded +type MarshalerJSONObject interface { + MarshalJSONObject(enc *Encoder) IsNil() bool } -// MarshalerArray is the interface to implement +// MarshalerJSONArray is the interface to implement // for a slice or an array to be encoded -type MarshalerArray interface { - MarshalArray(enc *Encoder) +type MarshalerJSONArray interface { + MarshalJSONArray(enc *Encoder) IsNil() bool } diff --git a/encode_array.go b/encode_array.go @@ -1,7 +1,7 @@ package gojay -// EncodeArray encodes an implementation of MarshalerArray to JSON -func (enc *Encoder) EncodeArray(v MarshalerArray) error { +// EncodeArray encodes an implementation of MarshalerJSONArray to JSON +func (enc *Encoder) EncodeArray(v MarshalerJSONArray) error { if enc.isPooled == 1 { panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder")) } @@ -13,17 +13,17 @@ func (enc *Encoder) EncodeArray(v MarshalerArray) error { } return nil } -func (enc *Encoder) encodeArray(v MarshalerArray) ([]byte, error) { +func (enc *Encoder) encodeArray(v MarshalerJSONArray) ([]byte, error) { enc.grow(200) enc.writeByte('[') - v.MarshalArray(enc) + v.MarshalJSONArray(enc) enc.writeByte(']') return enc.buf, enc.err } -// AddArray adds an implementation of MarshalerArray to be encoded, must be used inside a slice or array encoding (does not encode a key) +// AddArray adds an implementation of MarshalerJSONArray to be encoded, must be used inside a slice or array encoding (does not encode a key) // value must implement Marshaler -func (enc *Encoder) AddArray(v MarshalerArray) { +func (enc *Encoder) AddArray(v MarshalerJSONArray) { if v.IsNil() { enc.grow(3) r := enc.getPreviousRune() @@ -40,13 +40,13 @@ func (enc *Encoder) AddArray(v MarshalerArray) { enc.writeByte(',') } enc.writeByte('[') - v.MarshalArray(enc) + v.MarshalJSONArray(enc) enc.writeByte(']') } // AddArrayOmitEmpty adds an array or slice to be encoded, must be used inside a slice or array encoding (does not encode a key) // value must implement Marshaler -func (enc *Encoder) AddArrayOmitEmpty(v MarshalerArray) { +func (enc *Encoder) AddArrayOmitEmpty(v MarshalerJSONArray) { if v.IsNil() { return } @@ -56,13 +56,13 @@ func (enc *Encoder) AddArrayOmitEmpty(v MarshalerArray) { enc.writeByte(',') } enc.writeByte('[') - v.MarshalArray(enc) + v.MarshalJSONArray(enc) enc.writeByte(']') } // AddArrayKey adds an array or slice to be encoded, must be used inside an object as it will encode a key // value must implement Marshaler -func (enc *Encoder) AddArrayKey(key string, v MarshalerArray) { +func (enc *Encoder) AddArrayKey(key string, v MarshalerJSONArray) { if v.IsNil() { enc.grow(2 + len(key)) r := enc.getPreviousRune() @@ -83,13 +83,13 @@ func (enc *Encoder) AddArrayKey(key string, v MarshalerArray) { enc.writeByte('"') enc.writeStringEscape(key) enc.writeBytes(objKeyArr) - v.MarshalArray(enc) + v.MarshalJSONArray(enc) enc.writeByte(']') } // AddArrayKeyOmitEmpty adds an array or slice to be encoded and skips it if it is nil. // Must be called inside an object as it will encode a key. -func (enc *Encoder) AddArrayKeyOmitEmpty(key string, v MarshalerArray) { +func (enc *Encoder) AddArrayKeyOmitEmpty(key string, v MarshalerJSONArray) { if v.IsNil() { return } @@ -101,6 +101,6 @@ func (enc *Encoder) AddArrayKeyOmitEmpty(key string, v MarshalerArray) { enc.writeByte('"') enc.writeStringEscape(key) enc.writeBytes(objKeyArr) - v.MarshalArray(enc) + v.MarshalJSONArray(enc) enc.writeByte(']') } diff --git a/encode_array_test.go b/encode_array_test.go @@ -9,7 +9,7 @@ import ( type TestEncodingArrStrings []string -func (t TestEncodingArrStrings) MarshalArray(enc *Encoder) { +func (t TestEncodingArrStrings) MarshalJSONArray(enc *Encoder) { for _, e := range t { enc.AddString(e) } @@ -20,7 +20,7 @@ func (t TestEncodingArrStrings) IsNil() bool { type TestEncodingArr []*TestEncoding -func (t TestEncodingArr) MarshalArray(enc *Encoder) { +func (t TestEncodingArr) MarshalJSONArray(enc *Encoder) { for _, e := range t { enc.AddObject(e) } @@ -31,7 +31,7 @@ func (t TestEncodingArr) IsNil() bool { type testEncodingArrInterfaces []interface{} -func (t testEncodingArrInterfaces) MarshalArray(enc *Encoder) { +func (t testEncodingArrInterfaces) MarshalJSONArray(enc *Encoder) { for _, e := range t { enc.AddInterface(e) } @@ -118,7 +118,7 @@ func TestEncoderArrayMarshalAPI(t *testing.T) { testBool: true, }, } - r, err := MarshalArray(v) + r, err := MarshalJSONArray(v) assert.Nil(t, err, "Error should be nil") assert.Equal( t, @@ -178,7 +178,7 @@ func TestEncoderArrayEncodeAPI(t *testing.T) { type TestEncodingIntOmitEmpty []int -func (t TestEncodingIntOmitEmpty) MarshalArray(enc *Encoder) { +func (t TestEncodingIntOmitEmpty) MarshalJSONArray(enc *Encoder) { for _, e := range t { enc.AddIntOmitEmpty(e) } @@ -189,7 +189,7 @@ func (t TestEncodingIntOmitEmpty) IsNil() bool { type TestEncodingStringOmitEmpty []string -func (t TestEncodingStringOmitEmpty) MarshalArray(enc *Encoder) { +func (t TestEncodingStringOmitEmpty) MarshalJSONArray(enc *Encoder) { for _, e := range t { enc.AddStringOmitEmpty(e) } @@ -200,7 +200,7 @@ func (t TestEncodingStringOmitEmpty) IsNil() bool { type TestEncodingFloatOmitEmpty []float64 -func (t TestEncodingFloatOmitEmpty) MarshalArray(enc *Encoder) { +func (t TestEncodingFloatOmitEmpty) MarshalJSONArray(enc *Encoder) { for _, e := range t { enc.AddFloatOmitEmpty(e) } @@ -211,7 +211,7 @@ func (t TestEncodingFloatOmitEmpty) IsNil() bool { type TestEncodingFloat32OmitEmpty []float32 -func (t TestEncodingFloat32OmitEmpty) MarshalArray(enc *Encoder) { +func (t TestEncodingFloat32OmitEmpty) MarshalJSONArray(enc *Encoder) { for _, e := range t { enc.AddFloat32OmitEmpty(e) } @@ -222,7 +222,7 @@ func (t TestEncodingFloat32OmitEmpty) IsNil() bool { type TestEncodingBoolOmitEmpty []bool -func (t TestEncodingBoolOmitEmpty) MarshalArray(enc *Encoder) { +func (t TestEncodingBoolOmitEmpty) MarshalJSONArray(enc *Encoder) { for _, e := range t { enc.AddBoolOmitEmpty(e) } @@ -233,7 +233,7 @@ func (t TestEncodingBoolOmitEmpty) IsNil() bool { type TestEncodingArrOmitEmpty []TestEncodingBoolOmitEmpty -func (t TestEncodingArrOmitEmpty) MarshalArray(enc *Encoder) { +func (t TestEncodingArrOmitEmpty) MarshalJSONArray(enc *Encoder) { for _, e := range t { enc.AddArrayOmitEmpty(e) } @@ -246,7 +246,7 @@ type TestObjEmpty struct { empty bool } -func (t *TestObjEmpty) MarshalObject(enc *Encoder) { +func (t *TestObjEmpty) MarshalJSONObject(enc *Encoder) { } func (t *TestObjEmpty) IsNil() bool { @@ -255,7 +255,7 @@ func (t *TestObjEmpty) IsNil() bool { type TestEncodingObjOmitEmpty []*TestObjEmpty -func (t TestEncodingObjOmitEmpty) MarshalArray(enc *Encoder) { +func (t TestEncodingObjOmitEmpty) MarshalJSONArray(enc *Encoder) { for _, e := range t { enc.AddObjectOmitEmpty(e) } diff --git a/encode_embedded_json_test.go b/encode_embedded_json_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" ) -func (r *Request) MarshalObject(enc *Encoder) { +func (r *Request) MarshalJSONObject(enc *Encoder) { enc.AddStringKey("id", r.id) enc.AddStringKey("method", r.method) enc.AddEmbeddedJSONKey("params", &r.params) @@ -24,7 +24,7 @@ func (r *Request) IsNil() bool { type EmbeddedJSONArr []EmbeddedJSON -func (ear EmbeddedJSONArr) MarshalArray(enc *Encoder) { +func (ear EmbeddedJSONArr) MarshalJSONArray(enc *Encoder) { for _, e := range ear { enc.AddEmbeddedJSON(&e) } @@ -36,7 +36,7 @@ func (ear EmbeddedJSONArr) IsNil() bool { type EmbeddedJSONOmitEmptyArr []EmbeddedJSON -func (ear EmbeddedJSONOmitEmptyArr) MarshalArray(enc *Encoder) { +func (ear EmbeddedJSONOmitEmptyArr) MarshalJSONArray(enc *Encoder) { for _, e := range ear { enc.AddEmbeddedJSONOmitEmpty(&e) } diff --git a/encode_interface.go b/encode_interface.go @@ -18,9 +18,9 @@ func (enc *Encoder) Encode(v interface{}) error { return enc.EncodeString(vt) case bool: return enc.EncodeBool(vt) - case MarshalerArray: + case MarshalerJSONArray: return enc.EncodeArray(vt) - case MarshalerObject: + case MarshalerJSONObject: return enc.EncodeObject(vt) case int: return enc.EncodeInt(vt) @@ -56,9 +56,9 @@ func (enc *Encoder) AddInterface(value interface{}) { enc.AddString(vt) case bool: enc.AddBool(vt) - case MarshalerArray: + case MarshalerJSONArray: enc.AddArray(vt) - case MarshalerObject: + case MarshalerJSONObject: enc.AddObject(vt) case int: enc.AddInt(vt) @@ -97,9 +97,9 @@ func (enc *Encoder) AddInterfaceKey(key string, value interface{}) { enc.AddStringKey(key, vt) case bool: enc.AddBoolKey(key, vt) - case MarshalerArray: + case MarshalerJSONArray: enc.AddArrayKey(key, vt) - case MarshalerObject: + case MarshalerJSONObject: enc.AddObjectKey(key, vt) case int: enc.AddIntKey(key, vt) @@ -140,9 +140,9 @@ func (enc *Encoder) AddInterfaceKeyOmitEmpty(key string, v interface{}) { enc.AddStringKeyOmitEmpty(key, vt) case bool: enc.AddBoolKeyOmitEmpty(key, vt) - case MarshalerArray: + case MarshalerJSONArray: enc.AddArrayKeyOmitEmpty(key, vt) - case MarshalerObject: + case MarshalerJSONObject: enc.AddObjectKeyOmitEmpty(key, vt) case int: enc.AddIntKeyOmitEmpty(key, vt) diff --git a/encode_object.go b/encode_object.go @@ -6,7 +6,7 @@ var objKeyArr = []byte(`":[`) var objKey = []byte(`":`) // EncodeObject encodes an object to JSON -func (enc *Encoder) EncodeObject(v MarshalerObject) error { +func (enc *Encoder) EncodeObject(v MarshalerJSONObject) error { if enc.isPooled == 1 { panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder")) } @@ -23,19 +23,19 @@ func (enc *Encoder) EncodeObject(v MarshalerObject) error { return nil } -func (enc *Encoder) encodeObject(v MarshalerObject) ([]byte, error) { +func (enc *Encoder) encodeObject(v MarshalerJSONObject) ([]byte, error) { enc.grow(500) enc.writeByte('{') if !v.IsNil() { - v.MarshalObject(enc) + v.MarshalJSONObject(enc) } enc.writeByte('}') return enc.buf, enc.err } // AddObject adds an object to be encoded, must be used inside a slice or array encoding (does not encode a key) -// value must implement MarshalerObject -func (enc *Encoder) AddObject(v MarshalerObject) { +// value must implement MarshalerJSONObject +func (enc *Encoder) AddObject(v MarshalerJSONObject) { if v.IsNil() { enc.grow(2) r := enc.getPreviousRune() @@ -52,14 +52,14 @@ func (enc *Encoder) AddObject(v MarshalerObject) { enc.writeByte(',') } enc.writeByte('{') - v.MarshalObject(enc) + v.MarshalJSONObject(enc) enc.writeByte('}') } // AddObjectOmitEmpty adds an object to be encoded or skips it if IsNil returns true. // Must be used inside a slice or array encoding (does not encode a key) -// value must implement MarshalerObject -func (enc *Encoder) AddObjectOmitEmpty(v MarshalerObject) { +// value must implement MarshalerJSONObject +func (enc *Encoder) AddObjectOmitEmpty(v MarshalerJSONObject) { if v.IsNil() { return } @@ -69,13 +69,13 @@ func (enc *Encoder) AddObjectOmitEmpty(v MarshalerObject) { enc.writeByte(',') } enc.writeByte('{') - v.MarshalObject(enc) + v.MarshalJSONObject(enc) enc.writeByte('}') } // AddObjectKey adds a struct to be encoded, must be used inside an object as it will encode a key -// value must implement MarshalerObject -func (enc *Encoder) AddObjectKey(key string, value MarshalerObject) { +// value must implement MarshalerJSONObject +func (enc *Encoder) AddObjectKey(key string, value MarshalerJSONObject) { if value.IsNil() { enc.grow(2 + len(key)) r := enc.getPreviousRune() @@ -96,14 +96,14 @@ func (enc *Encoder) AddObjectKey(key string, value MarshalerObject) { enc.writeByte('"') enc.writeStringEscape(key) enc.writeBytes(objKeyObj) - value.MarshalObject(enc) + value.MarshalJSONObject(enc) enc.writeByte('}') } // AddObjectKeyOmitEmpty adds an object to be encoded or skips it if IsNil returns true. // Must be used inside a slice or array encoding (does not encode a key) -// value must implement MarshalerObject -func (enc *Encoder) AddObjectKeyOmitEmpty(key string, value MarshalerObject) { +// value must implement MarshalerJSONObject +func (enc *Encoder) AddObjectKeyOmitEmpty(key string, value MarshalerJSONObject) { if value.IsNil() { return } @@ -115,7 +115,7 @@ func (enc *Encoder) AddObjectKeyOmitEmpty(key string, value MarshalerObject) { enc.writeByte('"') enc.writeStringEscape(key) enc.writeBytes(objKeyObj) - value.MarshalObject(enc) + value.MarshalJSONObject(enc) enc.writeByte('}') } @@ -128,12 +128,12 @@ func (enc *Encoder) AddObjectKeyOmitEmpty(key string, value MarshalerObject) { // })) type EncodeObjectFunc func(*Encoder) -// MarshalObject implements MarshalerObject. -func (f EncodeObjectFunc) MarshalObject(enc *Encoder) { +// MarshalJSONObject implements MarshalerJSONObject. +func (f EncodeObjectFunc) MarshalJSONObject(enc *Encoder) { f(enc) } -// IsNil implements MarshalerObject. +// IsNil implements MarshalerJSONObject. func (f EncodeObjectFunc) IsNil() bool { return f == nil } diff --git a/encode_object_test.go b/encode_object_test.go @@ -27,7 +27,7 @@ func (t *testObject) IsNil() bool { return t == nil } -func (t *testObject) MarshalObject(enc *Encoder) { +func (t *testObject) MarshalJSONObject(enc *Encoder) { enc.AddStringKey("testStr", t.testStr) enc.AddIntKey("testInt", t.testInt) enc.AddIntKey("testInt64", int(t.testInt64)) @@ -51,7 +51,7 @@ func (t *testObjectWithUnknownType) IsNil() bool { return t == nil } -func (t *testObjectWithUnknownType) MarshalObject(enc *Encoder) { +func (t *testObjectWithUnknownType) MarshalJSONObject(enc *Encoder) { enc.AddInterfaceKey("unknownType", t.unknownType) } @@ -71,7 +71,7 @@ func (t *TestEncoding) IsNil() bool { return t == nil } -func (t *TestEncoding) MarshalObject(enc *Encoder) { +func (t *TestEncoding) MarshalJSONObject(enc *Encoder) { enc.AddStringKey("test", t.test) enc.AddStringKey("test2", t.test2) enc.AddIntKey("testInt", t.testInt) @@ -95,7 +95,7 @@ func (t *SubObject) IsNil() bool { return t == nil } -func (t *SubObject) MarshalObject(enc *Encoder) { +func (t *SubObject) MarshalJSONObject(enc *Encoder) { enc.AddIntKey("test1", t.test1) enc.AddStringKey("test2", t.test2) enc.AddFloatKey("test3", t.test3) @@ -111,7 +111,7 @@ func (t *testEncodingObjInterfaces) IsNil() bool { return t == nil } -func (t *testEncodingObjInterfaces) MarshalObject(enc *Encoder) { +func (t *testEncodingObjInterfaces) MarshalJSONObject(enc *Encoder) { enc.AddInterfaceKey("interfaceVal", t.interfaceVal) } @@ -168,7 +168,7 @@ func TestEncoderObjectMarshalAPI(t *testing.T) { }, }, } - r, err := MarshalObject(v) + r, err := MarshalJSONObject(v) assert.Nil(t, err, "Error should be nil") assert.Equal( t, @@ -327,7 +327,7 @@ func (t *TestObectOmitEmpty) IsNil() bool { return t == nil } -func (t *TestObectOmitEmpty) MarshalObject(enc *Encoder) { +func (t *TestObectOmitEmpty) MarshalJSONObject(enc *Encoder) { enc.AddIntKeyOmitEmpty("testInt", t.testInt) enc.AddIntKeyOmitEmpty("testIntNotEmpty", 1) enc.AddFloatKeyOmitEmpty("testFloat", t.testFloat) @@ -350,7 +350,7 @@ func (t *TestObectOmitEmptyInterface) IsNil() bool { return t == nil } -func (t *TestObectOmitEmptyInterface) MarshalObject(enc *Encoder) { +func (t *TestObectOmitEmptyInterface) MarshalJSONObject(enc *Encoder) { enc.AddInterfaceKeyOmitEmpty("testInt", 0) enc.AddInterfaceKeyOmitEmpty("testInt64", int64(0)) enc.AddInterfaceKeyOmitEmpty("testInt32", int32(0)) @@ -381,7 +381,7 @@ func TestEncoderObjectOmitEmpty(t *testing.T) { testInt: 0, testObect: &TestObectOmitEmpty{testInt: 1}, } - r, err := MarshalObject(v) + r, err := MarshalJSONObject(v) assert.Nil(t, err, "Error should be nil") assert.Equal( t, @@ -393,7 +393,7 @@ func TestEncoderObjectOmitEmpty(t *testing.T) { t.Run("encoder-omit-empty-interface", func(t *testing.T) { v := &TestObectOmitEmptyInterface{} - r, err := MarshalObject(v) + r, err := MarshalJSONObject(v) assert.Nil(t, err, "Error should be nil") assert.Equal( t, diff --git a/encode_stream.go b/encode_stream.go @@ -124,13 +124,13 @@ func (s *StreamEncoder) Cancel(err error) { } // AddObject adds an object to be encoded. -// value must implement MarshalerObject. -func (s *StreamEncoder) AddObject(v MarshalerObject) { +// value must implement MarshalerJSONObject. +func (s *StreamEncoder) AddObject(v MarshalerJSONObject) { if v.IsNil() { return } s.Encoder.writeByte('{') - v.MarshalObject(s.Encoder) + v.MarshalJSONObject(s.Encoder) s.Encoder.writeByte('}') s.Encoder.writeByte(s.delimiter) } @@ -143,10 +143,10 @@ func (s *StreamEncoder) AddString(v string) { s.Encoder.writeByte(s.delimiter) } -// AddArray adds an implementation of MarshalerArray to be encoded. -func (s *StreamEncoder) AddArray(v MarshalerArray) { +// AddArray adds an implementation of MarshalerJSONArray to be encoded. +func (s *StreamEncoder) AddArray(v MarshalerJSONArray) { s.Encoder.writeByte('[') - v.MarshalArray(s.Encoder) + v.MarshalJSONArray(s.Encoder) s.Encoder.writeByte(']') s.Encoder.writeByte(s.delimiter) } diff --git a/examples/encode-decode-map/main.go b/examples/encode-decode-map/main.go @@ -7,11 +7,11 @@ import ( "github.com/francoispqt/gojay" ) -// define our custom map type implementing MarshalerObject and UnmarshalerObject +// define our custom map type implementing MarshalerJSONObject and UnmarshalerJSONObject type myMap map[string]string // Implementing Unmarshaler -func (m myMap) UnmarshalObject(dec *gojay.Decoder, k string) error { +func (m myMap) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { str := "" err := dec.AddString(&str) if err != nil { @@ -28,7 +28,7 @@ func (m myMap) NKeys() int { } // Implementing Marshaler -func (m myMap) MarshalObject(enc *gojay.Encoder) { +func (m myMap) MarshalJSONObject(enc *gojay.Encoder) { for k, v := range m { enc.AddStringKey(k, v) } diff --git a/examples/http-json/main.go b/examples/http-json/main.go @@ -11,7 +11,7 @@ type message struct { bar string } -func (m *message) UnmarshalObject(dec *gojay.Decoder, k string) error { +func (m *message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { switch k { case "foo": return dec.AddString(&m.foo) @@ -25,7 +25,7 @@ func (m *message) NKeys() int { return 2 } -func (m *message) MarshalObject(dec *gojay.Encoder) { +func (m *message) MarshalJSONObject(dec *gojay.Encoder) { dec.AddStringKey("foo", m.foo) dec.AddStringKey("bar", m.bar) } diff --git a/examples/websocket/comm/comm.go b/examples/websocket/comm/comm.go @@ -15,7 +15,7 @@ type Message struct { UserName string } -func (m *Message) UnmarshalObject(dec *gojay.Decoder, k string) error { +func (m *Message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error { switch k { case "message": return dec.AddString(&m.Message) @@ -28,7 +28,7 @@ func (m *Message) NKeys() int { return 2 } -func (m *Message) MarshalObject(enc *gojay.Encoder) { +func (m *Message) MarshalJSONObject(enc *gojay.Encoder) { enc.AddStringKey("message", m.Message) enc.AddStringKey("userName", m.UserName) } @@ -37,7 +37,7 @@ func (u *Message) IsNil() bool { } // Here are defined our communication types -type Sender chan gojay.MarshalerObject +type Sender chan gojay.MarshalerJSONObject func (s Sender) MarshalStream(enc *gojay.StreamEncoder) { select { @@ -74,12 +74,12 @@ func (sc *SenderReceiver) SetReceiver() { } func (sc *SenderReceiver) SetSender(nCons int) { - sc.Send = Sender(make(chan gojay.MarshalerObject)) + sc.Send = Sender(make(chan gojay.MarshalerJSONObject)) sc.Enc = gojay.Stream.BorrowEncoder(sc.Conn).NConsumer(nCons).LineDelimited() go sc.Enc.EncodeStream(sc.Send) } -func (sc *SenderReceiver) SendMessage(m gojay.MarshalerObject) error { +func (sc *SenderReceiver) SendMessage(m gojay.MarshalerJSONObject) error { select { case <-sc.Enc.Done(): return errors.New("sender closed")