-
Notifications
You must be signed in to change notification settings - Fork 10
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
Writer produce correct top-level or in-array optional elem when custo… #73
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 |
---|---|---|
|
@@ -16,4 +16,5 @@ import | |
test_spec, | ||
test_parser, | ||
test_line_col, | ||
test_reader | ||
test_reader, | ||
test_writer |
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,101 @@ | ||
# json-serialization | ||
# Copyright (c) 2024 Status Research & Development GmbH | ||
# Licensed under either of | ||
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) | ||
# * MIT license ([LICENSE-MIT](LICENSE-MIT)) | ||
# at your option. | ||
# This file may not be copied, modified, or distributed except according to | ||
# those terms. | ||
|
||
import | ||
unittest2, | ||
../json_serialization/stew/results, | ||
../json_serialization/std/options, | ||
../json_serialization | ||
|
||
createJsonFlavor YourJson, | ||
omitOptionalFields = false | ||
|
||
createJsonFlavor MyJson, | ||
omitOptionalFields = true | ||
|
||
suite "Test writer": | ||
test "stdlib option top level some YourJson": | ||
var val = some(123) | ||
let json = YourJson.encode(val) | ||
check json == "123" | ||
|
||
test "stdlib option top level none YourJson": | ||
var val = none(int) | ||
let json = YourJson.encode(val) | ||
check json == "null" | ||
|
||
test "stdlib option top level some MyJson": | ||
var val = some(123) | ||
let json = MyJson.encode(val) | ||
check json == "123" | ||
|
||
test "stdlib option top level none MyJson": | ||
var val = none(int) | ||
let json = MyJson.encode(val) | ||
check json == "null" | ||
|
||
test "results option top level some YourJson": | ||
var val = Opt.some(123) | ||
let json = YourJson.encode(val) | ||
check json == "123" | ||
|
||
test "results option top level none YourJson": | ||
var val = Opt.none(int) | ||
let json = YourJson.encode(val) | ||
check json == "null" | ||
|
||
test "results option top level some MyJson": | ||
var val = Opt.some(123) | ||
let json = MyJson.encode(val) | ||
check json == "123" | ||
|
||
test "results option top level none MyJson": | ||
var val = Opt.none(int) | ||
let json = MyJson.encode(val) | ||
check json == "null" | ||
|
||
test "stdlib option array some YourJson": | ||
var val = [some(123), some(345)] | ||
let json = YourJson.encode(val) | ||
check json == "[123,345]" | ||
|
||
test "stdlib option array none YourJson": | ||
var val = [some(123), none(int), some(777)] | ||
let json = YourJson.encode(val) | ||
check json == "[123,null,777]" | ||
|
||
test "stdlib option array some MyJson": | ||
var val = [some(123), some(345)] | ||
let json = MyJson.encode(val) | ||
check json == "[123,345]" | ||
|
||
test "stdlib option array none MyJson": | ||
var val = [some(123), none(int), some(777)] | ||
let json = MyJson.encode(val) | ||
check json == "[123,null,777]" | ||
|
||
test "results option array some YourJson": | ||
var val = [Opt.some(123), Opt.some(345)] | ||
let json = YourJson.encode(val) | ||
check json == "[123,345]" | ||
|
||
test "results option array none YourJson": | ||
var val = [Opt.some(123), Opt.none(int), Opt.some(777)] | ||
let json = YourJson.encode(val) | ||
check json == "[123,null,777]" | ||
|
||
test "results option array some MyJson": | ||
var val = [Opt.some(123), Opt.some(345)] | ||
let json = MyJson.encode(val) | ||
check json == "[123,345]" | ||
|
||
test "results option array none MyJson": | ||
var val = [Opt.some(123), Opt.none(int), Opt.some(777)] | ||
let json = MyJson.encode(val) | ||
check json == "[123,null,777]" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm, I think the semantics proposed here could be very surprising for the user. Omitting optional fields in objects in one thing, but filtering such values out of array, so the array length is effectively changed is something that feels much more dangerous.
We could have a separate policy setting that controls this aspect perhaps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't change the array length. The old code is wrong. It didn't output anything between two comma which is violating the spec. Now it works correctly by writing
null
to that position in the array. It's correct according to the json spec and the rpc spec.