aboutsummaryrefslogtreecommitdiff
path: root/core/encoding/json/tokenizer.odin
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-09-29 13:06:03 +0100
committergingerBill <bill@gingerbill.org>2021-09-29 13:06:03 +0100
commit9bc12e3f381e65588afbd8b4ae7cb6e29cf5df79 (patch)
tree4cd7ef6d4b4cf9c11c2db7c3bbff63b0db5c34f5 /core/encoding/json/tokenizer.odin
parentb3f2263442a449fbd07803a0c454763c66671f60 (diff)
Improve `json.marshal` error handling for `io.Writer`; Add docs for the different JSON specifications
Diffstat (limited to 'core/encoding/json/tokenizer.odin')
-rw-r--r--core/encoding/json/tokenizer.odin44
1 files changed, 39 insertions, 5 deletions
diff --git a/core/encoding/json/tokenizer.odin b/core/encoding/json/tokenizer.odin
index f1b0db6ba..567600b90 100644
--- a/core/encoding/json/tokenizer.odin
+++ b/core/encoding/json/tokenizer.odin
@@ -402,6 +402,25 @@ is_valid_number :: proc(str: string, spec: Specification) -> bool {
}
}
}
+
+ if spec != .JSON && len(s) >= 2 {
+ // Allow for hexadecimal strings
+ if s[:2] == "0x" || s[:2] == "0X" {
+ s = s[2:]
+ if len(s) == 0 {
+ return false
+ }
+ hexadecimal_loop: for len(s) > 0 {
+ switch s[0] {
+ case '0'..='9', 'A'..='Z', 'a'..='z':
+ s = s[1:]
+ case:
+ break hexadecimal_loop
+ }
+ }
+ return len(s) == 0
+ }
+ }
switch s[0] {
case '0':
@@ -461,14 +480,16 @@ is_valid_string_literal :: proc(str: string, spec: Specification) -> bool {
if s[0] != s[len(s)-1] {
return false
}
- if s[0] != '"' || s[len(s)-1] != '"' {
+ switch quote {
+ case '"':
+ // okay
+ case '\'':
if spec != .JSON {
- if s[0] != '\'' || s[len(s)-1] != '\'' {
- return false
- }
- } else {
return false
}
+ // okay
+ case:
+ return false
}
s = s[1 : len(s)-1]
@@ -484,6 +505,19 @@ is_valid_string_literal :: proc(str: string, spec: Specification) -> bool {
switch s[i] {
case '"', '\'', '\\', '/', 'b', 'n', 'r', 't', 'f':
i += 1
+
+ case '\r':
+ if spec != .JSON && i+1 < len(s) && s[i+1] == '\n' {
+ i += 2
+ } else {
+ return false
+ }
+ case '\n':
+ if spec != .JSON {
+ i += 1
+ } else {
+ return false
+ }
case 'u':
if i >= len(s) {
return false