[英]Serialize json to an object with catch all dictionary property
I would like to use JSON.net to deserialize to an object but put unmapped properties in a dictionary property. 我想使用JSON.net反序列化为对象,但将未映射的属性放在字典属性中。 Is it possible?
可能吗?
For example given the json, 比如给json,
{one:1,two:2,three:3}
and the c# class: 和c#类:
public class Mapped {
public int One {get; set;}
public int Two {get; set;}
public Dictionary<string,object> TheRest {get; set;}
}
Can JSON.NET deserialize to an instance with values one=1, two=1, TheRest= Dictionary{{"three,3}} JSON.NET可以反序列化为值为1 = 1,2 = 1的实例,TheRest = Dictionary {{“three,3}}
The easiest way to do this is to use the JsonExtensionData
attribute to define a catch all dictionary. 最简单的方法是使用
JsonExtensionData
属性来定义catch all dictionary。
Example from the Json.Net documentation : Json.Net文档中的示例:
public class DirectoryAccount
{
// normal deserialization
public string DisplayName { get; set; }
// these properties are set in OnDeserialized
public string UserName { get; set; }
public string Domain { get; set; }
[JsonExtensionData]
private IDictionary<string, JToken> _additionalData;
[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
// SAMAccountName is not deserialized to any property
// and so it is added to the extension data dictionary
string samAccountName = (string)_additionalData["SAMAccountName"];
Domain = samAccountName.Split('\\')[0];
UserName = samAccountName.Split('\\')[1];
}
public DirectoryAccount()
{
_additionalData = new Dictionary<string, JToken>();
}
}
string json = @"{
'DisplayName': 'John Smith',
'SAMAccountName': 'contoso\\johns'
}";
DirectoryAccount account = JsonConvert.DeserializeObject<DirectoryAccount>(json);
Console.WriteLine(account.DisplayName);
// John Smith
Console.WriteLine(account.Domain);
// contoso
Console.WriteLine(account.UserName);
// johns
You can create a CustomCreationConverter to do what you need to do. 您可以创建CustomCreationConverter来执行您需要执行的操作。 Here's a sample (rather ugly, but demonstrates how you may want to go about this):
这是一个样本(相当丑陋,但演示了你可能想要这样做):
namespace JsonConverterTest1
{
public class Mapped
{
private Dictionary<string, object> _theRest = new Dictionary<string, object>();
public int One { get; set; }
public int Two { get; set; }
public Dictionary<string, object> TheRest { get { return _theRest; } }
}
public class MappedConverter : CustomCreationConverter<Mapped>
{
public override Mapped Create(Type objectType)
{
return new Mapped();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var mappedObj = new Mapped();
var objProps = objectType.GetProperties().Select(p => p.Name.ToLower()).ToArray();
//return base.ReadJson(reader, objectType, existingValue, serializer);
while (reader.Read())
{
if (reader.TokenType == JsonToken.PropertyName)
{
string readerValue = reader.Value.ToString().ToLower();
if (reader.Read())
{
if (objProps.Contains(readerValue))
{
PropertyInfo pi = mappedObj.GetType().GetProperty(readerValue, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
var convertedValue = Convert.ChangeType(reader.Value, pi.PropertyType);
pi.SetValue(mappedObj, convertedValue, null);
}
else
{
mappedObj.TheRest.Add(readerValue, reader.Value);
}
}
}
}
return mappedObj;
}
}
public class Program
{
static void Main(string[] args)
{
string json = "{'one':1, 'two':2, 'three':3, 'four':4}";
Mapped mappedObj = JsonConvert.DeserializeObject<Mapped>(json, new MappedConverter());
Console.WriteLine(mappedObj.TheRest["three"].ToString());
Console.WriteLine(mappedObj.TheRest["four"].ToString());
}
}
}
So the output of mappedObj after you deserialize the JSON string will be an object with its One
and Two
properties populated, and everything else put into the Dictionary
. 因此,在反序列化JSON字符串后,mappedObj的输出将是一个对象,
One
填充了One
和Two
属性,并将其他所有内容放入Dictionary
。 Granted, I hard-coded the One and Two values as int
s, but I think this demonstrates how you'd go about this. 当然,我将One和Two值硬编码为
int
,但我认为这证明了你如何去做。
I hope this helps. 我希望这有帮助。
EDIT : I updated the code to make it more generic. 编辑 :我更新了代码,使其更通用。 I didn't fully test it out, so there may some cases where it fails, but I think it gets you most of the way there.
我没有完全测试它,所以在某些情况下它会失败,但我认为它会让你大部分时间都在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.