[英]JSON.Net Deseralize json pair to Object Propery
Ok I have WebApi application that is sending back name value pairs like so 好吧,我有WebApi应用程序发送回名称值对,如此
{'FirstName':'SomeGuy'}
On the server the FirstName field is not just a string, it is a generic object that hold additional information about FirstName, and is not send back from the client. 在服务器上,FirstName字段不仅仅是一个字符串,它是一个通用对象,包含有关FirstName的其他信息,不会从客户端发回。
Here is a outline of the classes 这是课程大纲
public abstract class Field
{
protected object _value;
......More Properties/Methods
public bool HasValue
{
get { return _hasValue; }
}
public object Value
{
get { return _hasValue ? _value : null; }
}
protected void SetValue(object value, bool clearHasValue = false)
{
_value = value;
_hasValue = clearHasValue ?
false :
value != null;
}
}
public class Field<T> : Field
{
..Constructors and methods
public new T Value
{
get { return _hasValue ? (T)_value : default(T); }
set { SetValue(value); }
}
}
So.. In theory I may be trying to bind to a model like 所以..理论上我可能试图绑定到像这样的模型
class FieldModel
{
public Field<string> FirstName { get; set; }
public Field<string> LastName { get; set; }
public Field<Decimal> Amount { get; set; }
public FieldModel()
{
FirstName = new Field<string>();
LastName = new Field<string>();
Amount = new Field<decimal>();
}
}
So here is the issue.. I want FirstName in my json object to deseralize to right property. 所以这就是问题..我想在我的json对象中使用FirstName去deseralize到right属性。 Now if I modify the json package to
{'FirstName.Value':'SomeGuy'}
JSON.net works out of the box, but I really not to do that. 现在如果我将json包修改为
{'FirstName.Value':'SomeGuy'}
JSON.net开箱即用,但我真的不这样做。 I have been tying to make my own JsonConverter but have not been able to get that to work. 我一直在努力制作自己的JsonConverter,但却无法让它发挥作用。 So, I don't think this should be very hard, but I am a bit stuck.
所以,我认为这不应该很难,但我有点卡住了。
So.. I did come up with a solution that works, but I have to think there is a better way.. It uses dynamics and I have to think that I am missing an easy solution. 所以..我确实提出了一个有效的解决方案,但我必须认为有更好的方法..它使用动态,我不得不认为我错过了一个简单的解决方案。
public class FieldConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
var internalVal = serializer.Deserialize(reader, objectType.GetGenericArguments().FirstOrDefault());
var retVal = existingValue as dynamic;
retVal.Value = internalVal as dynamic;
return retVal;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanWrite
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return objectType.IsSubclassOf(typeof(Field));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
You can easily do this with JSON.NET's CustomCreationConverter. 您可以使用JSON.NET的CustomCreationConverter轻松完成此操作。 Here's an example :
这是一个例子 :
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
public class Employee : Person
{
public string Department { get; set; }
public string JobTitle { get; set; }
}
public class PersonConverter : CustomCreationConverter<Person>
{
public override Person Create(Type objectType)
{
return new Employee();
}
}
And the usage: 用法:
string json = @"{
'Department': 'Furniture',
'JobTitle': 'Carpenter',
'FirstName': 'John',
'LastName': 'Joinery',
'BirthDate': '1983-02-02T00:00:00'
}";
Person person = JsonConvert.DeserializeObject<Person>(json, new PersonConverter());
Console.WriteLine(person.GetType().Name);
// Employee
Employee employee = (Employee)person;
Console.WriteLine(employee.JobTitle);
// Carpenter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.