summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-14 11:02:33 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-18 22:45:54 -0700
commiteaaf7bc29d43b4470623c75e6e409a049b3083af (patch)
tree8acf0044d5bebef4f5d73ccd2e2cecc28d2b230d
parent218edcc3878394a6942d4f72e3be99137c22825a (diff)
compat/json: Valid: Check for EOF
-rw-r--r--ReleaseNotes.md5
-rw-r--r--compat/json/compat.go9
-rw-r--r--compat/json/compat_test.go34
3 files changed, 46 insertions, 2 deletions
diff --git a/ReleaseNotes.md b/ReleaseNotes.md
index 623d5da..613ea0c 100644
--- a/ReleaseNotes.md
+++ b/ReleaseNotes.md
@@ -9,6 +9,11 @@
+ Encoder, ReEncoder: Now correctly trims unnecessary the
trailing '0's from the fraction-part when compacting numbers.
+ - Compatibility bugfixes:
+
+ + compat/json.Valid: No longer considers truncated JSON
+ documents to be valid.
+
# v0.3.6 (2023-02-16)
Theme: Architectural improvements
diff --git a/compat/json/compat.go b/compat/json/compat.go
index c96470d..300ab2f 100644
--- a/compat/json/compat.go
+++ b/compat/json/compat.go
@@ -175,8 +175,13 @@ func Valid(data []byte) bool {
formatter := lowmemjson.NewReEncoder(io.Discard, lowmemjson.ReEncoderConfig{
Compact: true,
})
- _, err := formatter.Write(data)
- return err == nil
+ if _, err := formatter.Write(data); err != nil {
+ return false
+ }
+ if err := formatter.Close(); err != nil {
+ return false
+ }
+ return true
}
// Decode wrappers ///////////////////////////////////////////////////
diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go
new file mode 100644
index 0000000..5c8f3ee
--- /dev/null
+++ b/compat/json/compat_test.go
@@ -0,0 +1,34 @@
+// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package json
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestCompatValid(t *testing.T) {
+ t.Parallel()
+ type testcase struct {
+ In string
+ Exp bool
+ }
+ testcases := map[string]testcase{
+ "empty": {In: ``, Exp: false},
+ "num": {In: `1`, Exp: true},
+ "trunc": {In: `{`, Exp: false},
+ "object": {In: `{}`, Exp: true},
+ }
+ for tcName, tc := range testcases {
+ tc := tc
+ t.Run(tcName, func(t *testing.T) {
+ t.Parallel()
+ t.Logf("in=%q", tc.In)
+ act := Valid([]byte(tc.In))
+ assert.Equal(t, tc.Exp, act)
+ })
+ }
+}