Deserialize JSON to multiple properties - c#

I am programming against a third party API which returns JSON data, but the format can be a little strange. Certain properties can either be an object (which contains an Id property), or a string (which is the Id of the object). For example, both of the following are valid:
{
ChildObject: 'childobjectkey1'
}
and
{
ChildObject: {
Id: 'childobjectkey1',
// (other properties)
}
}
I'm trying to deserialize this using JSON.net into a strongly typed class, but haven't had much luck so far. My best idea was to serialise it to two properties, one a string and the other an object, and to use a custom JsonConverter for each to allow for the variable behaviour:
public abstract class BaseEntity
{
public string Id { get; set; }
}
public class ChildObject : BaseEntity { }
public class MyObject
{
[JsonProperty("ChildObject")]
[JsonConverter(typeof(MyCustomIdConverter))]
public string ChildObjectId { get; set; }
[JsonProperty("ChildObject")]
[JsonConverter(typeof(MyCustomObjectConverter))]
public ChildObject ChildObject { get; set; }
}
However, setting the JsonProperty attribute on two properties with the same PropertyName causes the exception:
Newtonsoft.Json.JsonSerializationException: A member with the name
'ChildObject' already exists on '.....'.
Use the JsonPropertyAttribute to specify another name.
I'm fairly sure the JsonConverter approach will work if I can get over this hurdle - I suspect the error is there because the JsonProperty attribute is used for Serialization as well as Deserialization. In this instance I have no interest in Serializing this class - it will only ever be used as the target for Deserialization.
I have no control over the remote end (it's a third party API), but I would like to be able to achieve this serialisation. I don't mind if it's using the approach I've started on, or one I've not thought of yet.
This question is also related, but there were no answers.

Try this (extend it with some thorough validation if you'll be using it in your code):
public class MyObject
{
public ChildObject MyChildObject;
public string MyChildObjectId;
[JsonProperty("ChildObject")]
public object ChildObject
{
get
{
return MyChildObject;
}
set
{
if (value is JObject)
{
MyChildObject = ((JToken)value).ToObject<ChildObject>();
MyChildObjectId = MyChildObject.Id;
}
else
{
MyChildObjectId = value.ToString();
MyChildObject = null;
}
}
}
}

Rather than creating two separate converters for each of the fields, it would be wise to create a single converter for the "main" property and link the other one to it. ChildObjectId is derived from the ChildObject.
public class MyObject
{
[JsonIgnore]
public string ChildObjectId
{
get { return ChildObject.Id; }
// I would advise against having a setter here
// you should only allow changes through the object only
set { ChildObject.Id = value; }
}
[JsonConverter(typeof(MyObjectChildObjectConverter))]
public ChildObject ChildObject { get; set; }
}
Now to convert the ChildObject can be a bit of a challenge. There are two possible representations of the object: a string or an object. Determine what representation you have and perform the conversion.
public class MyObjectChildObjectConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ChildObject);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var obj = serializer.Deserialize<JToken>(reader);
switch (obj.Type)
{
case JTokenType.Object:
return ReadAsObject(obj as JObject);
case JTokenType.String:
return ReadAsString((string)(JValue)obj);
default:
throw new JsonSerializationException("Unexpected token type");
}
}
private object ReadAsObject(JObject obj)
{
return obj.ToObject<ChildObject>();
}
private object ReadAsString(string str)
{
// do a lookup for the actual object or whatever here
return new ChildObject
{
Id = str,
};
}
}

Here is what I would do in this situation.
Only have a single property in your parent class for the child object, and make it of type ChildObject
Create a custom JsonConverter which can inspect the JSON and either:
deserialize the full instance of the child object if the data is present, or
create a new instance of the child object and set its ID, leaving all other properties blank. (Or you could do as Jeff Mercado suggested and have the converter load the object from a database based on the ID, if that applies to your situation.)
Optionally, put a property on the child object indicating whether it is fully populated. The converter can set this property during deserialization.
After deserialization, if there was a ChildObject property in the JSON (with either an ID or a full object value), you are guaranteed to have a ChildObject instance and you can get its ID from it; otherwise, if there was no ChildObject property in the JSON, the ChildObject property in the parent class will be null.
Below is a full working example to demonstrate. In this example, I modified the parent class to include three separate instances of the ChildObject to show the different possibilities in the JSON (string ID only, full object and neither present). They all use the same converter. I also added a Name property and an IsFullyPopulated property to the ChildObject class.
Here are the DTO classes:
public abstract class BaseEntity
{
public string Id { get; set; }
}
public class ChildObject : BaseEntity
{
public string Name { get; set; }
public bool IsFullyPopulated { get; set; }
}
public class MyObject
{
[JsonProperty("ChildObject1")]
[JsonConverter(typeof(MyCustomObjectConverter))]
public ChildObject ChildObject1 { get; set; }
[JsonProperty("ChildObject2")]
[JsonConverter(typeof(MyCustomObjectConverter))]
public ChildObject ChildObject2 { get; set; }
[JsonProperty("ChildObject3")]
[JsonConverter(typeof(MyCustomObjectConverter))]
public ChildObject ChildObject3 { get; set; }
}
Here is the converter:
class MyCustomObjectConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(ChildObject));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
ChildObject child = null;
if (token.Type == JTokenType.String)
{
child = new ChildObject();
child.Id = token.ToString();
child.IsFullyPopulated = false;
}
else if (token.Type == JTokenType.Object)
{
child = token.ToObject<ChildObject>();
child.IsFullyPopulated = true;
}
else if (token.Type != JTokenType.Null)
{
throw new JsonSerializationException("Unexpected token: " + token.Type);
}
return child;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}
Here is the test program to demonstrate the operation of the converter:
class Program
{
static void Main(string[] args)
{
string json = #"
{
""ChildObject1"":
{
""Id"": ""key1"",
""Name"": ""Foo Bar Baz""
},
""ChildObject2"": ""key2""
}";
MyObject obj = JsonConvert.DeserializeObject<MyObject>(json);
DumpChildObject("ChildObject1", obj.ChildObject1);
DumpChildObject("ChildObject2", obj.ChildObject2);
DumpChildObject("ChildObject3", obj.ChildObject3);
}
static void DumpChildObject(string prop, ChildObject obj)
{
Console.WriteLine(prop);
if (obj != null)
{
Console.WriteLine(" Id: " + obj.Id);
Console.WriteLine(" Name: " + obj.Name);
Console.WriteLine(" IsFullyPopulated: " + obj.IsFullyPopulated);
}
else
{
Console.WriteLine(" (null)");
}
Console.WriteLine();
}
}
And here is the output of the above:
ChildObject1
Id: key1
Name: Foo Bar Baz
IsFullyPopulated: True
ChildObject2
Id: key2
Name:
IsFullyPopulated: False
ChildObject3
(null)

Related

Class attribute for snake case properties [duplicate]

I am trying to bind my PascalCased c# model from snake_cased JSON in WebApi v2 (full framework, not dot net core).
Here's my api:
public class MyApi : ApiController
{
[HttpPost]
public IHttpActionResult DoSomething([FromBody]InputObjectDTO inputObject)
{
database.InsertData(inputObject.FullName, inputObject.TotalPrice)
return Ok();
}
}
And here's my input object:
public class InputObjectDTO
{
public string FullName { get; set; }
public int TotalPrice { get; set; }
...
}
The problem that I have is that the JSON looks like this:
{
"full_name": "John Smith",
"total_price": "20.00"
}
I am aware that I can use the JsonProperty attribute:
public class InputObjectDTO
{
[JsonProperty(PropertyName = "full_name")]
public string FullName { get; set; }
[JsonProperty(PropertyName = "total_price")]
public int TotalPrice { get; set; }
}
However my InputObjectDTO is huge, and there are many others like it too. It has hundreds of properties that are all snake cased, and it would be nice to not have to specify the JsonProperty attribute for each property. Can I make it to work "automatically"? Perhaps with a custom model binder or a custom json converter?
No need to reinvent the wheel. Json.Net already has a SnakeCaseNamingStrategy class to do exactly what you want. You just need to set it as the NamingStrategy on the DefaultContractResolver via settings.
Add this line to the Register method in your WebApiConfig class:
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() };
Here is a demo (console app) to prove the concept: https://dotnetfiddle.net/v5siz7
If you want to apply the snake casing to some classes but not others, you can do this by applying a [JsonObject] attribute specifying the naming strategy like so:
[JsonObject(NamingStrategyType = typeof(SnakeCaseNamingStrategy))]
public class InputObjectDTO
{
public string FullName { get; set; }
public decimal TotalPrice { get; set; }
}
The naming strategy set via attribute takes precedence over the naming strategy set via the resolver, so you can set your default strategy in the resolver and then use attributes to override it where needed. (There are three naming strategies included with Json.Net: SnakeCaseNamingStrategy, CamelCaseNamingStrategy and DefaultNamingStrategy.)
Now, if you want to deserialize using one naming strategy and serialize using a different strategy for the same class(es), then neither of the above solutions will work for you, because the naming strategies will be applied in both directions in Web API. So in in that case, you will need something custom like what is shown in #icepickle's answer to control when each is applied.
Well, you should be able to do it using a custom JsonConverter to read your data. Using the deserialization provided in Manojs' answer, you could create a DefaultContractResolver that would create a custom deserialization when the class has a SnakeCasedAttribute specified above.
The ContractResolver would look like the following
public class SnakeCaseContractResolver : DefaultContractResolver {
public new static readonly SnakeCaseContractResolver Instance = new SnakeCaseContractResolver();
protected override JsonContract CreateContract(Type objectType) {
JsonContract contract = base.CreateContract(objectType);
if (objectType?.GetCustomAttributes(true).OfType<SnakeCasedAttribute>().Any() == true) {
contract.Converter = new SnakeCaseConverter();
}
return contract;
}
}
The SnakeCaseConverter would be something like this?
public class SnakeCaseConverter : JsonConverter {
public override bool CanConvert(Type objectType) => objectType.GetCustomAttributes(true).OfType<SnakeCasedAttribute>().Any() == true;
private static string ConvertFromSnakeCase(string snakeCased) {
return string.Join("", snakeCased.Split('_').Select(part => part.Substring(0, 1).ToUpper() + part.Substring(1)));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
var target = Activator.CreateInstance( objectType );
var jobject = JObject.Load(reader);
foreach (var property in jobject.Properties()) {
var propName = ConvertFromSnakeCase(property.Name);
var prop = objectType.GetProperty(propName);
if (prop == null || !prop.CanWrite) {
continue;
}
prop.SetValue(target, property.Value.ToObject(prop.PropertyType, serializer));
}
return target;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
throw new NotImplementedException();
}
}
And then you could annotate your dto class using this attribute (which is just a placeholder)
[SnakeCased]
public class InputObjectDTO {
public string FullName { get; set; }
public int TotalPrice { get; set; }
}
and for reference, this is the used attribute
[AttributeUsage(AttributeTargets.Class)]
public class SnakeCasedAttribute : Attribute {
public SnakeCasedAttribute() {
// intended blank
}
}
One more thing to notice is that in your current form the JSON converter would throw an error ("20.00" is not an int), but I am going to guess that from here you can handle that part yourself :)
And for a complete reference, you could see the working version in this dotnetfiddle
You can add cusrom json converter code like below. This should allow you to specify property mapping.
public class ApiErrorConverter : JsonConverter
{
private readonly Dictionary<string, string> _propertyMappings = new Dictionary<string, string>
{
{"name", "error"},
{"code", "errorCode"},
{"description", "message"}
};
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return objectType.GetTypeInfo().IsClass;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
object instance = Activator.CreateInstance(objectType);
var props = objectType.GetTypeInfo().DeclaredProperties.ToList();
JObject jo = JObject.Load(reader);
foreach (JProperty jp in jo.Properties())
{
if (!_propertyMappings.TryGetValue(jp.Name, out var name))
name = jp.Name;
PropertyInfo prop = props.FirstOrDefault(pi =>
pi.CanWrite && pi.GetCustomAttribute<JsonPropertyAttribute>().PropertyName == name);
prop?.SetValue(instance, jp.Value.ToObject(prop.PropertyType, serializer));
}
return instance;
}
}
Then specify this attribute on your class.
This should work.
This blog explains the approach using console Application. https://www.jerriepelser.com/blog/deserialize-different-json-object-same-class/

Concat names of nested values of a json file in c# ex: ` "obj_nested1_nested2" : "text"

I'm looking for a way to transform this c# object:
class BaseClass
{
public string Value1 {get; set;}
public NestedObject nestedObject {get;set;}
}
class NestedObject
{
public string NestedValue1 {get; set;}
}
Into this json:
{
"Value1": "value1",
"NestedObject_NestedValue1": "nestedValue1"
}
By concatening the names of the nested parameters to their parent's name
Using normal serialization, this code: var json= JsonConvert.SerializeObject(baseClass);
Would instead return a json like this one:
{
"Value1": "value1",
"NestedObject": {
"NestedValue1": "nestedValue1"
}
}
I am sceptical about there being a way to deserialize a json like that back to an object tho.
Update:
As some asked what is the reason I'm trying to accomplish this:
The reason I asked this question is because I serialize this object to send as json metadata to a service that only allows referencing top level propreties in a way similar to this:
[Metadata_Value1] would return "value1"
However [Metadata_NestedObject_NestedValue1] doesn't work and there isn't any indication to there being a way to reference nested properties.
Taking this in consideration I hoped there would be some solution that would allow keeping the nested objects in my program but transforming them all to top properties when sending them to this service.
In the service I would then be able to do: [NestedObject_NestedValue1] and get the value "nestedValue1"
You can utilize a custom converter that looks like below :
public class NestedJsonConverter : JsonConverter
{
private readonly Type[] _types;
public NestedJsonConverter(params Type[] types)
{
_types = types;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);
if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
}
else
{
JObject o = (JObject)t;
writer.WriteStartObject();
void writeNested(JObject target, object source, string prefix)
{
target.Properties().ToList().ForEach(p =>
{
var prop = source.GetType().GetProperty(p.Name);
var value = prop.GetValue(source);
var prefixed = string.IsNullOrEmpty(prefix) ? p.Name : $"{prefix}_{p.Name}";
if (p.Value.Type == JTokenType.Object)
{
writeNested((JObject)p.Value, value, prefixed);
}
else if (p.Value.Type == JTokenType.Array)
{
// you may need a more advanced handling in array scenarios
var arr = (JArray)p.Value;
writer.WritePropertyName(prefixed);
arr.WriteTo(writer);
}
else
{
writer.WritePropertyName(prefixed);
writer.WriteValue(value);
}
}
);
}
writeNested(o, value, "");
writer.WriteEndObject();
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}
public override bool CanRead
{
get
{
return false;
}
}
public override bool CanConvert(Type objectType)
{
return _types.Any(t => t == objectType);
}
}
Dotnetfiddle

Failed to desirialize heterogeneous json data

So I am using Json.net to deserialize json data returned by REST APIs to a server, the data structure it returns is:
{ "keyXXX" : [[1,2,3,...]...], "last": 12345 }
The problem is the "key" is also part of data, it is not a field named "key", so I cannot use a class/struct, I had to use a IDictionary<string, int[][]> for the first part, but then the "last" part will throw an exception, because it is a single integer rather than an array of arrays.
This is what I've tried:
var dec = JsonConvert.DeserializeObject<IDictionary<string, int[][]>>(data);
This throws json exception:
Newtonsoft.Json.JsonSerializationException: Error converting value 1501555920 to type 'System.Decimal[][]'. Path 'last'.
//define a class
public class DPInfo
{
public decimal[][] points { get; set; }
public long last { get; set; }
}
var dec = JsonConvert.DeserializeObject<DPInfo>(data);
This will not work because the field name of the array is dynamic, so points will contains nothing after this.
Any way I can fix this?
You will need a custom JsonConverter to solve this. Here is one that should work:
public class DPInfoConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DPInfo);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject obj = JObject.Load(reader);
JProperty points = obj.Properties().FirstOrDefault(p => p.Name != "last");
DPInfo info = new DPInfo
{
key = points.Name, // remove this line if you don't need the key
points = points.Value.ToObject<decimal[][]>(),
last = (long)obj["last"]
};
return info;
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
To use it, just add a [JsonConverter] attribute to your class:
[JsonConverter(typeof(DPInfoConverter))]
public class DPInfo
{
public string key { get; set; } // remove this line if you don't need the key
public decimal[][] points { get; set; }
public long last { get; set; }
}
Then, deserialize as usual, and it should work:
string json = #"
{
""keyXXX"": [[1, 2, 3]],
""last"": 12345
}";
DPInfo info = JsonConvert.DeserializeObject<DPInfo>(json);
Fiddle: https://dotnetfiddle.net/7S6STp
You could use JObject.Parse to parse your string into a dynamic variable. It would at least ensure that the conversion from string to JSON succeeds, but then it would be up to you to validate that there is a value in each property before accessing it.
In your case the statement would look like this:
dynamic data = JObject.Parse("{ 'keyXXX' : [[1,2,3,...]...], 'last': 12345 }");

How to Omit Inherited Property From JSON.NET Serialization

If I have the following classes I want to serialize using JSON.NET:
[DataContract]
public class Thing
{
[DataMember(Name = "#context")]
public string Context => "http://schema.org"
}
[DataContract]
public class Organization : Thing
{
[DataMember(Name = "address")]
public Address Address { get; set; }
...
}
[DataContract]
public class Address : Thing
{
...
}
When I use JSON.NET to serialize an Organization I get:
{
"#context": "http://schema.org",
"address": {
"#context": "http://schema.org",
...
}
...
}
What is the most efficient way of ensuring that the #context property only appears in the top level Organization object and not in the Address object?
If Organization is the only top level descendant of Thing and also no fields of type Organization may appear in serialized objects, you may easily do this by defining ShouldSerializeContext in Thing as follows:
[DataContract]
public class Thing
{
[DataMember(Name = "#context")]
public string Context => "http://schema.org";
public bool ShouldSerializeContext() { return this is Organization; }
}
Demo: https://dotnetfiddle.net/GjmfbA
If any of the Thing's descendants may act as the root object, you may need to implement a custom converter. In WriteJson method of this converter, you may filter properties to be serialized. To remove the Context property from all but the root object check writer.Path, which will be an empty string for the root object:
[DataContract]
[JsonConverter(typeof(NoContextConverter))]
public class Thing
{
[DataMember(Name = "#context")]
public string Context => "http://schema.org";
}
// ...............
public class NoContextConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var props = value.GetType().GetProperties()
.Where(p => Attribute.IsDefined(p, typeof(DataMemberAttribute)))
.ToList();
if (writer.Path != "")
props.RemoveAll(p => p.Name == "Context");
writer.WriteStartObject();
foreach (var prop in props)
{
writer.WritePropertyName(prop.GetCustomAttribute<DataMemberAttribute>().Name);
serializer.Serialize(writer, prop.GetValue(value, null));
}
writer.WriteEndObject();
}
public override bool CanConvert(Type objectType)
{
return typeof(Thing).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Demo: https://dotnetfiddle.net/cIlXID
N.B. For some reason, dotnetfiddle.net does not allow to use DataContractAttribute and DataMemberAttribute from System.Runtime.Serialization so I had to comment out relevant lines in this demo.
While #DimitryEgorov's answer is probably the correct way to go, it uses reflection which makes it slow. In the solution below, I use StringBuilder to do a string replace on the final JSON.
private const string ContextPropertyJson = "\"#context\":\"http://schema.org\",";
public override string ToString() => RemoveAllButFirstContext(
JsonConvert.SerializeObject(this, new JsonSerializerSettings));
private static string RemoveAllButFirstContext(string json)
{
var stringBuilder = new StringBuilder(json);
var startIndex = ContextPropertyJson.Length + 1;
stringBuilder.Replace(
ContextPropertyJson,
string.Empty,
startIndex,
stringBuilder.Length - startIndex - 1);
return stringBuilder.ToString();
}

Peek JsonReader to read outside of JsonConverter's own object [duplicate]

I have a client which can call two different versions of a service.
One service only sends a single value:
{
"value" : { ... }
}
The second service always returns multiple values:
{
"values" : [
{ ... },
{ ... }
]
}
Ideally, I'd like to represent this with a single object in my client classes so the user never sees whether it's a single value or multiple values.
public class MyValues
{
public List<Stuff> Values { get; set; }
public Thing Other { get; set; }
}
I think that the only way I'll be able to accomplish this is with a custom JsonConverter class which I apply to MyValues, but I really only want to do something custom when I'm deserializing the property value. I can't seem to figure out if an IContractResolver would be a better way to go (e.g. somehow attach a phantom property to MyValues that deserializes value and puts it into Values.
If I create a custom converter, how to I tell it to deserialize everything else normally (e.g. if Other has an extra properties make sure they are handled appropriately, etc.)
Rather than writing a JsonConverter, you could make a set-only property Value on your MyValues, like so:
public class MyValues
{
[JsonProperty]
Stuff Value
{
set
{
(Values = Values ?? new List<Stuff>(1)).Clear();
Values.Add(value);
}
}
public List<Stuff> Values { get; set; }
public Thing Other { get; set; }
}
It could be public or private if marked with [JsonProperty]. In this case Json.NET will call the Value setter if the singleton "value" property is encountered in the JSON, and call the Values setter if the array "values" property is encountered. Since the property is set-only only the array property will be re-serialized.
To make a custom JsonConverter that has special processing for a few properties of a type but uses default processing for the remainder, you can load the JSON into a JObject, detach and process the custom properties, then populate the remainder from the JObject with JsonSerializer.Populate(), like so:
class MyValuesConverter : CustomPropertyConverterBase<MyValues>
{
protected override void ProcessCustomProperties(JObject obj, MyValues value, JsonSerializer serializer)
{
// Remove the value property for manual deserialization, and deserialize
var jValue = obj.GetValue("value", StringComparison.OrdinalIgnoreCase).RemoveFromLowestPossibleParent();
if (jValue != null)
{
(value.Values = value.Values ?? new List<Stuff>()).Clear();
value.Values.Add(jValue.ToObject<Stuff>(serializer));
}
}
}
public abstract class CustomPropertyConverterBase<T> : JsonConverter where T : class
{
public override bool CanConvert(Type objectType)
{
return typeof(T).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var jObj = JObject.Load(reader);
var contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(objectType);
var value = existingValue as T ?? (T)contract.DefaultCreator();
ProcessCustomProperties(jObj, value, serializer);
// Populate the remaining properties.
using (var subReader = jObj.CreateReader())
{
serializer.Populate(subReader, value);
}
return value;
}
protected abstract void ProcessCustomProperties(JObject obj, T value, JsonSerializer serializer);
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
public static class JsonExtensions
{
public static JToken RemoveFromLowestPossibleParent(this JToken node)
{
if (node == null)
return null;
var contained = node.AncestorsAndSelf().Where(t => t.Parent is JContainer && t.Parent.Type != JTokenType.Property).FirstOrDefault();
if (contained != null)
contained.Remove();
// Also detach the node from its immediate containing property -- Remove() does not do this even though it seems like it should
if (node.Parent is JProperty)
((JProperty)node.Parent).Value = null;
return node;
}
}

Categories