diff options
Diffstat (limited to 'json.go')
-rw-r--r-- | json.go | 42 |
1 files changed, 18 insertions, 24 deletions
@@ -7,7 +7,7 @@ import ( ) type JSON struct { - Factory NodeFactory + Factory Factory } func (j *JSON) Unmarshal(input string) (Node, error) { @@ -25,61 +25,55 @@ func (j *JSON) MustUnmarshal(input string) Node { } func (j *JSON) Marshal(n Node) string { - return n.String() + return n.ToJSON() } func (j *JSON) Node(value any) (Node, error) { switch value := value.(type) { case string: - n, err := j.Factory(StringType) + n, err := j.Factory.Produce(StringType) if err != nil { return nil, err } - n.(StringNode).SetString(value) + j.Factory.Fill(n, value) return n, nil case float64: - n, err := j.Factory(NumberType) + n, err := j.Factory.Produce(NumberType) if err != nil { return nil, err } - n.(NumberNode).SetNumber(value) + j.Factory.Fill(n, value) return n, nil case int: - n, err := j.Factory(NumberType) + n, err := j.Factory.Produce(NumberType) if err != nil { return nil, err } - n.(NumberNode).SetNumber(float64(value)) + j.Factory.Fill(n, float64(value)) return n, nil case bool: - n, err := j.Factory(BooleanType) + n, err := j.Factory.Produce(BooleanType) if err != nil { return nil, err } - n.(BooleanNode).SetBool(value) + j.Factory.Fill(n, value) return n, nil case nil: - return j.Factory(NullType) + return j.Factory.Produce(NullType) case map[string]Node: - n, err := j.Factory(ObjectType) + n, err := j.Factory.Produce(ObjectType) if err != nil { return nil, err } - on := n.(ObjectNode) - for k, v := range value { - on.SetKeyValue(k, v) - } - return on, nil + j.Factory.Fill(n, value) + return n, nil case []Node: - n, err := j.Factory(ArrayType) + n, err := j.Factory.Produce(ArrayType) if err != nil { return nil, err } - an := n.(ArrayNode) - for _, v := range value { - an.Append(v) - } - return an, nil + j.Factory.Fill(n, value) + return n, nil default: return nil, fmt.Errorf("invalid type %t", value) } @@ -93,7 +87,7 @@ func (j *JSON) MustNode(value any) Node { return n } -func New(factory NodeFactory) *JSON { +func New(factory Factory) *JSON { return &JSON{ Factory: factory, } |