I'm reading a large object which contains thousands of sub-objects that I don't want to deserialize into full C# objects (for performance reasons) but just load them as dummy wrapper objects to keep their JSON as strings (for later phase where they are serialized again).
So far I have the following concept code (not final), but I would prefer to avoid the JObject creation and just read somehow the sub-objects string straight from the reader into the wrapper json member string.
Is there an easy way to do so without switching over all possible tokens and reading them one by one?
namespace Playground
{
[JsonConverter(typeof(ObjectJsonWrapperConverter))]
public class ObjectJsonWrapper
{
public string Json;
}
public class ObjectJsonWrapperConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteRawValue(((ObjectJsonWrapper)value).Json);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var wrapper=new ObjectJsonWrapper();
if (reader.TokenType == JsonToken.Null)
{
return null;
}
else if (reader.TokenType == JsonToken.StartObject)
{
var obj=JsonConvert.SerializeObject(JObject.Load(reader));
wrapper.Json = obj;
return wrapper;
}
else
{
throw new ArgumentException("Bad parsing");
}
}
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
}
}
- Already saw the other question but it doesn't answer my needs