NewCodec creates a new json codec
(opts ...Option)
| 26 | |
| 27 | // NewCodec creates a new json codec |
| 28 | func NewCodec(opts ...Option) Codec { |
| 29 | var ( |
| 30 | codec Codec |
| 31 | marshalOptions = protojson.MarshalOptions{ |
| 32 | EmitUnpopulated: true, |
| 33 | } |
| 34 | unmarshalOptions = protojson.UnmarshalOptions{ |
| 35 | DiscardUnknown: true, |
| 36 | } |
| 37 | ) |
| 38 | codec.marshalOptions = marshalOptions |
| 39 | codec.unmarshalOptions = unmarshalOptions |
| 40 | for _, f := range opts { |
| 41 | f(&codec) |
| 42 | } |
| 43 | return codec |
| 44 | } |
| 45 | |
| 46 | // WithIndent allows the codec to indent json output while marshalling. It is |
| 47 | // useful when the json output is meant for humans to read. |
no outgoing calls