I currently have an issue writing/reading a particular object structure into and out of LiteDB. Using LiteDB 5.0.12
Below is an example of the object structure I am using with the majority of properties ommited as they're irrelevant. The Properties
property is of type Dictionary<string, object>
so we are able to use whatever we need as a key's value.
Dictionary<string, object> customProps = new Dictionary<string, object>();
customProps.Add("key1", 1);
customProps.Add("key2", "Hello");
customProps.Add("key3", true);
customProps.Add("key4", DateTime.Now);
customProps.Add("key5", new TimeSpan(1, 20, 0));
dynamic exp = new ExpandoObject();
exp.prop1 = "val1";
exp.prop2 = "val2";
customProps.Add("key6", exp);
var scope = new UserScope
{
...
Policy = new Policy
{
...
Properties = customProps
...
}
};
When saving this object to LiteDB (inserting or updating) the below is what is stored:
{
...
"Properties": {
"key1": 1,
"key2": "Hello",
"key3": true,
"key4": {
"$date": "2022-11-23T13:39:25.5220000Z"
},
"key5": {
"$numberLong": "48000000000"
},
"key6": [
{
"_type": "System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib",
"Key": "prop1",
"Value": "val1"
},
{
"_type": "System.Collections.Generic.KeyValuePair`2[[System.String, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib",
"Key": "prop2",
"Value": "val2"
}
]
}
}
Reading this object back out of LiteDB results in null values:
{
"Policy": {
...
"Properties": {
"key1": 1,
"key2": "Hello",
"key3": true,
"key4": "2022-11-23T13:39:25.522+00:00",
"key5": 48000000000,
"key6": [
{
"Key": null,
"Value": null
},
{
"Key": null,
"Value": null
}
]
}
}
}
Using Dictionary<string, object>
doesn't seem to be an issue with basic values in the value side as object, for example string, int, datetime etc but using anything slightly more complex seems to be resulting in unexpected behaviour.
Am I missing something here or would I need to use a converter of some sort or something similar?
Any assistance would be appreciated.