On 19 October 2012 14:11, Alexandre Bergel <alexandre.bergel@me.com> wrote:
Hi Igor,
I like this very much.
When you say:
Object>>#asObjectLiteral to be a subclass responsibility.
I am not convinced this has to be the case. In my opinion, #asObjectLiteral should be a replacement of #storeString. And #storeString is defined on Object and it is not abstract.
well, it is easy to implement generic one (iterate over all inst vars, and then over all indexable vars, and put them into array.. ) but main point for not providing default one is to make conversion explicit i.e.: - not all objects can be represented as literals - only those, which do, should implement conversion methods. but i agree , this is arguable. As long as you using objects which provide own converters, (so a generic one never takes place), you don't have to worry about it.
According to the object you are converting into a string, #storeString may produce code that exceed the amount of literal you can have in a method. Maybe #asObjectLiteral could split the result into chunk that can be individually embedded in a single method.
not the case. #( whatever (no matter how deeply nested) ) is a _single_ literal.
I agree with you that #asObjectLiteral should be overridden. It would be cool to have Class>> asObjectLiteral
How large your parser is?
if you asking this quesiton, you miss the point totally. there is no parser except smalltalk parser. :) What parser you need to parse: #( Array 1 2 3) ? then i converting it to #(1 2 3) but that is has nothing to do with parsing: Array>>fromObjectLiteral "Convert receiver back from object literal representation. Arrays is always holding a class name as first element, dispatch to the class to perform a conversion " ^ (Smalltalk globals at: self first) fromObjectLiteral: self Collection class>>fromObjectLiteral: objLiteralArray ^ self withAll: (objLiteralArray allButFirst collect: [:each | each fromObjectLiteral ] ) So, if that answering your question, those two methods is 'how large' my parser is :)
Alexandre
On Oct 19, 2012, at 8:09 AM, Igor Stasenko <siguctua@gmail.com> wrote:
Hi, as i promised before, here the simple smalltalk-based literal format. It based on smalltalk syntax, and so, unlike JSON, it doesn't needs to have separate parser (a normal smalltalk parser used for that).
The idea is quite simple: you can tell any object to represent itself as an 'object literal' , for example:
(1@3) asObjectLiteral --> #(#Point 1 3)
{ 1@2. 3@4. true. false . nil } asObjectLiteral
-> #(#Array #(#Point 1 2) #(#Point 3 4) true false nil)
(Dictionary newFromPairs: { 1->#(1 2 3) . 'foo' -> 'bar' }) asObjectLiteral -> #(#Dictionary 1 #(#Array 1 2 3) 'foo' 'bar')
Next thing, you can 'pretty-print' it (kinda):
#(#Dictionary 1 #(#Array 1 2 3) 'foo' 'bar') printObjectLiteral
'#(#Dictionary 1 (#Array 1 2 3) ''foo'' ''bar'')'
and sure thing, you can do reverse conversion:
'#(#Dictionary 1 (#Array 1 2 3) ''foo'' ''bar'')' parseAsObjectLiteral
a Dictionary('foo'->'bar' 1->#(1 2 3) )
Initially, i thought that it could be generic (by implementing default Object>>#asObjectLiteral), but then after discussing it with others, we decided to leave
Object>>#asObjectLiteral to be a subclass responsibility. So, potentially the format allows to represent any object(s) as literals, except from circular referencing objects, of course.
The implementation is fairly simple, as you may guess and contains no new classes, but just extension methods here and there.
Take it with grain and salt, since it is just a small proof of concept. (And if doing it for real it may need some changes etc). Since i am far from areas right now, where it can be used, i don't want to pursue it further or advocate if this is the right way to do things. Neither i having a public repository for this project..
So, if there anyone who willing to pick it up and pursue the idea further, please feel free to do so and make a public repository for project.
-- Best regards, Igor Stasenko. <ObjectLiterals-IgorStasenko.1.mcz>
-- _,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;: Alexandre Bergel http://www.bergel.eu ^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.
-- Best regards, Igor Stasenko.