[squeak-dev] [ANN] STON - Smalltalk Object Notation

Sven Van Caekenberghe sven at beta9.be
Mon May 7 20:07:46 UTC 2012


Hi Stéphane,

On 05 May 2012, at 14:31, Stéphane Rollandin wrote:

> Looks very interesting !
> 
> However, it converts fractions into floats, which is a show stopper for me:
> 
> String streamContents: [ :stream |(STONWriter on: stream) nextPut: 3/4].
> ==> '0.75'
> 
> Could this be fixed ?

It could be fixed. The question is, should it ?

I thought about this and made the decision not to do it.

Why ? 

Well, for one, STON is based on JSON and is (since recently suggested by Dale Henrichs) backwards compatible with it. 

But the other point is: Fractions are just one Number type, but so are ScaledDecimals. Also, and even JSON does that, infinite precision integers are left unspecified (i.e. the standard does not talk about minimum and maximum values, but obviously, in the JSON case, they would become important in cross platform situations).

But you could overwrite #stonOn: and #fromSton: specifically for Fractions, maybe like this:

Float>>#stonOn: stonWriter
	stonWriter writeObject: self streamShortList: [ :array |
		array add: numerator; add: denominator ]

Float class>>#fromSton: stonReader
	| representation |
	representation := stonReader parseList.
	^ self 
		numerator: representation first 
		denominator: representation second 

This would then result in

String streamContents: [ :stream |(STONWriter on: stream) nextPut: 3/4]

==> 'Fraction[3,4]'

(This example suggests that I need some more API, since Fractions should be treated as immutable, non-sharable objects, which is not the case in the above example).

Thanks for the feedback,

Regards,

Sven


--
Sven Van Caekenberghe
http://stfx.eu
Smalltalk is the Red Pill






More information about the Squeak-dev mailing list