aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2022-04-19 20:40:40 +0200
committerGitHub <noreply@github.com>2022-04-19 20:40:40 +0200
commite8c0be23f2b97c021f372c83f71c8ec2d2583d6c (patch)
treeee52c88b5faa406e61ae0a3c5c193fb62412cb35
parent29b2c0476698d0f4b240e87945cfa278da82b57a (diff)
parenta30b9b17b3a91bc856a037c1e1025e389a8524b3 (diff)
Merge pull request #1737 from Kelimion/fix_json_unmarshal
[json/unmarshal] Fix quoted strings.
-rw-r--r--core/encoding/json/parser.odin6
-rw-r--r--tests/core/encoding/json/test_core_json.odin20
2 files changed, 17 insertions, 9 deletions
diff --git a/core/encoding/json/parser.odin b/core/encoding/json/parser.odin
index c682ec9bd..7bf88c565 100644
--- a/core/encoding/json/parser.odin
+++ b/core/encoding/json/parser.odin
@@ -354,6 +354,12 @@ unquote_string :: proc(token: Token, spec: Specification, allocator := context.a
b := bytes_make(len(s) + 2*utf8.UTF_MAX, 1, allocator) or_return
w := copy(b, s[0:i])
+
+ if len(b) == 0 && allocator.data == nil {
+ // `unmarshal_count_array` calls us with a nil allocator
+ return string(b[:w]), nil
+ }
+
loop: for i < len(s) {
c := s[i]
switch {
diff --git a/tests/core/encoding/json/test_core_json.odin b/tests/core/encoding/json/test_core_json.odin
index c83710352..0e6a6412f 100644
--- a/tests/core/encoding/json/test_core_json.odin
+++ b/tests/core/encoding/json/test_core_json.odin
@@ -71,7 +71,8 @@ parse_json :: proc(t: ^testing.T) {
_, err := json.parse(transmute([]u8)json_data)
- expect(t, err == .None, "expected json error to be none")
+ msg := fmt.tprintf("Expected `json.parse` to return nil, got %v", err)
+ expect(t, err == nil, msg)
}
@test
@@ -88,8 +89,8 @@ marshal_json :: proc(t: ^testing.T) {
}
_, err := json.marshal(my_struct)
-
- expect(t, err == nil, "expected json error to be none")
+ msg := fmt.tprintf("Expected `json.marshal` to return nil, got %v", err)
+ expect(t, err == nil, msg)
}
PRODUCTS := `
@@ -97,7 +98,7 @@ PRODUCTS := `
"cash": "0",
"products": [
{
- "name": "Cog Cola",
+ "name": "Cog\nCola",
"cost": "3",
"owned": "1",
@@ -204,7 +205,7 @@ original_data := Game_Marshal{
cash = "0",
products = {
{
- name = "Cog Cola",
+ name = "Cog\nCola",
cost = "3",
owned = "1",
profit = "4",
@@ -331,13 +332,14 @@ unmarshal_json :: proc(t: ^testing.T) {
err := json.unmarshal(transmute([]u8)PRODUCTS, &g, json.DEFAULT_SPECIFICATION)
defer cleanup(g)
- expect(t, err == nil, "Expected json error to be nil")
+ msg := fmt.tprintf("Expected `json.unmarshal` to return nil, got %v", err)
+ expect(t, err == nil, msg)
- msg := fmt.tprintf("Expected %v products to have been unmarshaled, got %v", len(original_data.products), len(g.products))
+ msg = fmt.tprintf("Expected %v products to have been unmarshaled, got %v", len(original_data.products), len(g.products))
expect(t, len(g.products) == len(original_data.products), msg)
- msg = fmt.tprintf("Expected cash to have been unmarshaled as %v, got %v", original_data.cash, g.cash)
- expect(t, original_data.cash == g.cash, "Cash unmarshaled improperly")
+ msg = fmt.tprintf("Expected cash to have been unmarshaled as %v, got %v", original_data.cash, g.cash)
+ expect(t, original_data.cash == g.cash, msg)
for p, i in g.products {
expect(t, p == original_data.products[i], "Producted unmarshaled improperly")