diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-16 16:53:53 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-18 22:45:54 -0700 |
commit | 1a5b0561f53441d8a259a5096281699b5af16a6c (patch) | |
tree | aed7ab3089889582d7d817900d0796fb54acb0ab | |
parent | dfc67cecbd95344d296c31b537fa3ae8aec8c292 (diff) |
reencode: Add CompactFloats
-rw-r--r-- | ReleaseNotes.md | 9 | ||||
-rw-r--r-- | compat/json/compat_test.go | 3 | ||||
-rw-r--r-- | reencode.go | 10 | ||||
-rw-r--r-- | reencode_test.go | 6 |
4 files changed, 23 insertions, 5 deletions
diff --git a/ReleaseNotes.md b/ReleaseNotes.md index b1647da..ae147b1 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -4,11 +4,15 @@ User-facing changes: - - General bugfixes: + - General Changes: + Encoder, ReEncoder: Now correctly trims unnecessary the trailing '0's from the fraction-part when compacting numbers. + + ReEncoder: No longer compact floating-point numbers by + default, add a `CompactFloats` ReEncoderConfig option to + control this. + - Compatibility bugfixes: + compat/json.Valid: No longer considers truncated JSON @@ -17,6 +21,9 @@ + compat/json.Compact, compat/json.Indent: Don't write to the destination buffer if there is a syntax error. + + compat/json.Compact, compat/json.Indent: No longer compact + floating-point numbers; as `encoding/json` doesn't. + - Unicode: + Feature: Encoder, ReEncoder: Add an `InvalidUTF8` diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go index d989a4d..128bd1b 100644 --- a/compat/json/compat_test.go +++ b/compat/json/compat_test.go @@ -46,6 +46,7 @@ func TestCompatCompact(t *testing.T) { "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, "object": {In: `{}`, Out: `{}`}, "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, + "float": {In: `1.200e003`, Out: `1.200e003`}, } for tcName, tc := range testcases { tc := tc @@ -75,6 +76,7 @@ func TestCompatIndent(t *testing.T) { "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`}, "object": {In: `{}`, Out: `{}`}, "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""}, + "float": {In: `1.200e003`, Out: `1.200e003`}, } for tcName, tc := range testcases { tc := tc @@ -103,6 +105,7 @@ func TestCompatMarshal(t *testing.T) { testcases := map[string]testcase{ "non-utf8": {In: "\x85\xcd", Out: "\"\\ufffd\\ufffd\""}, "urc": {In: "\ufffd", Out: "\"\ufffd\""}, + "float": {In: 1.2e3, Out: `1200`}, } for tcName, tc := range testcases { tc := tc diff --git a/reencode.go b/reencode.go index 1a9999b..1943b9c 100644 --- a/reencode.go +++ b/reencode.go @@ -54,6 +54,10 @@ type ReEncoderConfig struct { // this is different than the usual behavior. ForceTrailingNewlines bool + // CompactFloats causes the *ReEncoder to trim unnecessary '0' + // digits from floating-point number values. + CompactFloats bool + // A JSON document is specified to be a sequence of Unicode // codepoints; InvalidUTF8 controls how the *ReEncoder behaves // when it encounters invalid UTF-8 bytes in a JSON string @@ -109,8 +113,10 @@ func NewReEncoder(out io.Writer, cfg ReEncoderConfig) *ReEncoder { } // Numbers - module = &reEncodeCompactNum{ - out: module, + if cfg.CompactFloats { + module = &reEncodeCompactNum{ + out: module, + } } // Strings diff --git a/reencode_test.go b/reencode_test.go index bc6d246..715e976 100644 --- a/reencode_test.go +++ b/reencode_test.go @@ -135,7 +135,8 @@ func TestReEncode(t *testing.T) { }, "numbers": { enc: ReEncoderConfig{ - Compact: true, + Compact: true, + CompactFloats: true, }, in: []any{ Number("1.200e003"), @@ -144,7 +145,8 @@ func TestReEncode(t *testing.T) { }, "numbers-zero": { enc: ReEncoderConfig{ - Compact: true, + Compact: true, + CompactFloats: true, }, in: []any{ Number("1.000e000"), |