Reputation: 14108
I have a class that I need to serialize to JSON in Couchbase in a custom manner. So I do something like:
var converters = new[] { new CustomConverter() };
var json = JsonConvert.SerializeObject(myObject, jsonConverters.ToArray());
var result = _couchbaseClient.ExecuteStore(StoreMode.Add, id, json);
But how can I deserialize this again, using the CustomConverter
? I want to retrieve the JSON and do the deserializing myself. I realize there's a ExecuteGet
method, but I'd like to work via a view.
Is this possible?
I've tried add my converter to the JsonConvert.DefaultSettings
but it doesn't seem to use it. I keep getting a JsonSerializationException
. I'm guessing Couchbase resets the settings, or uses its own.
Upvotes: 1
Views: 1265
Reputation: 14108
Digging into the Couchbase code, I found it. Couchbase has some extension methods which use their own settings. You can see this on GitHub:
public static class CouchbaseClientExtensions
{
public static JsonSerializerSettings JsonSerializerSettings;
static CouchbaseClientExtensions()
{
JsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new DocumentIdContractResolver()
};
}
// ...
private static string SerializeObject(object value)
{
var json = JsonConvert.SerializeObject(value,
Formatting.None,
JsonSerializerSettings);
return json;
}
}
So you can add your own converters like this:
foreach (var jsonConverter in jsonConverters)
{
if (!CouchbaseClientExtensions
.JsonSerializerSettings
.Converters.Any(x => x.GetType() == jsonConverter.GetType()))
{
CouchbaseClientExtensions
.JsonSerializerSettings
.Converters.Add(jsonConverter);
}
}
Upvotes: 1