-
Notifications
You must be signed in to change notification settings - Fork 420
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
Add helpers to easily read and write a serializable type to JSON #25464
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a95b697
Add helper methods to read and write JSON
DanilaFe c5a23c3
Add tests for the new JSON helpers
DanilaFe 8c913ef
Lock down instability of JSON methods
DanilaFe 5985d28
Rename a file that doesn't use methods accordingly
DanilaFe 501112d
Remove unused code.
DanilaFe 36d4ad4
Update test .good file after its rename
DanilaFe d5903f4
Add documentation to new helpers
DanilaFe 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use IO, JSON; | ||
|
||
record myPair : serializable { | ||
var x: int; | ||
var y: string; | ||
} | ||
|
||
var myValue = new myPair(1, "hello"); | ||
var myValueJson = toJson(myValue); | ||
|
||
writeln(fromJson("{\"x\": 1, \"y\": \"hello\"}", myPair)); |
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 @@ | ||
(x = 1, y = hello) |
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,9 @@ | ||
use IO, JSON; | ||
|
||
record myPair : serializable { | ||
var x: int; | ||
var y: string; | ||
} | ||
|
||
var myValue = new myPair(1, "hello"); | ||
writeln(toJson(myValue)); |
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 @@ | ||
{"x":1, "y":"hello"} |
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,10 @@ | ||
use JSON; | ||
|
||
class Fighter { | ||
var subclass:string; | ||
var level:int; | ||
} | ||
|
||
var s = "{\"subclass\":\"Ninja\", \"level\":7}"; | ||
var n = fromJson(s, owned Fighter); | ||
writeln(n); |
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 @@ | ||
{subclass = Ninja, level = 7} |
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,8 @@ | ||
use IO, JSON; | ||
|
||
record myPair : serializable { | ||
var x: int; | ||
var y: string; | ||
} | ||
|
||
writeln(fromJson("[{\"x\":1, \"y\":\"hello\"}, {\"x\":1, \"y\":\"hello\"}, {\"x\":1, \"y\":\"hello\"}]", [0..2] myPair)); |
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 @@ | ||
(x = 1, y = hello) (x = 1, y = hello) (x = 1, y = hello) |
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,9 @@ | ||
use IO, JSON; | ||
|
||
record myPair : serializable { | ||
var x: int; | ||
var y: string; | ||
} | ||
|
||
var myValue = new myPair(1, "hello"); | ||
writeln(toJson([myValue, myValue, myValue])); |
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 @@ | ||
[{"x":1, "y":"hello"}, {"x":1, "y":"hello"}, {"x":1, "y":"hello"}] |
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,12 @@ | ||
use IO, JSON; | ||
|
||
record myPair : serializable { | ||
var x: int; | ||
var y: string; | ||
} | ||
|
||
var myValue = new myPair(1, "hello"); | ||
var myValueJson = toJson(myValue); | ||
|
||
writeln(myValueJson); | ||
writeln(fromJson(myValueJson, myPair)); |
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,4 @@ | ||
jsonmethods.chpl:9: warning: toJson is unstable | ||
jsonmethods.chpl:12: warning: fromJson is unstable | ||
{"x":1, "y":"hello"} | ||
(x = 1, y = hello) |
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.
these values are unused, should they be removed from this test?
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.
Good catch, they should be.