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 quickstart section #21

Merged
merged 6 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions HelpSource/Classes/JSONlib.schelp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ e[\myPoint].x
METHOD:: convertToSC
Encodes a given JSON string to a link::Classes/Event::, link::Classes/Dictionary:: or link::Classes/Array::.

argument:: string
JSON string one wants to parse.
Keep in mind that any code::":: needs to be escaped with code::\"::, so at least every key needs this.
argument:: json
JSON link::Classes/String:: or link::Classes/Symbol:: one wants to parse.
Keep in mind that when using a String any code::":: needs to be escaped with code::\":: (so at least every key needs this) and on a Symbol code::':: needs to be escaped with code::\'::.
If you have copied a JSON from elsewhere either save it as a file and use link::#*parseFile:: or use the console of your browser by simply wrapping the JSON into backticks (e.g. code::`{"my": "json"}`::).
The returned string will contain the all necessary string escapes for SuperCollider."
argument:: customDecoder
Expand Down
69 changes: 68 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,76 @@ which this Quark implements, building on top of the existing implementation.
There also exists a [json](https://github.com/supercollider-quarks/json) Quark which also adds a wrapper for [sccode.org](https://sccode.org) but lacks a recursive encoding of objects.
The goal of `JSONlib` is to simply provide a full implementation of the JSON standard in sclang and nothing beyond it.

## Quickstart

### Installation

```supercollider
// install the quark
Quarks.install("https://github.com/musikinformatik/JSONlib.git");

// restart the interpreter so the new classes are available
thisProcess.recompile;

// open documention
HelpBrowser.openHelpFor("Classes/JSONlib");
```

### Basic usage

#### Parse a JSON

Let's say we have a JSON with an integer as a value

```json
{
"hello": 42
}
```

which we want to parse in sclang.

```supercollider
// use Symbol instead of String to get rid of escaping quotation marks
j = '{"hello": 42}';

// turn this into an Event
d = JSONlib.convertToSC(j);
// -> ( 'hello': 42 )

// an integer gets parsed as an integer
d[\hello].class
// -> Integer

// compare to the built-in method of sclang
// it uses a Dictionary instead of an Event
d = j.parseJSON()
// -> Dictionary[ (hello -> 42) ]

// but 42 is a string here
d["hello"].class
// -> String
```

#### Encode an Event as JSON

```supercollider
// create an event
e = (\foo: "bar", \baz: (\nested: true));

e.asJSON
// -> { "baz": { "nested": true }, "foo": "bar" }

// or use the class JSONlib
JSONlib.convertToJSON(e);
// -> { "baz": { "nested": true }, "foo": "bar" }
```

Advanced usage is described in the SCdoc documentation.

## Development

Run tests via
Make sure to run the tests via

```supercollider
TestJSONlib.run;
Expand Down
10 changes: 10 additions & 0 deletions Tests/TestJSONlib.sc
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,16 @@ TestJSONlib : UnitTest {
}


test_jsonSymbolAsString {
var j = '{"hello": 42}';
var e = JSONlib.convertToSC(j);
this.assertEquals(
e[\hello],
42,
"convertToSC needs to accept Symbols as well",
);
}

// private implementation
// util

Expand Down
12 changes: 8 additions & 4 deletions classes/JSONlib.sc
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ JSONlib {
^this.new(postWarnings, customEncoder: customEncoder).prConvertToJson(object)
}

*convertToSC {|string, customDecoder=nil, useEvent=true, postWarnings=true|
if(string.isKindOf(String).not) {
Error("Can only parse a String to JSON but received %".format(string.class)).throw
*convertToSC {|json, customDecoder=nil, useEvent=true, postWarnings=true|
if(json.isKindOf(Symbol)) {
json = json.asString;
};

if(json.isKindOf(String).not) {
Error("Can only parse a String to JSON but received %".format(json.class)).throw
};
^this.new(
postWarnings,
customDecoder: customDecoder,
useEvent: useEvent
).prConvertToSC(string.parseJSON)
).prConvertToSC(json.parseJSON)
}

*parseFile {|filePath, customDecoder=nil, useEvent=true, postWarnings=true|
Expand Down