I'm attempting to marshal/de-marshal an type object and I'm running into an error.
It marshals fine, but de-marshalling causes the following error:
Error: unhandled exception: unknown file(1, 1) Error: int for pointer type expected expected [JsonParsingError]
Here's the type, note that Direction is an enum:
type
Exits = array[Direction,Room]
Room* = ref object of RootObj
descr:string
exits:Exits
Here's the resulting JSON from marshalling
{ "descr": "room 1", "exits": [ null, null, [ 140693998915776, { "descr": "room 2", "exits": [ null, null, null, null, null, null, null, null, null, null ] } ], null, [ 140693998915888, { "descr": "room 3", "exits": [ null, null, null, null, null, null, null, null, null, null ] } ], null, null, null, null, null ] }
I know the marshal page has the following restriction:
Restriction: For objects their type is not serialized. This means essentially that it does not work if the object has some other runtime type than its compiletime type:
but I don't feel like that's the issue. It appears that marshaller is pulling out the pointer value?
What am I missing here? I understand writing a generalized marshaller that deals with reference types in these sorts of scenarios can be complex and ugly, so if I can't do what I'm trying to do that's perfectly ok, I'm just wanting to understand what's going on here.
This looks as though you're serializing room[], but then trying to deserialize it as an instance of Room.
Briefly, instances of reference types are serialized as pairs [id, serialized_version], where id is a numeric id for the reference. Repeated occurrences of the same reference can then be serialized simply as id. The top level object in your JSON dump does not have an id, which makes it look as though you've used $$room[] instead of $$room to generate the JSON. It's also consistent with the error you are getting.