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)
| 125 | // a root node that you know is already of the correct type, you can just |
| 126 | // call [NodeBase.UnmarshalJSON] on it instead. |
| 127 | func 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 | } |