mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
wip json encoding
This commit is contained in:
44
pkg/yqlib/encoder.go
Normal file
44
pkg/yqlib/encoder.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package yqlib
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
yaml "gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Encoder interface {
|
||||
Encode(node *yaml.Node) error
|
||||
}
|
||||
|
||||
type yamlEncoder struct {
|
||||
encoder *yaml.Encoder
|
||||
}
|
||||
|
||||
func NewYamlEncoder(destination io.Writer) Encoder {
|
||||
var encoder = yaml.NewEncoder(destination)
|
||||
encoder.SetIndent(2)
|
||||
return &yamlEncoder{encoder}
|
||||
}
|
||||
|
||||
func (ye *yamlEncoder) Encode(node *yaml.Node) error {
|
||||
return ye.encoder.Encode(node)
|
||||
}
|
||||
|
||||
type jsonEncoder struct {
|
||||
encoder *json.Encoder
|
||||
}
|
||||
|
||||
func NewJsonEncoder(destination io.Writer) Encoder {
|
||||
var encoder = json.NewEncoder(destination)
|
||||
return &jsonEncoder{encoder}
|
||||
}
|
||||
|
||||
func (je *jsonEncoder) Encode(node *yaml.Node) error {
|
||||
var dataBucket interface{}
|
||||
errorDecoding := node.Decode(&dataBucket)
|
||||
if errorDecoding != nil {
|
||||
return errorDecoding
|
||||
}
|
||||
return je.encoder.Encode(dataBucket)
|
||||
}
|
||||
@@ -14,7 +14,7 @@ var parseValueTests = []struct {
|
||||
testDescription string
|
||||
}{
|
||||
{"true", "", "!!bool", "boolean"},
|
||||
{"true", "!!string", "!!string", "boolean forced as string"},
|
||||
{"true", "!!str", "!!str", "boolean forced as string"},
|
||||
{"3.4", "", "!!float", "float"},
|
||||
{"1212121", "", "!!int", "big number"},
|
||||
{"1212121.1", "", "!!float", "big float number"},
|
||||
|
||||
Reference in New Issue
Block a user