In order to increase performance, I have cached the result of a larger operation as JSON in a table - together with a key column to determine which row(s) to return. So the
Assuming you have a structure like this for serializing:
public class Record
{
[JsonProperty("id")]
public int Id
{
get;
set;
}
[JsonProperty("json")]
[JsonConverter(typeof(SpecialJsonConverter))]
public string Json
{
get;
set;
}
}
And you use code like this for serialization:
var data = new []
{
new Record() { Id=1, Json = "{\"property\":\"data\"}" },
new Record() { Id=2, Json = "{\"property\":\"data2\", \"property2\":[1, 2, 3]}" }
};
var serialized = JsonConvert.SerializeObject(data);
Console.WriteLine(serialized);
All you need is to write a proper converter for the Json property. Luckily there is a method WriteToken in the JsonWriter class that could serve our needs:
public sealed class SpecialJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var reader = new JsonTextReader(new StringReader(value.ToString()));
writer.WriteToken(reader);
}
}