Hello,
Description
I was trying to use STON in a personal project (Serialization of Bloc). And i found a bug.
severity: high
Reproduction
code to reproduce:
serialization := STON toString: Float infinity.
materialization := STON fromString: serialization.
current result:
STON fromString: serialization
raise an exception but the bug occurs durring the serilization.
STON toString: Float infinity
create a String : 'Float infinity'
.
Because STON serialize this 'number' as a Class, it will try to materialize it as class. And the exception is raise.
Possible fix
I try to fix it quick by adding a custom method fromSton:
in Float class
Float class >> fromSton: aStonReader
aStonReader match: 'infinity' do: [ ^ self infinity ].
^ self nan
BUT, it breaks the STONReference
indexation.
Durring serialization := STON toString: Float infinity.
the STONWriter
doesn't have any reference in the variable named objects
.
Durring materialize:= STON toString: Float infinity.
with the custom fromSton:
, the STONReader
have the reference of a Float object.
This will create errors of indexation when resolving STONReference
.
Maybe a solution ?
I have a suggestion to fix the issue.
We could make the Float infinity
as constant (like a boolean true
or false
) when we serialize it.
It would look like something like this:
Float >> stonOn: stonWriter
self isInfinite
ifTrue: [ stonWriter writeInfinite ]
ifFalse: [ stonWriter writeFloat: self asFloat ]
STONWriter >> writeInfinite
writeStream << #infinite
STONReader >> parseConstantDo: block
"Custom to include Float infinite"
readStream peek = $t ifTrue: [
^ self match: 'true' do: [ block value: true ] ].
readStream peek = $f ifTrue: [
^ self match: 'false' do: [ block value: false ] ].
readStream peek = $i ifTrue: [
self match: 'infinite' do: [ block value: Float infinity ] ].
readStream peek = $n ifTrue: [
readStream next.
readStream peek = $i ifTrue: [
self match: 'il' do: [ block value: nil ] ].
readStream peek = $u ifTrue: [
self match: 'ull' do: [ block value: nil ] ] ]
I don't know if it will work with the JSON serialization.
Other
pharo image : Pharo-11.0.0+build.346.sha.b6ce4527b4f4881d6c6b4f968f1d1348b783b7a2 (64 Bit)