I am looking for a way to partially serialize a model to Json using Json.Net. How the partial serialization should look like I want to define on the property of a parent object. So partial serialization can look different for different parent models. To illustrate what I want here some code:
private class MyTestObject
{
[SerializeOnly("TestValue1")]
[SerializeOnly("TestValue3")]
public ComplexTestObject Property1 { get; set; }
public MyTestObject()
{
Property1 = new ComplexTestObject();
}
}
private class ComplexTestObject
{
public string TestValue1 { get; set; }
public string TestValue2 { get; set; }
public string TestValue3 { get; set; }
public ComplexTestObject()
{
TestValue1 = "value1";
TestValue2 = "value2";
TestValue3 = "value3";
}
}
Now when I serialize an instance of class MyTestObject I want to get the following Json:
{
"Property1" : {
"TestValue1" : "value1",
"TestValue3" : "value3",
}
}
You can see that SerializeOnly is used to define which properties are to be serialized.
To achieve this I can create a SerializeOnlyAttribute. When trying to use this in a custom Serialization ContractResolver I can only see the attributes of the specific member and so I cannot see any SerializeOnlyAttribute because they reside on the parent.
Is there a simple way to achieve the desired behavior with Json.Net? It might be possible writing a custom JsonConverter but how can this be built such that only handling the attributes is covered and still the default converters are used?
You can solve this in two parts:
Create a custom JsonConverter that can accept a list of names of properties to serialize.
Create a custom ContractResolver that looks for properties that have at least one [SerializeOnly] attribute applied, and apply the custom converter to those properties, passing the list of child property names gathered from the applied attributes.
Here is what the resolver might look like:
class CustomResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> props = base.CreateProperties(type, memberSerialization);
foreach (JsonProperty prop in props)
{
if (!prop.PropertyType.IsPrimitive && prop.PropertyType != typeof(string))
{
PropertyInfo pi = type.GetProperty(prop.UnderlyingName);
if (pi != null && pi.CanRead)
{
var childPropertiesToSerialize = pi.GetCustomAttributes<SerializeOnly>()
.Select(att => att.PropertyName);
if (childPropertiesToSerialize.Any())
{
prop.Converter = new CustomConverter(childPropertiesToSerialize);
}
}
}
}
return props;
}
}
And here is the converter:
class CustomConverter : JsonConverter
{
private HashSet<string> propertiesToSerialize;
public CustomConverter(IEnumerable<string> propertiesToSerialize)
{
this.propertiesToSerialize = new HashSet<string>(propertiesToSerialize);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
foreach (PropertyInfo prop in value.GetType().GetProperties())
{
if (prop.CanRead && propertiesToSerialize.Contains(prop.Name))
{
writer.WritePropertyName(prop.Name);
serializer.Serialize(writer, prop.GetValue(value));
}
}
writer.WriteEndObject();
}
public override bool CanRead
{
get { return false; }
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
}
Demo:
class Program
{
static void Main(string[] args)
{
var test = new MyTestObject();
var settings = new JsonSerializerSettings();
settings.ContractResolver = new CustomResolver();
settings.Formatting = Formatting.Indented;
var json = JsonConvert.SerializeObject(test, settings);
Console.WriteLine(json);
}
class MyTestObject
{
[SerializeOnly("TestValue1")]
[SerializeOnly("TestValue3")]
public ComplexTestObject Property1 { get; set; }
[SerializeOnly("TestValue2")]
public ComplexTestObject Property2 { get; set; }
public MyTestObject()
{
Property1 = new ComplexTestObject();
Property2 = new ComplexTestObject();
}
}
class ComplexTestObject
{
public string TestValue1 { get; set; }
public string TestValue2 { get; set; }
public string TestValue3 { get; set; }
public ComplexTestObject()
{
TestValue1 = "value1";
TestValue2 = "value2";
TestValue3 = "value3";
}
}
}
Output:
{
"Property1": {
"TestValue1": "value1",
"TestValue3": "value3"
},
"Property2": {
"TestValue2": "value2"
}
}
Fiddle: https://dotnetfiddle.net/Fj7QcW
C# already has an attribute to help you with this
https://msdn.microsoft.com/en-us/library/system.web.script.serialization.scriptignoreattribute(v=vs.110).aspx
[ScriptIgnore]
public string MyParam{get;set;}
Related
how can I deserialize below json structure using newtonsoft json.net in .net.
{
"users" : {
"parentname":"test",
"100034" : {
"name" : "tom",
"state" : "WA",
"id" : "cedf-c56f-18a4-4b1"
},
"10045" : {
"name" : "steve",
"state" : "NY",
"id" : "ebb2-92bf-3062-7774"
},
"12345" : {
"name" : "mike",
"state" : "MA",
"id" : "fb60-b34f-6dc8-aaf7"
}
}
}
I tried below code but its not working. I got error 'Error converting value "test" to type 'ConsoleApplication2.User'. Path 'users.parentname', line 5, position 35.'
class Program
{
static void Main(string[] args)
{
string json = #"
{
""users"": {
""parentname"":""test"",
""10045"": {
""name"": ""steve"",
""state"": ""NY"",
""id"": ""ebb2-92bf-3062-7774""
}
}
}";
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
}
}
class RootObject
{
public string ParentName { get; set; }
public Dictionary<string, User> users { get; set; }
}
class User
{
public string name { get; set; }
public string state { get; set; }
public string id { get; set; }
public string ParentName { get; set; }
}
Please suggest.
You have a couple problems:
Your JSON has an extra level of nesting, with the root object containing a single property "users":
{
"users" : { ... }
}
Your data model needs to reflect this.
Your "users" object has a mixture of known and unknown property names. The question Deserialize json with known and unknown fields addresses a similar situation, however in your case your unknown properties always have a fixed schema and their values should be deserialized into a dictionary of POCOs -- specifically the User class. Therefore the answers there don't quite meet your needs, nor does the build-in functionality [JsonExtensionData].
The following converter allows for unknown properties to be deserialized into a typed container, rather than into an dictionary of arbitrary types:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class JsonTypedExtensionDataAttribute : Attribute
{
}
public class TypedExtensionDataConverter<TObject> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(TObject).IsAssignableFrom(objectType);
}
JsonProperty GetExtensionJsonProperty(JsonObjectContract contract)
{
try
{
return contract.Properties.Where(p => p.AttributeProvider.GetAttributes(typeof(JsonTypedExtensionDataAttribute), false).Any()).Single();
}
catch (InvalidOperationException ex)
{
throw new JsonSerializationException(string.Format("Exactly one property with JsonTypedExtensionDataAttribute is required for type {0}", contract.UnderlyingType), ex);
}
}
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 extensionJsonProperty = GetExtensionJsonProperty(contract);
var extensionJProperty = (JProperty)null;
for (int i = jObj.Count - 1; i >= 0; i--)
{
var property = (JProperty)jObj.AsList()[i];
if (contract.Properties.GetClosestMatchProperty(property.Name) == null)
{
if (extensionJProperty == null)
{
extensionJProperty = new JProperty(extensionJsonProperty.PropertyName, new JObject());
jObj.Add(extensionJProperty);
}
((JObject)extensionJProperty.Value).Add(property.RemoveFromLowestPossibleParent());
}
}
var value = existingValue ?? contract.DefaultCreator();
using (var subReader = jObj.CreateReader())
serializer.Populate(subReader, value);
return value;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(value.GetType());
var extensionJsonProperty = GetExtensionJsonProperty(contract);
JObject jObj;
using (new PushValue<bool>(true, () => Disabled, (canWrite) => Disabled = canWrite))
{
jObj = JObject.FromObject(value, serializer);
}
var extensionValue = (jObj[extensionJsonProperty.PropertyName] as JObject).RemoveFromLowestPossibleParent();
if (extensionValue != null)
{
for (int i = extensionValue.Count - 1; i >= 0; i--)
{
var property = (JProperty)extensionValue.AsList()[i];
jObj.Add(property.RemoveFromLowestPossibleParent());
}
}
jObj.WriteTo(writer);
}
[ThreadStatic]
static bool disabled;
// Disables the converter in a thread-safe manner.
bool Disabled { get { return disabled; } set { disabled = value; } }
public override bool CanWrite { get { return !Disabled; } }
public override bool CanRead { get { return !Disabled; } }
}
public struct PushValue<T> : IDisposable
{
Action<T> setValue;
T oldValue;
public PushValue(T value, Func<T> getValue, Action<T> setValue)
{
if (getValue == null || setValue == null)
throw new ArgumentNullException();
this.setValue = setValue;
this.oldValue = getValue();
setValue(value);
}
#region IDisposable Members
// By using a disposable struct we avoid the overhead of allocating and freeing an instance of a finalizable class.
public void Dispose()
{
if (setValue != null)
setValue(oldValue);
}
#endregion
}
public static class JsonExtensions
{
public static TJToken RemoveFromLowestPossibleParent<TJToken>(this TJToken node) where TJToken : JToken
{
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;
}
public static IList<JToken> AsList(this IList<JToken> container) { return container; }
}
Then use it in your classes as follows:
class RootObject
{
[JsonProperty("users")]
public Users Users { get; set; }
}
[JsonConverter(typeof(TypedExtensionDataConverter<Users>))]
class Users
{
public Users()
{
this.UserTable = new Dictionary<string, User>();
}
[JsonProperty("parentname")]
public string ParentName { get; set; }
[JsonTypedExtensionData]
public Dictionary<string, User> UserTable { get; set; }
}
class User
{
public string name { get; set; }
public string state { get; set; }
public string id { get; set; }
}
I wrote the converter in a fairly general way so it can be reused. A converter that is hardcoded for the Users type would require less code.
Your Json has to look like this:
{
"ParentName":"test",
"users":{
"10045":{
"name":"steve",
"state":"NY",
"id":"ebb2-92bf-3062-7774",
"ParentName":"someOtherName"
}
}
}
In order to deserialize it with your given class structure:
class RootObject
{
public string ParentName { get; set; }
public Dictionary<string, User> users { get; set; }
}
class User
{
public string name { get; set; }
public string state { get; set; }
public string id { get; set; }
public string ParentName { get; set; }
}
Now you can deserialize the Json string with:
var root = JsonConvert.DeserializeObject<RootObject>(json);
I create a class for define my request, I don't get the accepted JSON string
I define this object:
public class Request
{
public Var_Args[] var_args { get; set; }
}
public class Var_Args
{
public object title { get; set; }
public object owner { get; set; }
}
when I convert it to json, I get the following string:
{"requests":[{"var_args":[{"title":"Test","owner":"skaner"}]}]}
how can I define the class, for get the accepted json string:
{"requests":[{"var_args":[{"title":"Test"},{"owner":"skaner"}]}]}
You can write a custom JSON converter that can serialize every property of an object (of a known type) into a different JSON object.
public class PropertyAsObjectConverter : JsonConverter
{
private readonly Type[] _types;
public PropertyAsObjectConverter(params Type[] types)
{
_types = types;
}
public override bool CanConvert(Type objectType)
{
return _types.Any(t => t == objectType);
}
public override bool CanRead
{
get { return false; }
}
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 properties = value.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance);
foreach(var property in properties)
{
var name = property.Name;
var attrs = property.GetCustomAttributes(typeof(JsonPropertyAttribute));
if(attrs != null)
{
if (attrs.FirstOrDefault() is JsonPropertyAttribute attr)
name = attr.PropertyName;
}
writer.WriteStartObject();
writer.WritePropertyName(name);
serializer.Serialize(writer, property.GetValue(value));
writer.WriteEndObject();
}
}
}
This implements only the serialization, but you can extend it to support deserialization too. You can also extend it to serialize fields should you need that.
You can then define your classes as follows. Notice that I am using JsonPropertyAttribute here to specify the name in the serialized JSON.
public class Content
{
[JsonProperty("requests")]
public Request Value { get; set; }
}
public class Request
{
[JsonProperty("var_args")]
public VarArgs[] Arguments { get; set; }
}
public class VarArgs
{
[JsonProperty("title")]
public object Title { get; set; }
[JsonProperty("owner")]
public object Owner { get; set; }
}
This is how you can use it:
static void Main(string[] args)
{
var request = new Content()
{
Value = new Request()
{
Arguments = new VarArgs[]
{
new VarArgs()
{
Title = "Test",
Owner = "Skaner",
}
}
}
};
var text = JsonConvert.SerializeObject(
request,
Formatting.None,
new PropertyAsObjectConverter(typeof(VarArgs)));
Console.WriteLine(text);
}
The output for this sample is the one you expect:
{"requests":{"var_args":[{"title":"Test"},{"owner":"Skaner"}]}}
You could use a custom JsonConverter like the below.
It takes the Var_Args object and splits it in two different JObject which correspond to two different JSON objects.
public class VarArgsConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var obj = (JObject)JToken.FromObject(value);
var objTitle = new JObject();
objTitle.Add("title", obj.GetValue("title"));
var objOwner = new JObject();
objOwner.Add("owner", obj.GetValue("owner"));
objTitle.WriteTo(writer);
objOwner.WriteTo(writer);
}
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 objectType == typeof(Var_Args);
}
}
public class Wrapper
{
[JsonProperty("requests")]
public Request Requests { get; set; }
}
public class Request
{
public Var_Args[] var_args { get; set; }
}
public class Var_Args
{
public object title { get; set; }
public object owner { get; set; }
}
Then use it:
var wrapper = new Wrapper();
var request = new Request();
request.var_args = new Var_Args[] {
new Var_Args(){ title = "Test", owner = "skaner" },
new Var_Args(){ title = "Test2", owner = "skaner2" }
};
wrapper.Requests = request;
var serialized = JsonConvert.SerializeObject(wrapper, new VarArgsConverter());
Output
{"requests":{"var_args":[{"title":"Test"},{"owner":"skaner"},{"title":"Test2"},{"owner":"skaner2"}]}}
Note: I'm using the Wrapper class just to produce the requested JSON.
If you don't want to specify the converter each time, you can register your converter globally. Please see this answer which explains how you can do that. So, the serializer will use your custom JsonConverter every time you try to serialize a Var_Args object.
If you register the JsonConvert globally you can use:
var serialized = JsonConvert.SerializeObject(wrapper);
You can use System.Reflection to redefine Var_Args as an implementation of the IEnumerable<Dictionary<string,object>> interface by adding two methods to the class:
public class Var_Args : IEnumerable<Dictionary<string,object>>
{
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<Dictionary<string,object>> GetEnumerator()
{
var Properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var Property in Properties)
{
var Entry = new Dictionary<string,object>();
Entry.Add(Property.Name, Property.GetValue(this));
yield return Entry;
}
}
public object title { get; set; }
public object owner { get; set; }
}
While Reflection may be regarded as slow, there is a technique you can use to statically compile an IEnumerable at runtime so that the reflection only occurs once for the definition of the class, like this:
public class Var_Args : IEnumerable<Dictionary<string,object>>
{
private struct PropertyList<T>
{
public static readonly List<Func<T,Dictionary<string,object>>> PropertyGetters;
static PropertyList()
{
PropertyGetters = new List<Func<T,Dictionary<string,object>>>();
var Properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var Property in Properties)
{
var Args = new [] { Expression.Parameter(typeof(T)) };
var Key = Property.Name;
var Value = Expression.Property(Args[0], Property);
Func<T,object> Get = Expression.Lambda<Func<T,object>>(Value, Args).Compile();
PropertyGetters.Add(obj =>
{
var entry = new Dictionary<string,object>();
entry.Add(Key, Get(obj));
return entry;
});
}
}
}
protected static IEnumerable<Dictionary<string,object>> GetPropertiesAsEntries<T>(T obj)
{
return PropertyList<T>.PropertyGetters.Select(f => f(obj));
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<Dictionary<string,object>> GetEnumerator()
{
return GetPropertiesAsEntries(this).GetEnumerator();
}
public object title { get; set; }
public object owner { get; set; }
}
I have two JSON files (that I can't change the format for) in the following format:
Main file -
[
{
"Name":"XYZ",
"UnitReferenceId":1
},
{
"Name":"ABC",
"UnitReferenceId":2
}
]
The lookup/reference JSON file -
[
{
"UnitReferenceId":1,
"Units":[
{
"Unit":"mg",
"Scale":1
},
{
"Unit":"gm",
"Scale":1000
},
{
"Unit":"kg",
"Scale":1000000
}
]
},
{
"UnitReferenceId":2,
"Units":[
{
"Unit":"mm",
"Scale":1
},
{
"Unit":"m",
"Scale":1000
},
{
"Unit":"km",
"Scale":1000000
}
]
}
]
How would I go about deserializing that into C# classes using Newtonsoft JSON into something like:
public class Widget
{
public string Name {get; set;}
public UnitReference UnitReference { get; set; }
}
public class UnitReference
{
public long UnitReferenceId { get; set; }
public List<Unit> Units { get; set; }
}
public class Unit
{
[JsonProperty("Unit")]
public string UnitValue { get; set; }
public long Scale { get; set; }
}
Any help would be greatly appreciated!
You can do this by reading your two JSON files as follows:
First read the lookup/reference JSON file for UnitReference as a List<UnitReference>, then convert to a Dictionary<long, UnitReference> lookup table.
Next, read the main file using a custom JsonConverter for Widget that is passed the Dictionary<long, UnitReference> lookup table and can translate between UnitReferenceId and UnitReference during reading and writing.
Thus your classes would look like the following:
public class UnitReference
{
readonly long unitReferenceId;
public UnitReference(long unitReferenceId)
{
this.unitReferenceId = unitReferenceId;
}
public long UnitReferenceId { get { return unitReferenceId; } }
public List<Unit> Units { get; set; }
}
public class Unit
{
[JsonProperty("Unit")]
public string UnitValue { get; set; }
public long Scale { get; set; }
}
public class Widget
{
public string Name { get; set; }
public UnitReference UnitReference { get; set; }
}
(My only modification was to make UnitReferenceId be read-only so that it safely could be used as a dictionary key.)
Then, define the following converter:
public class WidgetConverter : CustomPropertyConverterBase<Widget>
{
readonly IDictionary<long, UnitReference> units;
public WidgetConverter(IDictionary<long, UnitReference> units)
{
this.units = units;
}
protected override void ReadCustomProperties(JObject obj, Widget value, JsonSerializer serializer)
{
var id = (long?)obj.GetValue("UnitReferenceId", StringComparison.OrdinalIgnoreCase);
if (id != null)
value.UnitReference = units[id.Value];
}
protected override bool ShouldSerialize(JsonProperty property, object value)
{
if (property.UnderlyingName == nameof(Widget.UnitReference))
return false;
return base.ShouldSerialize(property, value);
}
protected override void WriteCustomProperties(JsonWriter writer, Widget value, JsonSerializer serializer)
{
if (value.UnitReference != null)
{
writer.WritePropertyName("UnitReferenceId");
writer.WriteValue(value.UnitReference.UnitReferenceId);
}
}
}
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();
ReadCustomProperties(jObj, value, serializer);
// Populate the remaining properties.
using (var subReader = jObj.CreateReader())
{
serializer.Populate(subReader, value);
}
return value;
}
protected abstract void ReadCustomProperties(JObject obj, T value, JsonSerializer serializer);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(value.GetType());
writer.WriteStartObject();
foreach (var property in contract.Properties.Where(p => ShouldSerialize(p, value)))
{
var propertyValue = property.ValueProvider.GetValue(value);
if (propertyValue == null && serializer.NullValueHandling == NullValueHandling.Ignore)
continue;
writer.WritePropertyName(property.PropertyName);
serializer.Serialize(writer, propertyValue);
}
WriteCustomProperties(writer, (T)value, serializer);
writer.WriteEndObject();
}
protected virtual bool ShouldSerialize(JsonProperty property, object value)
{
return property.Readable && !property.Ignored && (property.ShouldSerialize == null || property.ShouldSerialize(value));
}
protected abstract void WriteCustomProperties(JsonWriter writer, T value, JsonSerializer serializer);
}
And deserialize as follows:
var units = JsonConvert.DeserializeObject<List<UnitReference>>(unitsJsonString)
.ToDictionary(u => u.UnitReferenceId);
var settings = new JsonSerializerSettings
{
Converters = { new WidgetConverter(units) },
};
var widgets = JsonConvert.DeserializeObject<List<Widget>>(widgetsJsonString, settings);
Notes:
Here I am deserializing from JSON strings for demo purposes, but you can deserialize directly from your file(s) as shown in Deserialize JSON from a file.
The base class CustomPropertyConverterBase<T> for WidgetConverter automatically reads and writes all properties for the object being (de)serialized. WidgetConverter then overrides this behavior for the UnitReference property only, avoiding the necessity to manually serialize all the remaining properties of Widget.
Sample fiddle.
I use json2csharp for creating my classes quickly. If you have to implement it in code, see JSON C# Class Generator Project.
how can I deserialize below json structure using newtonsoft json.net in .net.
{
"users" : {
"parentname":"test",
"100034" : {
"name" : "tom",
"state" : "WA",
"id" : "cedf-c56f-18a4-4b1"
},
"10045" : {
"name" : "steve",
"state" : "NY",
"id" : "ebb2-92bf-3062-7774"
},
"12345" : {
"name" : "mike",
"state" : "MA",
"id" : "fb60-b34f-6dc8-aaf7"
}
}
}
I tried below code but its not working. I got error 'Error converting value "test" to type 'ConsoleApplication2.User'. Path 'users.parentname', line 5, position 35.'
class Program
{
static void Main(string[] args)
{
string json = #"
{
""users"": {
""parentname"":""test"",
""10045"": {
""name"": ""steve"",
""state"": ""NY"",
""id"": ""ebb2-92bf-3062-7774""
}
}
}";
RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
}
}
class RootObject
{
public string ParentName { get; set; }
public Dictionary<string, User> users { get; set; }
}
class User
{
public string name { get; set; }
public string state { get; set; }
public string id { get; set; }
public string ParentName { get; set; }
}
Please suggest.
You have a couple problems:
Your JSON has an extra level of nesting, with the root object containing a single property "users":
{
"users" : { ... }
}
Your data model needs to reflect this.
Your "users" object has a mixture of known and unknown property names. The question Deserialize json with known and unknown fields addresses a similar situation, however in your case your unknown properties always have a fixed schema and their values should be deserialized into a dictionary of POCOs -- specifically the User class. Therefore the answers there don't quite meet your needs, nor does the build-in functionality [JsonExtensionData].
The following converter allows for unknown properties to be deserialized into a typed container, rather than into an dictionary of arbitrary types:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class JsonTypedExtensionDataAttribute : Attribute
{
}
public class TypedExtensionDataConverter<TObject> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(TObject).IsAssignableFrom(objectType);
}
JsonProperty GetExtensionJsonProperty(JsonObjectContract contract)
{
try
{
return contract.Properties.Where(p => p.AttributeProvider.GetAttributes(typeof(JsonTypedExtensionDataAttribute), false).Any()).Single();
}
catch (InvalidOperationException ex)
{
throw new JsonSerializationException(string.Format("Exactly one property with JsonTypedExtensionDataAttribute is required for type {0}", contract.UnderlyingType), ex);
}
}
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 extensionJsonProperty = GetExtensionJsonProperty(contract);
var extensionJProperty = (JProperty)null;
for (int i = jObj.Count - 1; i >= 0; i--)
{
var property = (JProperty)jObj.AsList()[i];
if (contract.Properties.GetClosestMatchProperty(property.Name) == null)
{
if (extensionJProperty == null)
{
extensionJProperty = new JProperty(extensionJsonProperty.PropertyName, new JObject());
jObj.Add(extensionJProperty);
}
((JObject)extensionJProperty.Value).Add(property.RemoveFromLowestPossibleParent());
}
}
var value = existingValue ?? contract.DefaultCreator();
using (var subReader = jObj.CreateReader())
serializer.Populate(subReader, value);
return value;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(value.GetType());
var extensionJsonProperty = GetExtensionJsonProperty(contract);
JObject jObj;
using (new PushValue<bool>(true, () => Disabled, (canWrite) => Disabled = canWrite))
{
jObj = JObject.FromObject(value, serializer);
}
var extensionValue = (jObj[extensionJsonProperty.PropertyName] as JObject).RemoveFromLowestPossibleParent();
if (extensionValue != null)
{
for (int i = extensionValue.Count - 1; i >= 0; i--)
{
var property = (JProperty)extensionValue.AsList()[i];
jObj.Add(property.RemoveFromLowestPossibleParent());
}
}
jObj.WriteTo(writer);
}
[ThreadStatic]
static bool disabled;
// Disables the converter in a thread-safe manner.
bool Disabled { get { return disabled; } set { disabled = value; } }
public override bool CanWrite { get { return !Disabled; } }
public override bool CanRead { get { return !Disabled; } }
}
public struct PushValue<T> : IDisposable
{
Action<T> setValue;
T oldValue;
public PushValue(T value, Func<T> getValue, Action<T> setValue)
{
if (getValue == null || setValue == null)
throw new ArgumentNullException();
this.setValue = setValue;
this.oldValue = getValue();
setValue(value);
}
#region IDisposable Members
// By using a disposable struct we avoid the overhead of allocating and freeing an instance of a finalizable class.
public void Dispose()
{
if (setValue != null)
setValue(oldValue);
}
#endregion
}
public static class JsonExtensions
{
public static TJToken RemoveFromLowestPossibleParent<TJToken>(this TJToken node) where TJToken : JToken
{
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;
}
public static IList<JToken> AsList(this IList<JToken> container) { return container; }
}
Then use it in your classes as follows:
class RootObject
{
[JsonProperty("users")]
public Users Users { get; set; }
}
[JsonConverter(typeof(TypedExtensionDataConverter<Users>))]
class Users
{
public Users()
{
this.UserTable = new Dictionary<string, User>();
}
[JsonProperty("parentname")]
public string ParentName { get; set; }
[JsonTypedExtensionData]
public Dictionary<string, User> UserTable { get; set; }
}
class User
{
public string name { get; set; }
public string state { get; set; }
public string id { get; set; }
}
I wrote the converter in a fairly general way so it can be reused. A converter that is hardcoded for the Users type would require less code.
Your Json has to look like this:
{
"ParentName":"test",
"users":{
"10045":{
"name":"steve",
"state":"NY",
"id":"ebb2-92bf-3062-7774",
"ParentName":"someOtherName"
}
}
}
In order to deserialize it with your given class structure:
class RootObject
{
public string ParentName { get; set; }
public Dictionary<string, User> users { get; set; }
}
class User
{
public string name { get; set; }
public string state { get; set; }
public string id { get; set; }
public string ParentName { get; set; }
}
Now you can deserialize the Json string with:
var root = JsonConvert.DeserializeObject<RootObject>(json);
We would like to be able to serialize/deserialize json from/to C# classes, with the main class having an instance of a polymorphic child object. Doing so is easy using Json.Net's TypeNameHandling.Auto setting. However, we would like to do so without the "$type" field.
The first thought is to be able to rename "$type" to a value of our choosing and to have the value for the type be an enum that would map the sub-types properly. I have not seen that as being an option but would be glad to hear if it is possible.
The second thought was along the following lines... Below is a first pass at classes, with the top level class having an indicator (SubTypeType) as to what type of data is contained in the child object (SubTypeData). I've dug around a bit into the Json.Net documentation and have tried a few things but have had no luck.
We currently have full control over the data definition, but once it is deployed, then things are locked.
public class MainClass
{
public SubType SubTypeType { get; set; }
public SubTypeClassBase SubTypeData { get; set; }
}
public class SubTypeClassBase
{
}
public class SubTypeClass1 : SubTypeClassBase
{
public string AaaField { get; set; }
}
public class SubTypeClass2 : SubTypeClassBase
{
public string ZzzField { get; set; }
}
Having the subtype information in the container class is problematic for two reasons:
The container class instance is not accessible when Json.NET is reading the contained class.
If you later need to convert the SubTypeClassBase property into, say, a list, there will be nowhere to put the subtype information.
Instead, I would recommend adding the subtype information as a property in the base class:
[JsonConverter(typeof(SubTypeClassConverter))]
public class SubTypeClassBase
{
[JsonConverter(typeof(StringEnumConverter))] // Serialize enums by name rather than numerical value
public SubType Type { get { return typeToSubType[GetType()]; } }
}
Now the custom subtype enum will be serialized whenever an object assignable to SubTypeClassBase is serialized. Having done that, for deserialization you can create a JsonConverter that loads the json for a given SubTypeClassBase into a temporary JObject, checks the value of the "Type" property, and deserializes the JSON object as the appropriate class.
Prototype implementation below:
public enum SubType
{
BaseType,
Type1,
Type2,
}
[JsonConverter(typeof(SubTypeClassConverter))]
public class SubTypeClassBase
{
static readonly Dictionary<Type, SubType> typeToSubType;
static readonly Dictionary<SubType, Type> subTypeToType;
static SubTypeClassBase()
{
typeToSubType = new Dictionary<Type,SubType>()
{
{ typeof(SubTypeClassBase), SubType.BaseType },
{ typeof(SubTypeClass1), SubType.Type1 },
{ typeof(SubTypeClass2), SubType.Type2 },
};
subTypeToType = typeToSubType.ToDictionary(pair => pair.Value, pair => pair.Key);
}
public static Type GetType(SubType subType)
{
return subTypeToType[subType];
}
[JsonConverter(typeof(StringEnumConverter))] // Serialize enums by name rather than numerical value
public SubType Type { get { return typeToSubType[GetType()]; } }
}
public class SubTypeClass1 : SubTypeClassBase
{
public string AaaField { get; set; }
}
public class SubTypeClass2 : SubTypeClassBase
{
public string ZzzField { get; set; }
}
public class SubTypeClassConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(SubTypeClassBase);
}
public override bool CanWrite { get { return false; } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);
var typeToken = token["Type"];
if (typeToken == null)
throw new InvalidOperationException("invalid object");
var actualType = SubTypeClassBase.GetType(typeToken.ToObject<SubType>(serializer));
if (existingValue == null || existingValue.GetType() != actualType)
{
var contract = serializer.ContractResolver.ResolveContract(actualType);
existingValue = contract.DefaultCreator();
}
using (var subReader = token.CreateReader())
{
// Using "populate" avoids infinite recursion.
serializer.Populate(subReader, existingValue);
}
return existingValue;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
You can try with JsonSubtypes converter implementation that support registering type mapping with enum values.
In your case it looks like this:
public class MainClass
{
public SubTypeClassBase SubTypeData { get; set; }
}
[JsonConverter(typeof(JsonSubtypes), "SubTypeType")]
[JsonSubtypes.KnownSubType(typeof(SubTypeClass1), SubType.WithAaaField)]
[JsonSubtypes.KnownSubType(typeof(SubTypeClass2), SubType.WithZzzField)]
public class SubTypeClassBase
{
public SubType SubTypeType { get; set; }
}
public class SubTypeClass1 : SubTypeClassBase
{
public string AaaField { get; set; }
}
public class SubTypeClass2 : SubTypeClassBase
{
public string ZzzField { get; set; }
}
public enum SubType
{
WithAaaField,
WithZzzField
}
[TestMethod]
public void Deserialize()
{
var obj = JsonConvert.DeserializeObject<MainClass>("{\"SubTypeData\":{\"ZzzField\":\"zzz\",\"SubTypeType\":1}}");
Assert.AreEqual("zzz", (obj.SubTypeData as SubTypeClass2)?.ZzzField);
}
Here's a full example that can both read and write JSON with polymorphic objects.
Assuming we have the following class structure:
public class Base {}
public class SubClass1 : Base {
public int field1;
}
public class SubClass2 : Base {
public int field2;
}
We can use a custom converter that creates an extra field in JSON named type when serializing and reads it when deserializing.
public class PolymorphicJsonConverter : JsonConverter
{
public override object ReadJson (JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
JObject item = JObject.Load(reader);
var type = item["type"].Value<string>();
if (type == "SubClass1") {
return item.ToObject<SubClass1>();
} else if (type == "SubClass2") {
return item.ToObject<SubClass2>();
} else {
return null;
}
}
public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer) {
JObject o = JObject.FromObject(value);
if (value is SubClass1) {
o.AddFirst(new JProperty("type", new JValue("SubClass1")));
} else if (value is SubClass1) {
o.AddFirst(new JProperty("type", new JValue("SubClass2")));
}
o.WriteTo(writer);
}
public override bool CanConvert (Type objectType) {
return typeof(Base).IsAssignableFrom(objectType);
}
}
You could use this converter in a container class like so:
public class Container {
public List<Base> items;
public string Save() {
return JsonConvert.SerializeObject(items, new PolymorphicJsonConverter())
}
public void Load(string jsonText) {
items = JsonConvert.DeserializeObject<List<Base>>(jsonText, new PolymorphicJsonConverter());
}
}
Alternatively you could use JSON.net's built in type hinting, instead of rolling your own JsonConverter, but it's not so flexible and creates very un-portable JSON.
JsonConvert.SerializeObject(items, new JsonSerializerSettings {
TypeNameHandling = TypeNameHandling.Auto
});