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 docs #17

Merged
merged 4 commits into from
Jan 23, 2023
Merged

add docs #17

merged 4 commits into from
Jan 23, 2023

Conversation

capital-G
Copy link
Member

Some docs

Closes #2 by covering most of it

@capital-G capital-G requested a review from telephon January 20, 2023 12:45
@capital-G capital-G linked an issue Jan 20, 2023 that may be closed by this pull request
6 tasks
Copy link
Contributor

@telephon telephon left a 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

## 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.
Copy link
Contributor

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?

Copy link
Member Author

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

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

conversion


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.
Copy link
Contributor

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")"

Copy link
Member Author

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

Copy link
Contributor

@telephon telephon Jan 22, 2023

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 Show resolved Hide resolved
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.
Copy link
Contributor

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::.

Copy link
Member Author

Choose a reason for hiding this comment

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

nice gh conversion :)

Copy link
Contributor

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.

Copy link
Contributor

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.

(
f = {|object|
if(object.class == Point, {
[object.x, object.y];
Copy link
Contributor

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

Copy link
Member Author

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

Copy link
Contributor

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, ]; :)

Copy link
Member Author

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 :)

Copy link
Contributor

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.


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.
Copy link
Contributor

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
(
Copy link
Contributor

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) })

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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/

HelpSource/Classes/JSONlib.schelp Outdated Show resolved Hide resolved
Copy link
Member Author

@capital-G capital-G left a 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 :)

## 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.
Copy link
Member Author

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


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.
Copy link
Member Author

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

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.
Copy link
Member Author

Choose a reason for hiding this comment

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

nice gh conversion :)

e[\foo].value
// -> nil

// you can also use .value on other scalars
Copy link
Member Author

Choose a reason for hiding this comment

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

switched to data types

(
f = {|object|
if(object.class == Point, {
[object.x, object.y];
Copy link
Member Author

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
(
Copy link
Member Author

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.

@capital-G capital-G requested a review from telephon January 21, 2023 21:49

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.
Copy link
Contributor

@telephon telephon Jan 22, 2023

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.

@capital-G capital-G merged commit cf03e5d into main Jan 23, 2023
@capital-G capital-G deleted the 2-write-documentation branch January 23, 2023 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Write documentation
2 participants