Can I make Raven DB serialize an object as it would a string, if i have created an implicit type conversion operator?

c#, json, json.net, ravendb

Solution

You can write a JsonConverter and teach RavenDB how you want to store the data. After you write the converter, register it in the store.Conventions.CustomizeSerializer event.

Problem

I have a class that looks something like this: ``` public class MyClass { string _value; public static implicit operator MyClass (string value) { return new MyClass(value); } MyClass(string value) { // Do something... _value = value; } public override string ToString() { // Do something... return _value; } } ``` Hence, I can use the class like this: ``` MyClass a = "Hello!"; ``` But in Raven DB it will just be stored like ``` "SomeProperty": {} ``` since it has no public properties. And it is quite useless. To solve this I would make the _value private member a public property instead, like this: ``` public string Value { get; set; } ``` and Raven DB will store ``` "SomeProperty": { "Value": "Hello!" } ``` and it will be deserializable. But I don't want this public property. Can I somehow make Raven DB serialize and deserialize the class as was it would a string? Like: ``` "SomeProperty": "Hello!" ```

Original source