We’ve abstracted the Codec interface to unify the serialization/deserialization logic for processing requests, and you can implement your own Codec to support more formats. The specific source code is inencoding

These formats are battery-included.

  • json
  • protobuf
  • xml
  • yaml

Interface

You should implement the following Codec interface for your custom codec.

  1. // Codec interface is for serialization and deserialization, notice that these methods must be thread-safe.
  2. type Codec interface {
  3. Marshal(v interface{}) ([]byte, error)
  4. Unmarshal(data []byte, v interface{}) error
  5. Name() string
  6. }

Example of Codec Implementation

You may refer to the included implementations in kratos, such as json when you implementing custom Codec.

  1. // https://github.com/go-kratos/kratos/blob/main/encoding/json/json.go
  2. package json
  3. import (
  4. "encoding/json"
  5. "reflect"
  6. "github.com/go-kratos/kratos/v2/encoding"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. "google.golang.org/protobuf/proto"
  9. )
  10. // Name is the name registered for the json codec.
  11. const Name = "json"
  12. var (
  13. // MarshalOptions is a configurable JSON format marshaller.
  14. MarshalOptions = protojson.MarshalOptions{
  15. EmitUnpopulated: true,
  16. }
  17. // UnmarshalOptions is a configurable JSON format parser.
  18. UnmarshalOptions = protojson.UnmarshalOptions{
  19. DiscardUnknown: true,
  20. }
  21. )
  22. func init() {
  23. encoding.RegisterCodec(codec{})
  24. }
  25. // codec is a Codec implementation with json.
  26. type codec struct{}
  27. func (codec) Marshal(v interface{}) ([]byte, error) {
  28. if m, ok := v.(proto.Message); ok {
  29. return MarshalOptions.Marshal(m)
  30. }
  31. return json.Marshal(v)
  32. }
  33. func (codec) Unmarshal(data []byte, v interface{}) error {
  34. rv := reflect.ValueOf(v)
  35. for rv.Kind() == reflect.Ptr {
  36. if rv.IsNil() {
  37. rv.Set(reflect.New(rv.Type().Elem()))
  38. }
  39. rv = rv.Elem()
  40. }
  41. if m, ok := v.(proto.Message); ok {
  42. return UnmarshalOptions.Unmarshal(data, m)
  43. } else if m, ok := reflect.Indirect(reflect.ValueOf(v)).Interface().(proto.Message); ok {
  44. return UnmarshalOptions.Unmarshal(data, m)
  45. }
  46. return json.Unmarshal(data, v)
  47. }
  48. func (codec) Name() string {
  49. return Name
  50. }

Usage

Register Custom Codec

  1. encoding.RegisterCodec(codec{})

Get the Codec

  1. jsonCodec := encoding.GetCodec("json")

Serialization

  1. // You should manually import this package if you use it directly: import _ "github.com/go-kratos/kratos/v2/encoding/json"
  2. jsonCode := encoding.GetCodec("json")
  3. type user struct {
  4. Name string
  5. Age string
  6. state bool
  7. }
  8. u := &user{
  9. Name: "kratos",
  10. Age: "2",
  11. state: false,
  12. }
  13. bytes, _ := jsonCode.Marshal(u)
  14. // output {"Name":"kratos","Age":"2"}

Deserialization

  1. // You should manually import this package if you use it directly:import _ "github.com/go-kratos/kratos/v2/encoding/json"
  2. jsonCode := encoding.GetCodec("json")
  3. type user struct {
  4. Name string
  5. Age string
  6. state bool
  7. }
  8. u := &user{}
  9. jsonCode.Unmarshal([]byte(`{"Name":"kratos","Age":"2"}`), &u)
  10. //output &{kratos 2 false}