mauriziodm/DJSON

Can't serialize my class

Closed this issue · 1 comments

I can't serialize my class from the class itself.

My class has a SaveToStream (and a LoadFromStream, SaveToFile, LoadFromFile too) method to serialize itself to a TStream descendant, but giving a look at the Call Stack, seems like DJSON does call my class' SaveToStream procedure, which then runs another serialization and so on, leading to a stack overflow exception.

At the moment I found a workaround: renaming those to Load and Save (both overloaded to support TStream or a TFilename as parameter) I got it working, but I really would appreciate if this can be solved so my class will look more standard.

Thanks for your attention. :)

Hi zizzo81m the problem you encounter is normal for how DJSON was designed. In practice, DJSON considers as "streamable" any object that has the "SaveToStream" and "LoadFromStream" methods and that it is not a list, a dictionary, or similar or even individual objects with which there is a one-to-one or one-to-many relationship (HasOne, HasMany, BelongsTo). For these "streamable objects", DJSON does not make a normal serialization but automatically inserts into the JSON the contents of their stream stream by calling the methods mentioned above (naturally encoding them). This mechanism is the basis of the ability of the tool to properly serialize objects with TBitMap properties (for example) without having to specify any attributes on the class statement. Unfortunately, however, in your case, it's creating a problem that has resolved momentarily by you (correctly) changing the name to those two methods. To permanently resolve, you could add DJSON to a djUnstreamable attribute that indicates to DJSON that although the class possesses the two "SaveToStream" and "LoadFromStream" methods should not be considered as "streamable". What do you think about it?