trpc.group/trpc-go/trpc-go@v1.0.3/codec/serialization_json.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package codec 15 16 import ( 17 jsoniter "github.com/json-iterator/go" 18 ) 19 20 // JSONAPI is used by tRPC JSON serialization when the object does 21 // not conform to protobuf proto.Message interface. 22 // 23 // Deprecated: This global variable is exportable due to backward comparability issue but 24 // should not be modified. If users want to change the default behavior of 25 // internal JSON serialization, please use register your customized serializer 26 // function like: 27 // 28 // codec.RegisterSerializer(codec.SerializationTypeJSON, yourOwnJSONSerializer) 29 var JSONAPI = jsoniter.ConfigCompatibleWithStandardLibrary 30 31 // JSONSerialization provides json serialization mode. 32 // golang official json package is implemented by reflection, 33 // and has very low performance. So trpc-go choose json-iterator package 34 // to implement json serialization. 35 type JSONSerialization struct{} 36 37 // Unmarshal deserializes the in bytes into body. 38 func (s *JSONSerialization) Unmarshal(in []byte, body interface{}) error { 39 return JSONAPI.Unmarshal(in, body) 40 } 41 42 // Marshal returns the serialized bytes in json protocol. 43 func (s *JSONSerialization) Marshal(body interface{}) ([]byte, error) { 44 return JSONAPI.Marshal(body) 45 }