commit b1c88ac7fccc2e0c992aa32529ebc79edd537261
parent ca0442d6e33334128a2ee68f24c6c962de72ead3
Author: Pavel Makarenko <cryfall@gmail.com>
Date: Tue, 22 May 2018 14:12:15 +0300
Added MarshalAny() support
Diffstat:
2 files changed, 54 insertions(+), 1 deletion(-)
diff --git a/encode.go b/encode.go
@@ -1,9 +1,10 @@
package gojay
import (
- "fmt"
"io"
+ "fmt"
"reflect"
+ "encoding/json"
)
// MarshalJSONObject returns the JSON encoding of v.
@@ -97,6 +98,38 @@ func MarshalJSONArray(v MarshalerJSONArray) ([]byte, error) {
// fmt.Println(b) // {"id":123456}
// }
func Marshal(v interface{}) ([]byte, error) {
+ return marshal(v, false)
+}
+
+// MarshalAny returns the JSON encoding of v.
+//
+// MarshalAny takes interface v and encodes it according to its type.
+// Basic example with a string:
+// b, err := gojay.Marshal("test")
+// fmt.Println(b) // "test"
+//
+// If v implements Marshaler or Marshaler interface
+// it will call the corresponding methods.
+//
+// If it cannot find any supported type it will be marshalled though default Go "json" package.
+// Warning, this function can be slower, than a default "Marshal"
+//
+// type TestStruct struct {
+// id int
+// }
+//
+// func main() {
+// test := &TestStruct{
+// id: 123456,
+// }
+// b, _ := gojay.Marshal(test)
+// fmt.Println(b) // {"id": 123456}
+// }
+func MarshalAny(v interface{}) ([]byte, error) {
+ return marshal(v, true)
+}
+
+func marshal(v interface{}, any bool) ([]byte, error) {
switch vt := v.(type) {
case MarshalerJSONObject:
enc := BorrowEncoder(nil)
@@ -163,6 +196,10 @@ func Marshal(v interface{}) ([]byte, error) {
defer enc.Release()
return enc.encodeEmbeddedJSON(vt)
default:
+ if any {
+ return json.Marshal(vt)
+ }
+
return nil, InvalidMarshalError(fmt.Sprintf(invalidMarshalErrorMsg, reflect.TypeOf(vt).String()))
}
}
diff --git a/encode_object_test.go b/encode_object_test.go
@@ -274,6 +274,22 @@ func TestEncoderObjectMarshalAPI(t *testing.T) {
string(r),
"Result of marshalling is different as the one expected")
})
+ t.Run("marshal-any-object", func(t *testing.T) {
+ test := struct {
+ Foo string
+ Bar int
+ }{
+ "test",
+ 100,
+ }
+ r, err := MarshalAny(test)
+ assert.Nil(t, err, "Error should be nil")
+ assert.Equal(
+ t,
+ `{"Foo":"test","Bar":100}`,
+ string(r),
+ "Result of marshalling is different as the one expected")
+ })
}
type TestObectOmitEmpty struct {