2023-04-12 09:04:02 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2023-04-13 04:53:14 +00:00
|
|
|
log.Debugf("cool I handled that")
|
2023-04-12 09:04:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|