-
Notifications
You must be signed in to change notification settings - Fork 0
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 docs #17
add docs #17
Conversation
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.
nice! Just a few small things
HelpSource/Classes/JSONlib.schelp
Outdated
## does not respect the types of a JSON but instead uses the type of String for every value | ||
:: | ||
|
||
An encoder emphasis::simply:: needs to translate the SuperCollider scalars to the set of JSON scalars. |
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.
Do you only mean numbers? Booleans, arrays and sub-dictionaries also require translation, no?
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.
thx, data types is probably more fitting
HelpSource/Classes/JSONlib.schelp
Outdated
Consult the source code on what kind of implicit conversions are made. | ||
These defaults can be overwritten and extended by providing a code::customEncoder:: or code::customDecoder:: (see link::Classes/JSONlib#*new::). | ||
|
||
The type converion issue is a bit more difficult as it is not possible for us to recover the original types from the JSON explicitly. |
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.
conversion
HelpSource/Classes/JSONlib.schelp
Outdated
|
||
The type converion issue is a bit more difficult as it is not possible for us to recover the original types from the JSON explicitly. | ||
Was code::"42":: always a string or was it an link::Classes/Integer:: once that got converted by link::Classes/String#-parseJSON:: from an Integer to a link::Classes/String::? | ||
- so we decided to use implicit casting if the string matches a JSON representation other than String. |
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.
casting -> "assignment of an appropriate class ("casting")"
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.
but explicit/implicit casting is a really common term :/
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.
Yes, the word "casting" is never used in SuperCollider, just as "data type". You can take a look in the documentation. Why this is so is a complicated issue that I still try to understand better. I think the reason is this: in the Smalltalk lingo, these terms tend to be confusing, because where they originaly came from, they referred to parts of expressions (a type of a variable, whose data is casted to another variable of another type), and didn't refer to the objects themselves. Now one could argue that in Smalltalk, the type is in the object, but that is potentially misleading, because the object may change its behaviour in radical ways over its lifetime. Well, so much for that. I don't want to insist, we can fix later if you prefer.
HelpSource/Classes/JSONlib.schelp
Outdated
code::null:: is a special value in JSON which emphasis::represents the intentional absence of any object value.:: footnote::https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null::. | ||
|
||
SuperColliders equivalent would be link::Classes/Nil:: which gets implemented as an object via code::nil::. | ||
Yet it is not as easy as that because we can not store code::nil:: as a value in a link::Classes/Dictionary:: as this is the sclang syntax to delete a key-value pair within a dictionary. |
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.
as this is the sclang syntax to delete a key-value pair within a dictionary.
->
because nil
represents the return value for an empty slot and code::put(key, nil):: is equivalent to removing the object at code::key::.
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.
nice gh conversion :)
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.
🚪
argument:: customEncoder | ||
Using a custom encoder allows to specify the encoding an abitrary object into its JSON representation. | ||
To introduce a custom encoding (e.g. link::Classes/Point:: or link::Classes/Function::) simply provide a function which accepts the object as a first argument and return a non-code::nil:: value if it a custom representation should be used. | ||
|
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.
value if it a custom representation should be used.
->
value if this custom representation should be used. Otherwise, it will be encoded as usual.
HelpSource/Classes/JSONlib.schelp
Outdated
( | ||
f = {|object| | ||
if(object.class == Point, { | ||
[object.x, object.y]; |
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.
you can drop the colons
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.
yeah, but I prefer to have semicolons everywhere
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.
ok, but then also write [object.x, object.y, ];
:)
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.
Nah, ;
is commonly used to terminate a statement, ,
is used to separate elements :)
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.
so yes, ok if that looks better to you, let's keep this convention in this quark.
HelpSource/Classes/JSONlib.schelp
Outdated
|
||
argument:: string | ||
JSON string one wants to parse. | ||
Have in mind that any code::":: needs to be escaped with code::\"::, so at least every key needs this. |
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.
Have -> Keep
j = JSONlib.convertToJSON(e); | ||
|
||
// save JSON string on disk as .json file | ||
( |
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.
alternatively:
File.use("~/foo.json".standardizePath, "w", { |f| f.write(j) })
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.
To me this seems more implicit as I have to guess that the provided function is a callback which is called when the file has been opened successfully - and this is where I have to look into the docs. Also to me the .write
is more in line with other languages.
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.
ok. use
has a protect
built in so that it doesn't leave open files when something fails.
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.
BTW, there is a really great concept for this in Python, context managers https://realpython.com/python-with-statement/
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.
Thx for the really necessary review. Please take a look again if its ok and if so please merge :)
HelpSource/Classes/JSONlib.schelp
Outdated
## does not respect the types of a JSON but instead uses the type of String for every value | ||
:: | ||
|
||
An encoder emphasis::simply:: needs to translate the SuperCollider scalars to the set of JSON scalars. |
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.
thx, data types is probably more fitting
HelpSource/Classes/JSONlib.schelp
Outdated
|
||
The type converion issue is a bit more difficult as it is not possible for us to recover the original types from the JSON explicitly. | ||
Was code::"42":: always a string or was it an link::Classes/Integer:: once that got converted by link::Classes/String#-parseJSON:: from an Integer to a link::Classes/String::? | ||
- so we decided to use implicit casting if the string matches a JSON representation other than String. |
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.
but explicit/implicit casting is a really common term :/
HelpSource/Classes/JSONlib.schelp
Outdated
code::null:: is a special value in JSON which emphasis::represents the intentional absence of any object value.:: footnote::https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null::. | ||
|
||
SuperColliders equivalent would be link::Classes/Nil:: which gets implemented as an object via code::nil::. | ||
Yet it is not as easy as that because we can not store code::nil:: as a value in a link::Classes/Dictionary:: as this is the sclang syntax to delete a key-value pair within a dictionary. |
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.
nice gh conversion :)
HelpSource/Classes/JSONlib.schelp
Outdated
e[\foo].value | ||
// -> nil | ||
|
||
// you can also use .value on other scalars |
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.
switched to data types
HelpSource/Classes/JSONlib.schelp
Outdated
( | ||
f = {|object| | ||
if(object.class == Point, { | ||
[object.x, object.y]; |
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.
yeah, but I prefer to have semicolons everywhere
j = JSONlib.convertToJSON(e); | ||
|
||
// save JSON string on disk as .json file | ||
( |
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.
To me this seems more implicit as I have to guess that the provided function is a callback which is called when the file has been opened successfully - and this is where I have to look into the docs. Also to me the .write
is more in line with other languages.
HelpSource/Classes/JSONlib.schelp
Outdated
|
||
The type converion issue is a bit more difficult as it is not possible for us to recover the original types from the JSON explicitly. | ||
Was code::"42":: always a string or was it an link::Classes/Integer:: once that got converted by link::Classes/String#-parseJSON:: from an Integer to a link::Classes/String::? | ||
- so we decided to use implicit casting if the string matches a JSON representation other than String. |
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.
Yes, the word "casting" is never used in SuperCollider, just as "data type". You can take a look in the documentation. Why this is so is a complicated issue that I still try to understand better. I think the reason is this: in the Smalltalk lingo, these terms tend to be confusing, because where they originaly came from, they referred to parts of expressions (a type of a variable, whose data is casted to another variable of another type), and didn't refer to the objects themselves. Now one could argue that in Smalltalk, the type is in the object, but that is potentially misleading, because the object may change its behaviour in radical ways over its lifetime. Well, so much for that. I don't want to insist, we can fix later if you prefer.
Some docs
Closes #2 by covering most of it