JSONlib

A JSON de- and encoder for SuperCollider

The JSON implementation of the SuperCollider standard library lacks certain features such as

  • [ ] a JSON encoder (the conversion of an dictionary to a JSON string)
  • [ ] parsing JSON values into their respective type (.parseJSON converts everything to a string)

which this Quark implements, building on top of the existing implementation.

There also exists a json Quark which also adds a wrapper for 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

// 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

{
    "hello": 42
}

which we want to parse in sclang.

// 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

// 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

Make sure to run the tests via

TestJSONlib.run;

License

GPL-2.0

Version Kinds Released Published by Notes
0.1.0 quark 1 hour, 25 minutes ago capital-G

No dependencies.

Filename Kind ABI Platform
jsonlib-0.1.0-quark-any-any [details] quark any any
jsonlib-0.1.0-source-any-any [details] source any any
Tests/TestJSONlib.sc
  • TestJSONlib : UnitTest line 1
    • test_objectEncode
    • test_objectStringKeysEncode
    • test_arrayEncode
    • test_valuesEncode
    • test_stringsEncode
    • test_numberEncode
    • test_nullEncode
    • test_numberAsKey
    • test_functionAsValue
    • test_functionsAsValueRecursive
    • test_anotherFunctionAsValue
    • test_ref
    • test_stringNull
    • test_objectDecode
    • test_arrayDecode
    • test_valuesDecode
    • test_stringsDecode
    • test_numbersDecode
    • test_jsonNullDecode
    • test_jsonEncodeDict
    • test_jsonEncodeEvent
    • test_customEncoderFunc
    • test_customDecoderFunc
    • test_arrayMethod
    • test_dictMethod
    • test_eventMethod
    • test_jsonSymbolAsString
    • prGetPathFor
    • prParseJsonFile
classes/JSONlib.sc
  • JSONlib : Object line 1 docs
    • *new
    • *convertToJSON
    • *convertToSC
    • *parseFile
    • postWarnings
    • postWarnings_
    • useEvent
    • useEvent_
    • customEncoder
    • customEncoder_
    • customDecoder
    • customDecoder_
    • prConvertToJson
    • prConvertToSC
classes/extJSON.sc
  • + Collection line 1
    • asJSON
  • + Dictionary line 7
    • asJSON

Metadata

Updated
1 hour, 25 minutes ago

License
GPL-3.0-or-later

Owner
capital-G

Install

Magnet CLI:

magnet add jsonlib