Support encoding json.Number type (#923)
Co-authored-by: Thomas Pelletier <thomas@pelletier.codes>
This commit is contained in:
@@ -565,10 +565,11 @@ complete solutions exist out there.
|
||||
|
||||
## Versioning
|
||||
|
||||
Go-toml follows [Semantic Versioning](https://semver.org). The supported version
|
||||
of [TOML](https://github.com/toml-lang/toml) is indicated at the beginning of
|
||||
this document. The last two major versions of Go are supported
|
||||
(see [Go Release Policy](https://golang.org/doc/devel/release.html#policy)).
|
||||
Expect for parts explicitely marked otherwise, go-toml follows [Semantic
|
||||
Versioning](https://semver.org). The supported version of
|
||||
[TOML](https://github.com/toml-lang/toml) is indicated at the beginning of this
|
||||
document. The last two major versions of Go are supported (see [Go Release
|
||||
Policy](https://golang.org/doc/devel/release.html#policy)).
|
||||
|
||||
## License
|
||||
|
||||
|
||||
+12
-1
@@ -19,6 +19,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
@@ -33,7 +34,11 @@ Reading from a file:
|
||||
jsontoml file.json > file.toml
|
||||
`
|
||||
|
||||
var useJsonNumber bool
|
||||
|
||||
func main() {
|
||||
flag.BoolVar(&useJsonNumber, "use-json-number", false, "unmarshal numbers into `json.Number` type instead of as `float64`")
|
||||
|
||||
p := cli.Program{
|
||||
Usage: usage,
|
||||
Fn: convert,
|
||||
@@ -45,11 +50,17 @@ func convert(r io.Reader, w io.Writer) error {
|
||||
var v interface{}
|
||||
|
||||
d := json.NewDecoder(r)
|
||||
e := toml.NewEncoder(w)
|
||||
|
||||
if useJsonNumber {
|
||||
d.UseNumber()
|
||||
e.SetMarshalJsonNumbers(true)
|
||||
}
|
||||
|
||||
err := d.Decode(&v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e := toml.NewEncoder(w)
|
||||
return e.Encode(v)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ func TestConvert(t *testing.T) {
|
||||
input string
|
||||
expected string
|
||||
errors bool
|
||||
useJsonNumber bool
|
||||
}{
|
||||
{
|
||||
name: "valid json",
|
||||
@@ -26,6 +27,19 @@ func TestConvert(t *testing.T) {
|
||||
}`,
|
||||
expected: `[mytoml]
|
||||
a = 42.0
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "use json number",
|
||||
useJsonNumber: true,
|
||||
input: `
|
||||
{
|
||||
"mytoml": {
|
||||
"a": 42
|
||||
}
|
||||
}`,
|
||||
expected: `[mytoml]
|
||||
a = 42
|
||||
`,
|
||||
},
|
||||
{
|
||||
@@ -37,6 +51,7 @@ a = 42.0
|
||||
|
||||
for _, e := range examples {
|
||||
b := new(bytes.Buffer)
|
||||
useJsonNumber = e.useJsonNumber
|
||||
err := convert(strings.NewReader(e.input), b)
|
||||
if e.errors {
|
||||
require.Error(t, err)
|
||||
|
||||
@@ -3,6 +3,7 @@ package toml
|
||||
import (
|
||||
"bytes"
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
@@ -41,6 +42,7 @@ type Encoder struct {
|
||||
arraysMultiline bool
|
||||
indentSymbol string
|
||||
indentTables bool
|
||||
marshalJsonNumbers bool
|
||||
}
|
||||
|
||||
// NewEncoder returns a new Encoder that writes to w.
|
||||
@@ -87,6 +89,17 @@ func (enc *Encoder) SetIndentTables(indent bool) *Encoder {
|
||||
return enc
|
||||
}
|
||||
|
||||
// SetMarshalJsonNumbers forces the encoder to serialize `json.Number` as a
|
||||
// float or integer instead of relying on TextMarshaler to emit a string.
|
||||
//
|
||||
// *Unstable:* This method does not follow the compatiblity guarnatees of
|
||||
// semver. It can be changed or removed without a new major version being
|
||||
// issued.
|
||||
func (enc *Encoder) SetMarshalJsonNumbers(indent bool) *Encoder {
|
||||
enc.marshalJsonNumbers = indent
|
||||
return enc
|
||||
}
|
||||
|
||||
// Encode writes a TOML representation of v to the stream.
|
||||
//
|
||||
// If v cannot be represented to TOML it returns an error.
|
||||
@@ -252,6 +265,18 @@ func (enc *Encoder) encode(b []byte, ctx encoderCtx, v reflect.Value) ([]byte, e
|
||||
return append(b, x.String()...), nil
|
||||
case LocalDateTime:
|
||||
return append(b, x.String()...), nil
|
||||
case json.Number:
|
||||
if enc.marshalJsonNumbers {
|
||||
if x == "" { /// Useful zero value.
|
||||
return append(b, "0"...), nil
|
||||
} else if v, err := x.Int64(); err == nil {
|
||||
return enc.encode(b, ctx, reflect.ValueOf(v))
|
||||
} else if f, err := x.Float64(); err == nil {
|
||||
return enc.encode(b, ctx, reflect.ValueOf(f))
|
||||
} else {
|
||||
return nil, fmt.Errorf("toml: unable to convert %q to int64 or float64", x)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hasTextMarshaler := v.Type().Implements(textMarshalerType)
|
||||
|
||||
@@ -948,6 +948,29 @@ func TestEncoderSetIndentSymbol(t *testing.T) {
|
||||
assert.Equal(t, expected, w.String())
|
||||
}
|
||||
|
||||
func TestEncoderSetMarshalJsonNumbers(t *testing.T) {
|
||||
var w strings.Builder
|
||||
enc := toml.NewEncoder(&w)
|
||||
enc.SetMarshalJsonNumbers(true)
|
||||
err := enc.Encode(map[string]interface{}{
|
||||
"A": json.Number("1.1"),
|
||||
"B": json.Number("42e-3"),
|
||||
"C": json.Number("42"),
|
||||
"D": json.Number("0"),
|
||||
"E": json.Number("0.0"),
|
||||
"F": json.Number(""),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
expected := `A = 1.1
|
||||
B = 0.042
|
||||
C = 42
|
||||
D = 0
|
||||
E = 0.0
|
||||
F = 0
|
||||
`
|
||||
assert.Equal(t, expected, w.String())
|
||||
}
|
||||
|
||||
func TestEncoderOmitempty(t *testing.T) {
|
||||
type doc struct {
|
||||
String string `toml:",omitempty,multiline"`
|
||||
|
||||
Reference in New Issue
Block a user