trpc.group/trpc-go/trpc-go@v1.0.2/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 json packing and unpacking object, users can change
    21  // the internal parameter.
    22  var JSONAPI = jsoniter.ConfigCompatibleWithStandardLibrary
    23  
    24  // JSONSerialization provides json serialization mode.
    25  // golang official json package is implemented by reflection,
    26  // and has very low performance. So trpc-go choose json-iterator package
    27  // to implement json serialization.
    28  type JSONSerialization struct{}
    29  
    30  // Unmarshal deserializes the in bytes into body.
    31  func (s *JSONSerialization) Unmarshal(in []byte, body interface{}) error {
    32  	return JSONAPI.Unmarshal(in, body)
    33  }
    34  
    35  // Marshal returns the serialized bytes in json protocol.
    36  func (s *JSONSerialization) Marshal(body interface{}) ([]byte, error) {
    37  	return JSONAPI.Marshal(body)
    38  }