2023-04-12 09:04:02 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-04-13 05:40:41 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-04-12 09:04:02 +00:00
|
|
|
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
)
|
|
|
|
|
2023-04-13 05:40:41 +00:00
|
|
|
func (o *CandidateNode) setScalarFromJson(value interface{}) error {
|
|
|
|
o.Kind = ScalarNode
|
|
|
|
switch rawData := value.(type) {
|
|
|
|
case nil:
|
|
|
|
o.Tag = "!!null"
|
|
|
|
o.Value = "null"
|
|
|
|
case float64, float32:
|
|
|
|
o.Value = fmt.Sprintf("%v", value)
|
|
|
|
o.Tag = "!!float"
|
|
|
|
// json decoder returns ints as float.
|
|
|
|
if value == float64(int(rawData.(float64))) {
|
|
|
|
// aha it's an int disguised as a float
|
|
|
|
o.Tag = "!!int"
|
|
|
|
}
|
|
|
|
case int, int64, int32:
|
|
|
|
o.Value = fmt.Sprintf("%v", value)
|
|
|
|
o.Tag = "!!int"
|
|
|
|
case bool:
|
|
|
|
o.Value = fmt.Sprintf("%v", value)
|
|
|
|
o.Tag = "!!bool"
|
|
|
|
case string:
|
|
|
|
o.Value = rawData
|
|
|
|
o.Tag = "!!str"
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unrecognised type :( %v", rawData)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *CandidateNode) UnmarshalJSON(data []byte) error {
|
|
|
|
log.Debug("UnmarshalJSON")
|
|
|
|
switch data[0] {
|
|
|
|
case '{':
|
|
|
|
log.Debug("UnmarshalJSON - its a map!")
|
|
|
|
// its a map
|
|
|
|
o.Kind = MappingNode
|
|
|
|
o.Tag = "!!map"
|
|
|
|
|
|
|
|
dec := json.NewDecoder(bytes.NewReader(data))
|
|
|
|
_, err := dec.Token() // open object
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// cycle through k/v
|
|
|
|
var tok json.Token
|
|
|
|
for tok, err = dec.Token(); err == nil; tok, err = dec.Token() {
|
|
|
|
// we can expect two types: string or Delim. Delim automatically means
|
|
|
|
// that it is the closing bracket of the object, whereas string means
|
|
|
|
// that there is another key.
|
|
|
|
if _, ok := tok.(json.Delim); ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
childKey := o.CreateChild()
|
|
|
|
childKey.IsMapKey = true
|
|
|
|
childKey.Value = tok.(string)
|
|
|
|
childKey.Kind = ScalarNode
|
|
|
|
childKey.Tag = "!!str"
|
|
|
|
|
|
|
|
childValue := o.CreateChild()
|
|
|
|
childValue.Key = childKey
|
|
|
|
|
|
|
|
if err := dec.Decode(childValue); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Content = append(o.Content, childKey, childValue)
|
|
|
|
}
|
|
|
|
// unexpected error
|
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case '[':
|
2023-05-04 09:22:33 +00:00
|
|
|
o.Kind = SequenceNode
|
|
|
|
o.Tag = "!!seq"
|
2023-04-13 05:40:41 +00:00
|
|
|
log.Debug("UnmarshalJSON - its an array!")
|
|
|
|
var children []*CandidateNode
|
|
|
|
if err := json.Unmarshal(data, &children); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// now we put the children into the content, and set a key value for them
|
|
|
|
for i, child := range children {
|
2023-05-04 09:27:37 +00:00
|
|
|
|
|
|
|
if child == nil {
|
|
|
|
// need to represent it as a null scalar
|
|
|
|
child = createScalarNode(nil, "null")
|
|
|
|
}
|
2023-04-13 05:40:41 +00:00
|
|
|
childKey := o.CreateChild()
|
|
|
|
childKey.Kind = ScalarNode
|
|
|
|
childKey.Tag = "!!int"
|
|
|
|
childKey.Value = fmt.Sprintf("%v", i)
|
2023-04-17 07:23:48 +00:00
|
|
|
childKey.IsMapKey = true
|
2023-04-13 05:40:41 +00:00
|
|
|
|
|
|
|
child.Parent = o
|
|
|
|
child.Key = childKey
|
2023-05-04 09:22:33 +00:00
|
|
|
o.Content = append(o.Content, child)
|
2023-04-13 05:40:41 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Debug("UnmarshalJSON - its a scalar!")
|
|
|
|
// otherwise, must be a scalar
|
|
|
|
var scalar interface{}
|
|
|
|
err := json.Unmarshal(data, &scalar)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug("UnmarshalJSON - scalar is %v", scalar)
|
|
|
|
|
|
|
|
return o.setScalarFromJson(scalar)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-12 09:04:02 +00:00
|
|
|
func (o *CandidateNode) MarshalJSON() ([]byte, error) {
|
2023-04-13 04:34:34 +00:00
|
|
|
log.Debugf("MarshalJSON %v", NodeToString(o))
|
2023-04-12 09:04:02 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
enc.SetEscapeHTML(false) // do not escape html chars e.g. &, <, >
|
|
|
|
|
|
|
|
switch o.Kind {
|
|
|
|
case DocumentNode:
|
2023-04-13 04:34:34 +00:00
|
|
|
log.Debugf("MarshalJSON DocumentNode")
|
2023-04-12 09:04:02 +00:00
|
|
|
err := enc.Encode(o.Content[0])
|
|
|
|
return buf.Bytes(), err
|
|
|
|
case AliasNode:
|
2023-04-13 04:34:34 +00:00
|
|
|
log.Debugf("MarshalJSON AliasNode")
|
2023-04-12 09:04:02 +00:00
|
|
|
err := enc.Encode(o.Alias)
|
|
|
|
return buf.Bytes(), err
|
|
|
|
case ScalarNode:
|
2023-04-13 04:34:34 +00:00
|
|
|
log.Debugf("MarshalJSON ScalarNode")
|
2023-04-12 09:04:02 +00:00
|
|
|
value, err := o.GetValueRep()
|
|
|
|
if err != nil {
|
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
|
|
|
err = enc.Encode(value)
|
|
|
|
return buf.Bytes(), err
|
|
|
|
case MappingNode:
|
2023-04-13 04:34:34 +00:00
|
|
|
log.Debugf("MarshalJSON MappingNode")
|
2023-04-12 09:04:02 +00:00
|
|
|
buf.WriteByte('{')
|
|
|
|
for i := 0; i < len(o.Content); i += 2 {
|
|
|
|
if err := enc.Encode(o.Content[i].Value); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buf.WriteByte(':')
|
|
|
|
if err := enc.Encode(o.Content[i+1]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if i != len(o.Content)-2 {
|
|
|
|
buf.WriteByte(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.WriteByte('}')
|
2023-04-13 04:53:14 +00:00
|
|
|
return buf.Bytes(), nil
|
2023-04-12 09:04:02 +00:00
|
|
|
case SequenceNode:
|
2023-04-13 04:34:34 +00:00
|
|
|
log.Debugf("MarshalJSON SequenceNode")
|
2023-04-12 09:04:02 +00:00
|
|
|
err := enc.Encode(o.Content)
|
|
|
|
return buf.Bytes(), err
|
2023-04-13 04:53:14 +00:00
|
|
|
default:
|
|
|
|
err := enc.Encode(nil)
|
|
|
|
return buf.Bytes(), err
|
2023-04-12 09:04:02 +00:00
|
|
|
}
|
|
|
|
}
|