Skip to content

Commit

Permalink
docs: add comments informing that global variable JSONAPI should not …
Browse files Browse the repository at this point in the history
…be modified (trpc-group#157)
  • Loading branch information
Andrew-M-C committed Jan 20, 2024
1 parent 65a60b1 commit ae69f71
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
11 changes: 9 additions & 2 deletions codec/serialization_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ import (
jsoniter "github.com/json-iterator/go"
)

// JSONAPI is json packing and unpacking object, users can change
// the internal parameter.
// Deprecated: JSONAPI is used by tRPC JSON serialization when the object does
// not conform to protobuf proto.Message interface.
//
// This global variable is exportable due to backward comparability issue but
// should not be modified. If users want to change the default behavior of
// internal JSON serialization, please use register your customized serializer
// function like:
//
// codec.RegisterSerializer(codec.SerializationTypeJSON, yourOwnJSONSerializer)
var JSONAPI = jsoniter.ConfigCompatibleWithStandardLibrary

// JSONSerialization provides json serialization mode.
Expand Down
4 changes: 3 additions & 1 deletion codec/serialization_jsonpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ var Marshaler = protojson.MarshalOptions{EmitUnpopulated: true, UseProtoNames: t
var Unmarshaler = protojson.UnmarshalOptions{DiscardUnknown: false}

// JSONPBSerialization provides jsonpb serialization mode. It is based on
// protobuf/jsonpb.
// protobuf/jsonpb. This serializer will firstly try jsonpb's serialization. If
// object does not conform to protobuf proto.Message interface, json-iterator
// will be used.
type JSONPBSerialization struct{}

// Unmarshal deserialize the in bytes into body.
Expand Down
13 changes: 12 additions & 1 deletion restful/serialize_jsonpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,23 @@ func init() {

// JSONPBSerializer is used for content-Type: application/json.
// It's based on google.golang.org/protobuf/encoding/protojson.
//
// This serializer will firstly try jsonpb's serialization. If object does not
// conform to protobuf proto.Message interface, the serialization will switch to
// json-iterator.
type JSONPBSerializer struct {
AllowUnmarshalNil bool // allow unmarshalling nil body
}

// JSONAPI is a copy of jsoniter.ConfigCompatibleWithStandardLibrary.
// Deprecated: JSONAPI is a copy of jsoniter.ConfigCompatibleWithStandardLibrary.
// github.com/json-iterator/go is faster than Go's standard json library.
//
// This global variable is exportable due to backward comparability issue but
// should not be modified. If users want to change the default behavior of
// internal JSON serialization, please use register your customized serializer
// function like:
//
// restful.RegisterSerializer(yourOwnJSONSerializer)
var JSONAPI = jsoniter.ConfigCompatibleWithStandardLibrary

// Marshaller is a configurable protojson marshaler.
Expand Down

0 comments on commit ae69f71

Please sign in to comment.