Ted
Ted

Reputation: 20184

ServiceStack.OrmLite: Again, serialization fails, a bool becomes a string when reading it back from blobbed field

I have asked questions regarding serialization and deserialization before regarding ServiceStack OrmLite, and I am not sure if this is the same issues I've had before, but I don't think so.

Basically, I have a public Dictionary<string, object> _CustomProperties where I store some random data.

I add random data to this dictionary like this:

ms._SetCustomProperty<bool>("ConvertedContactItems", true);

like so:

enter image description here

and this is blobbed using OrmLite, like this:

enter image description here

When I read it back (using Untyped API), I do as below:

bool hasConvertedContactItems = actor._GetCustomProperty<bool>("ConvertedContactItems");

but the boolean true is now a string, not a bool:

enter image description here

The question is: what am I doing wrong? I have a slight feeling this might be an answer that sounds the same as before, but I am not quite sure about it.

UPDATE 1

Below is a Dictionary<string, object> serialized using JsvSerializer in OrmLite. I thought that since it cannot properly deserialze a boolean, it for sure cannot deserialize a custom object. Well, it seems that custom objects have sometimes the type info in there. The CustomerSettings gets correctly deserialized, but bool, datetime etc, does not.

So, if type info is added for custom objects, why not for primitives so we can deserialize them correctly? (Too bloated? Then why for custom objects?)

{
    MyBoolProp:True, // <-- why not emit type info, like CustomerSettings below?
    MyDateTime:2018-08-01T22:49:58.7586897+02:00, // <-- why not emit type info
    Settings:
    {
        __type:"Commons.CustomerSettings,Commons",
        NotifStarted:False,
        NotifFinished:True
    }
}

Upvotes: 2

Views: 131

Answers (1)

mythz
mythz

Reputation: 143374

The serialized output does not contain any Type information so if you try to deserialize it into an object it doesn't know what Type to deserialize into so it leaves it as a string which is also an object.

I'd recommend avoid using object and instead if you want to serialize untyped key/value pairs to instead serialize a Dictionary<string,string> that way the value is always stored and retrieved as a string.

Of course if you need to serialize a typed Dictionary like Dictionary<string,bool> then you should use that instead.

Upvotes: 2

Related Questions