@borrrden - I have similar needs as @escamoteur. I have way too many different types of objects for it to be efficient to manually maintain code to go to and from dictionaries for all of them.
Is it possible to just pass in a string of json that contains the data of all my user properties that I want to save? That way I can use JSON.net (or whatever) configured how I need to it be (for example, I need it to be able to handle NodaTime types) in order to create/parse the json myself.
In other words, it would be great I could use Couchbase.net library to handle the interaction with the underlying data store and allow me to get raw json in and out. No need for dictionaries.
Is this possible today?
I found a pretty simple solution that appears to work in my tests so far however it isn’t as efficient as I’d like. Basically, JSON.net can be used to convert from a Dictionary<string, object> to my custom class and vice versa. When I get a dictionary, I convert it to json (using JSON.net directly) and then deserialize it to the class I actually want (also using JSON.net). This avoids any issues with sometimes encountering JArray or needing to implement custom handling for saving/parsing things like DateTimeOffset. Here are the two key methods:
public static Dictionary<string, object> ToCouchDictionary(object obj)
{
// Experimental
var json = JsonConvert.SerializeObject(obj);
var jsonToDict = JsonConvert.DeserializeObject<Dictionary<string, object>> (json);
return jsonToDict;
}
public static TObj ToObject<TObj>(IDictionary<string, object> dict)
{
// Experimental
var json = JsonConvert.SerializeObject(dict);
var obj = JsonConvert.DeserializeObject<TObj> (json);
return obj;
}
Does anyone see any limitations/drawbacks with this approach? I think its inefficient since I’m adding extra serialization & deserialization steps every time. Ideally I think I would like to have the option of just getting/setting the raw json user properties.
I would appreciate any of your thoughts on this.