-
Notifications
You must be signed in to change notification settings - Fork 1
feat(stdlib): make json_encode strict and add json_encode_lossy π§ #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f7bb0ac
feat(stdlib): make json_encode strict and add json_encode_lossy π§
timfennis e712243
fix(stdlib): detect cycles through heaps and iterators in json_encodeβ¦
timfennis 97c582b
feat(stdlib): map the unit value to JSON null instead of None π³οΈ
timfennis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
tests/functional/programs/605_stdlib_serde/003_numbers.ndc
This file contains hidden or 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,13 @@ | ||
| // Big integers round-trip exactly instead of degrading to floats | ||
| let big = 2 ^ 100 + 1; | ||
| assert_eq(json_decode(json_encode(big)), big); | ||
| assert_eq(json_decode("123456789123456789123456789"), 123456789123456789123456789); | ||
|
|
||
| // Numbers written with a decimal point or exponent decode to floats | ||
| assert_eq(json_decode("1e2"), 100.0); | ||
| assert_eq(json_decode("123.5"), 123.5); | ||
| assert_eq(json_decode(json_encode(0.1)), 0.1); | ||
|
|
||
| // null and the unit value are the same thing on both sides | ||
| assert_eq(json_decode("null"), ()); | ||
| assert_eq(json_encode(()), "null"); |
This file contains hidden or 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,41 @@ | ||
| // json_encode_lossy accepts everything json_encode rejects (except functions | ||
| // and cyclic values) by degrading it to the nearest JSON representation | ||
|
|
||
| // Rationals become floats | ||
| assert_eq(json_encode_lossy(10/3), "3.3333333333333335"); | ||
|
|
||
| // Complex numbers become strings | ||
| assert_eq(json_encode_lossy(5.0 + 3.1j), "\"5+3.1i\""); | ||
|
|
||
| // Options are unwrapped | ||
| assert_eq(json_encode_lossy(Some(5)), "5"); | ||
| assert_eq(json_encode_lossy(None), "null"); | ||
|
|
||
| // Sets become objects with null values | ||
| assert_eq(json_encode_lossy(%{"a", "b"}), "{\"a\":null,\"b\":null}"); | ||
|
|
||
| // Non-finite floats and the unit value become null | ||
| assert_eq(json_encode_lossy(0.0 / 0.0), "null"); | ||
| assert_eq(json_encode_lossy((1, (), 2)), "[1,null,2]"); | ||
|
|
||
| // Non-string map keys are stringified, map defaults are dropped | ||
| assert_eq(json_encode_lossy(%{1: "a"}), "{\"1\":\"a\"}"); | ||
| assert_eq(json_encode_lossy(%{: 0}), "{}"); | ||
|
|
||
| // Iterators are drained | ||
| assert_eq(json_encode_lossy(1..4), "[1,2,3]"); | ||
|
|
||
| // Heaps become arrays in priority order | ||
| let h = MaxHeap(); | ||
| h.push(1); h.push(3); h.push(2); | ||
| assert_eq(json_encode_lossy(h), "[3,2,1]"); | ||
|
|
||
| let m = MinHeap(); | ||
| m.push(3); m.push(1); m.push(2); | ||
| assert_eq(json_encode_lossy(m), "[1,2,3]"); | ||
|
|
||
| // Deques become arrays | ||
| let d = Deque(); | ||
| d.push_back(1); | ||
| d.push_front(0); | ||
| assert_eq(json_encode_lossy(d), "[0,1]"); |
9 changes: 9 additions & 0 deletions
9
tests/functional/programs/605_stdlib_serde/005_unit_and_sets.ndc
This file contains hidden or 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,9 @@ | ||
| // The unit value () pairs with JSON null in both directions | ||
| assert_eq(json_encode(()), "null"); | ||
| assert_eq(json_decode("null"), ()); | ||
| assert_eq(json_encode([1, (), 2]), "[1,null,2]"); | ||
|
|
||
| // A set is a map with unit values, so it round-trips as an object with nulls | ||
| assert_eq(json_encode(%{"a", "b"}), "{\"a\":null,\"b\":null}"); | ||
| assert_eq(json_decode(json_encode(%{"a", "b"})), %{"a", "b"}); | ||
| assert_eq(json_decode("{\"a\":null}"), %{"a": ()}); |
2 changes: 2 additions & 0 deletions
2
tests/functional/programs/605_stdlib_serde/010_encode_error_rational.ndc
This file contains hidden or 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,2 @@ | ||
| json_encode(10/3); | ||
| // expect-error: cannot convert a rational number to JSON |
2 changes: 2 additions & 0 deletions
2
tests/functional/programs/605_stdlib_serde/011_encode_error_complex.ndc
This file contains hidden or 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,2 @@ | ||
| json_encode(5.0 + 3.1j); | ||
| // expect-error: cannot convert a complex number to JSON |
4 changes: 4 additions & 0 deletions
4
tests/functional/programs/605_stdlib_serde/013_encode_error_heap.ndc
This file contains hidden or 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,4 @@ | ||
| let h = MaxHeap(); | ||
| h.push(1); | ||
| json_encode(h); | ||
| // expect-error: cannot convert a heap to JSON |
2 changes: 2 additions & 0 deletions
2
tests/functional/programs/605_stdlib_serde/014_encode_error_iterator.ndc
This file contains hidden or 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,2 @@ | ||
| json_encode(1..10); | ||
| // expect-error: cannot convert an iterator to JSON |
2 changes: 2 additions & 0 deletions
2
tests/functional/programs/605_stdlib_serde/015_encode_error_option.ndc
This file contains hidden or 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,2 @@ | ||
| json_encode(Some(5)); | ||
| // expect-error: cannot convert an option to JSON |
2 changes: 2 additions & 0 deletions
2
tests/functional/programs/605_stdlib_serde/016_encode_error_none.ndc
This file contains hidden or 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,2 @@ | ||
| json_encode([1, None, 2]); | ||
| // expect-error: cannot convert None to JSON |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.