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

Null ref #11

Merged
merged 4 commits into from
Jan 19, 2023
Merged
Changes from 1 commit
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
41 changes: 40 additions & 1 deletion classes/JSONlib.sc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ JSONlib {
var array;
^case
{ v.isKindOf(Symbol) } { this.prConvertToJson(v.asString) }
{ v == "null" or: { v.value == nil } or: { v == nil } } { "null" }
// only check value if it is a ref
{ (v.isKindOf(Ref)).if({v.value==nil}, {false}) or: { v == nil } } { "null" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please instead of

{ (v.isKindOf(Ref)).if({v.value==nil}, {false}) or: { v == nil } } { "null" }

make it

{ v.isNil or: { v.isKindOf(Ref) and: { v.value.isNil } } } { "null" }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imlpemented

// sc closely implements the JSON string, see https://www.json.org/json-en.html
// but the post window parses \n as linebreak etc. which makes copying of the JSON from
// the post window error prone
Expand Down Expand Up @@ -279,6 +280,7 @@ TestJSONlib : UnitTest {
);
}

// encoding of non-json values
test_functionAsValue {
var o = (
\func: {|x| "hello".postln}
Expand Down Expand Up @@ -308,6 +310,43 @@ TestJSONlib : UnitTest {
);
}

// additional ref/nil test
test_anotherFunctionAsValue {
// primary purpose to verify null check
var o = (
\func: { |x| x !? (x+1) },
);
var j = JSONlib.convertToJSON(o);
this.assertEquals(
j,
"{ \"func\": \"{ |x| x !? (x+1) }\" }",
".value should not get called on a non-ref",
);
}

test_ref {
var o = (
\ref: `42,
);
var j = JSONlib.convertToJSON(o);
this.assertEquals(
j,
"{ \"ref\": \"`(42)\" }",
"Non-nil refs should be encoded as compile strings",
);
}

test_stringNull {
var o = (
\null: "null",
);
var j = JSONlib.convertToJSON(o);
this.assertEquals(
j,
"{ \"null\": \"null\" }",
"A \"null\" string should be represented as a string",
)
}

// decoding tests - taken from json.org
// we only test for valid json
Expand Down