Skip to content
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 7 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions modules/standard/JSON.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -1171,4 +1171,21 @@ module JSON {
}
}
}

@unstable
proc fromJson(jsonString: string, type loadAs): loadAs throws {
var fileReader = openStringReader(jsonString,
deserializer=new jsonDeserializer());
return fileReader.read(loadAs);
}

@unstable
proc toJson(arg): string throws {
var memWriter = openMemFile();
var writer = memWriter.writer(locking=false).withSerializer(jsonSerializer);
writer.write(arg);
writer.close();

return memWriter.reader(locking=false).readAll(string);
}
}
11 changes: 11 additions & 0 deletions test/io/serializers/jsonHelpers/basicread.chpl
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);
Copy link
Contributor

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?

Copy link
Contributor Author

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.


writeln(fromJson("{\"x\": 1, \"y\": \"hello\"}", myPair));
1 change: 1 addition & 0 deletions test/io/serializers/jsonHelpers/basicread.good
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(x = 1, y = hello)
9 changes: 9 additions & 0 deletions test/io/serializers/jsonHelpers/basicwrite.chpl
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));
1 change: 1 addition & 0 deletions test/io/serializers/jsonHelpers/basicwrite.good
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"x":1, "y":"hello"}
10 changes: 10 additions & 0 deletions test/io/serializers/jsonHelpers/issue-8278.chpl
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);
1 change: 1 addition & 0 deletions test/io/serializers/jsonHelpers/issue-8278.good
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{subclass = Ninja, level = 7}
8 changes: 8 additions & 0 deletions test/io/serializers/jsonHelpers/readarray.chpl
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));
1 change: 1 addition & 0 deletions test/io/serializers/jsonHelpers/readarray.good
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)
9 changes: 9 additions & 0 deletions test/io/serializers/jsonHelpers/writearray.chpl
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]));
1 change: 1 addition & 0 deletions test/io/serializers/jsonHelpers/writearray.good
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"}]
12 changes: 12 additions & 0 deletions test/unstable/jsonhelpers.chpl
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));
4 changes: 4 additions & 0 deletions test/unstable/jsonhelpers.good
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)