MarshalJSON marshals the node by injecting the [Node.NodeType] as a nodeType field and the [NodeBase.NumChildren] as a numChildren field at the start of the standard JSON encoding output.
()
| 21 | // field and the [NodeBase.NumChildren] as a numChildren field at the start of |
| 22 | // the standard JSON encoding output. |
| 23 | func (n *NodeBase) MarshalJSON() ([]byte, error) { |
| 24 | // the non pointer value does not implement MarshalJSON, so it will not result in infinite recursion |
| 25 | b, err := json.Marshal(reflectx.Underlying(reflect.ValueOf(n.This)).Interface()) |
| 26 | if err != nil { |
| 27 | return b, err |
| 28 | } |
| 29 | data := `"nodeType":"` + n.NodeType().Name + `",` |
| 30 | if n.NumChildren() > 0 { |
| 31 | data += `"numChildren":` + strconv.Itoa(n.NumChildren()) + "," |
| 32 | } |
| 33 | b = slices.Insert(b, 1, []byte(data)...) |
| 34 | return b, nil |
| 35 | } |
| 36 | |
| 37 | // unmarshalTypeCache is a cache of [reflect.Type] values used |
| 38 | // for unmarshalling in [NodeBase.UnmarshalJSON]. This cache has |
no test coverage detected