5
Come aggiungere automaticamente un campo type
a ogni JSON serializzato in Go?Come aggiungere automaticamente un campo tipo a JSON in Go?
Ad esempio, in questo codice:
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{ ... }))
Invece di generare qualcosa come:
{
"operator": "*",
"left": {
"value": 6,
"raw": "6"
},
"right": {
"value": 7,
"raw": "7"
}
}
vorrei generare questo:
{
"type": "BinaryExpression",
"operator": "*",
"left": {
"type": "Literal",
"value": 6,
"raw": "6"
},
"right": {
"type": "Literal",
"value": 7,
"raw": "7"
}
}
C'è un modo per farlo sovrascrivendo 'MarshalJSON' una volta (forse per' interface {} 'o qualcosa), invece che per ogni struct? –