How to add type field automatically for every serialized JSON in Go?
For example, in this code:
type Literal struct { Value interface{} `json:'value'` Raw string `json:'raw'` } type BinaryExpression struct { Operator string `json:"operator"` Right Literal `json:"right"` Left Literal `json:"left"` } os.Stdout.Write(json.Marshal(&BinaryExpression{ ... }))
Instead of creating something like:
{ "operator": "*", "left": { "value": 6, "raw": "6" }, "right": { "value": 7, "raw": "7" } }
I would like to generate this:
{ "type": "BinaryExpression", "operator": "*", "left": { "type": "Literal", "value": 6, "raw": "6" }, "right": { "type": "Literal", "value": 7, "raw": "7" } }
source share