Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/json
diff options
context:
space:
mode:
Diffstat (limited to 'json')
-rw-r--r--json/read.go2
-rw-r--r--json/write.go4
-rw-r--r--json/write_test.go57
3 files changed, 61 insertions, 2 deletions
diff --git a/json/read.go b/json/read.go
index f3a0a65..91589cf 100644
--- a/json/read.go
+++ b/json/read.go
@@ -29,7 +29,9 @@ const (
type JSONReaderState int
const (
+ // Immediately before a value
JSONReaderStateValue JSONReaderState = iota
+ // Immediate after a value
JSONReaderStateValueEnd
)
diff --git a/json/write.go b/json/write.go
index c2a220e..3a5a621 100644
--- a/json/write.go
+++ b/json/write.go
@@ -209,7 +209,7 @@ func (writer *JSONWriter) navigateTo(keepLen int, path []walk.PathSegment, state
}
func (writer *JSONWriter) inMapAt(keepLen int, path []walk.PathSegment) bool {
- if keepLen < len(path) {
+ if len(path) != 0 {
return false
}
@@ -222,7 +222,7 @@ func (writer *JSONWriter) inMapAt(keepLen int, path []walk.PathSegment) bool {
}
func (writer *JSONWriter) inArrayAt(keepLen int, path []walk.PathSegment) bool {
- if keepLen < len(path) {
+ if len(path) != 0 {
return false
}
diff --git a/json/write_test.go b/json/write_test.go
index 05b228e..b495d94 100644
--- a/json/write_test.go
+++ b/json/write_test.go
@@ -222,6 +222,63 @@ func TestWrite(t *testing.T) {
},
expected: `[[100],[],null,[200]]`,
},
+ {
+ values: []walk.Value {
+ walk.MapValue {{
+ Key: "a",
+ Value: walk.MapValue {{
+ Key: "b",
+ Value: walk.StringValue("map"),
+ }},
+ }},
+ walk.MapValue {{
+ Key: "a",
+ Value: walk.ArrayValue {{
+ Index: 0,
+ Value: walk.StringValue("array"),
+ }},
+ }},
+ },
+ expected: `{"a":{"b":"map"},"a":["array"]}`,
+ },
+ {
+ values: []walk.Value {
+ walk.ArrayValue {{
+ Index: 0,
+ Value: walk.ArrayValue {{
+ Index: 0,
+ Value: walk.ArrayValue {{
+ Index: 1,
+ Value: walk.StringValue("a"),
+ }},
+ }},
+ }},
+ walk.ArrayValue {{
+ Index: 0,
+ Value: walk.ArrayValue {{
+ Index: 1,
+ Value: walk.ArrayValue{},
+ }},
+ }},
+ },
+ expected: `[[["a"],[]]]`,
+ },
+ {
+ values: []walk.Value {
+ walk.ArrayValue {{
+ Index: 0,
+ Value: walk.MapValue {{
+ Key: "a",
+ Value: walk.StringValue("a"),
+ }},
+ }},
+ walk.ArrayValue {{
+ Index: 0,
+ Value: walk.MapValue{},
+ }},
+ },
+ expected: `[{"a":"a"}]`,
+ },
}
for i, test := range tests {