MCPcopy Create free account
hub / github.com/cogentcore/core / UnmarshalRootJSON

Function UnmarshalRootJSON

tree/json.go:127–143  ·  view source on GitHub ↗

UnmarshalRootJSON loads the given JSON to produce a new root node of the correct type with all properties and children loaded. If you have a root node that you know is already of the correct type, you can just call [NodeBase.UnmarshalJSON] on it instead.

(b []byte)

Source from the content-addressed store, hash-verified

125// a root node that you know is already of the correct type, you can just
126// call [NodeBase.UnmarshalJSON] on it instead.
127func UnmarshalRootJSON(b []byte) (Node, error) {
128 // we must make a temporary parent so that the type of the node can be updated
129 parent := New[NodeBase]()
130 // this NodeBase type is just temporary and will be fixed by [NodeBase.UnmarshalJSON]
131 nb := New[NodeBase](parent)
132 err := nb.UnmarshalJSON(b)
133 if err != nil {
134 return nil, err
135 }
136 // the node must be fetched from the parent's children since the pointer may have changed
137 n := parent.Child(0)
138 // we must safely remove the node from its temporary parent
139 n.AsTree().Parent = nil
140 parent.Children = nil
141 parent.Destroy()
142 return n, nil
143}

Callers 3

syncNodesFromMimeDataMethod · 0.92
nodesFromMimeDataMethod · 0.92
TestNodeRootJSONFunction · 0.85

Calls 4

ChildMethod · 0.80
AsTreeMethod · 0.65
DestroyMethod · 0.65
UnmarshalJSONMethod · 0.45

Tested by 1

TestNodeRootJSONFunction · 0.68