-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import x.json2.decoder2 as json | ||
|
||
pub struct Stru { | ||
val int | ||
val2 string | ||
val3 Stru2 | ||
} | ||
|
||
pub struct Stru2 { | ||
a int | ||
churrasco string | ||
} | ||
|
||
struct StructType[T] { | ||
mut: | ||
val T | ||
} | ||
|
||
struct StructTypeOption[T] { | ||
mut: | ||
val ?T | ||
} | ||
|
||
struct StructTypePointer[T] { | ||
mut: | ||
val &T | ||
} | ||
|
||
fn test_array_of_strings() { | ||
// Structs | ||
assert json.decode[StructType[string]]('{"val": "2"}')! == StructType{ | ||
val: '2' | ||
} | ||
assert json.decode[StructType[int]]('{"val": 2}')! == StructType{ | ||
val: 2 | ||
} | ||
|
||
// maps | ||
assert json.decode[map[string]string]('{"val": "2"}')! == { | ||
'val': '2' | ||
} | ||
// assert json.decode[map[string]int]('{"val": 2}')! == {"val": 2} | ||
|
||
// // nested map | ||
// assert json.decode[map[string]map[string]string]('{"val": {"val2": "2"}}')! == {"val": {"val2": "2"}} | ||
|
||
// nested struct | ||
assert json.decode[Stru]('{"val": 1, "val2": "lala", "val3": {"a": 2, "churrasco": "leleu"}}')! == Stru{ | ||
val: 1 | ||
val2: 'lala' | ||
val3: Stru2{ | ||
a: 2 | ||
churrasco: 'leleu' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import x.json2.decoder2 as json | ||
|
||
fn test_json_escape_low_chars() { | ||
assert json.decode[string](r'"\u001b"')! == '\u001b' | ||
assert json.decode[string](r'"\u000f"')! == '\u000f' | ||
assert json.decode[string](r'" "')! == '\u0020' | ||
assert json.decode[string](r'"\u0000"')! == '\u0000' | ||
} | ||
|
||
fn test_json_string() { | ||
assert json.decode[string](r'"te\u2714st"')! == 'te✔st' | ||
// assert json.decode[string]('te✔st')! == 'te✔st' | ||
} | ||
|
||
fn test_json_string_emoji() { | ||
assert json.decode[string](r'"🐈"')! == '🐈' | ||
assert json.decode[string](r'"💀"')! == '💀' | ||
assert json.decode[string](r'"🐈💀"')! == '🐈💀' | ||
} | ||
|
||
fn test_json_string_non_ascii() { | ||
assert json.decode[string](r'"\u3072\u3089\u304c\u306a"')! == 'ひらがな' | ||
assert json.decode[string]('"a\\u3072b\\u3089c\\u304cd\\u306ae fgh"')! == 'aひbらcがdなe fgh' | ||
assert json.decode[string]('"\\u3072\\u3089\\u304c\\u306a"')! == 'ひらがな' | ||
} | ||
|
||
fn test_utf8_strings_are_not_modified() { | ||
assert json.decode[string]('"ü"')! == 'ü' | ||
assert json.decode[string]('"Schilddrüsenerkrankungen"')! == 'Schilddrüsenerkrankungen' | ||
} |