JSON Marshaling
encoding/json in both directions: tags that rename or drop fields, Decoder and Encoder streaming, custom MarshalJSON, RawMessage. It is the standard-library package Go interviews probe most.
part ofGo (Golang)overview, primer and where to startread it →on this pageshowhide
explore
- Evolving a Wire Struct4 questions
- Struct Tags and Mapping5 questions
- Types on the Wire5 questions
- Streaming Encoders and Decoders4 questions
- Custom Marshalers and RawMessage5 questions
- Nulls, Omitted and Defaults5 questions
- Stricter v2 Decoding5 questions
questions
page 2 of 2In a json struct tag, what does the ,string option do to the field's encoded value?
basics
~20 sThe ,string option makes encoding/json write the field's value inside a JSON string, so an int64 holding 63 encodes as "63" rather than 63, and requires a quoted value when decoding. It applies only to string, integer, floating-point and boolean fields.
When may a JSON response field be removed if some clients can never be upgraded?
basics
~20 sOnly when evidence, not a code search, says nothing in the wild still reads it: traffic by client version measured against the release that stopped needing it. Until then keep emitting the key, and decide by what emitting it forces you to keep alive.
When should a fleet move from encoding/json to encoding/json/v2, and who owns the traffic it starts rejecting?
basics
~20 sAdopt it where strictness is the product -- inbound trust boundaries -- and leave interior and round-tripping code alone. It is an import change, so it stages per service. The team facing the sender owns the rejections, so they hold a veto and a documented per-call loosening option.
showing 31–33 of 33