使用JSON.Net将字符串属性值反序列化为类实例 [英] Deserializing a string property value to a class instance using JSON.Net
问题描述
我正在从服务器上反序列化一些JSON,这在大多数情况下很简单:
I'm deserializing some JSON from a server which is, for the most part, simple:
{
"id": "ABC123"
"number" 1234,
"configured_perspective": "ComplexPerspective[WithOptions,Encoded]"
}
但是,不幸的是,当嵌套对象会更好时,该"configured_perspective"属性是服务器使用奇怪的组合字符串的情况.
That "configured_perspective" property, however, is an unfortunate case of the server using a weirdly put-together string when a nested object would have been better.
为减轻.NET用户的痛苦,我将其转换为对象模型中的自定义类:
To ease the suffering of our .NET users, I convert this into a custom class in my object model:
public class Example
{
public string id { get; set; }
public int number { get; set; }
public Perspective configured_perspective { get; set; }
}
// Note, instances of this class are immutable
public class Perspective
{
public CoreEnum base_perspective { get; }
public IEnumerable<OptionEnum> options { get; }
public Perspective(CoreEnum baseArg, IEnumerable<OptionEnum> options) { ... }
public Perspective(string stringRepresentation) {
//Parses that gross string to this nice class
}
public static implicit operator Perspective(string fromString) =>
new Perspective(fromString);
public override string ToString() =>
base_perspective + '[' + String.Join(",", options) + ']';
}
如您所见,我已经组合了一个自定义类Perspective
,该类可以在JSON字符串之间进行转换,但是我似乎无法获得Newtonsoft JSON来自动将字符串转换为我的Perspective类.
As you can see, I've put together a custom class Perspective
that converts to and from the JSON string, but I can't seem to get Newtonsoft JSON to automatically convert the string to my Perspective class.
我试图让它使用[JsonConstructor]
属性来调用字符串构造函数,但它只是使用null
来调用构造函数,而不是使用JSON中存在的字符串值.
I tried getting it to call the string constructor with the [JsonConstructor]
attribute, but it just calls the constructor with null
, not with the string value present in the JSON.
我印象深刻(基于 https://stackoverflow.com/a/34186322/529618 ) JSON.NET将使用隐式/显式字符串转换运算符将JSON中的简单字符串转换为目标类型的实例(如果可用),但它似乎会忽略它,并仅返回错误:
I was under the impression (based on https://stackoverflow.com/a/34186322/529618) that JSON.NET would use implicit/explicit string conversion operators to convert a simple string in JSON to an instance of the target type when available, but it seems to ignore it, and just returns the error:
Newtonsoft.Json.JsonSerializationException:无法找到用于Perspective类型的构造函数.一个类应该具有一个默认的构造函数,一个带有参数的构造函数或一个标有JsonConstructor属性的构造函数.路径"configured_perspective"
Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type Perspective. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'configured_perspective'
我试图避免为我的Example
类编写自定义JsonConverter-我很确定会有一种开箱即用的方式将简单的字符串值转换为非字符串属性类型,我还没找到.
I'm trying to avoid resorting to writing a custom JsonConverter for my Example
class - I was pretty sure there would be an out-of-the-box way to convert simple string values to a non-string property type, I just haven't found it yet.
推荐答案
在阅读您的文章的最后部分之前,我实际上写了一个自定义的序列化程序类,但是后来我有了一个主意.
I actually wrote out a custom serializer class before doing reading the last of your article, but I then had an idea.
如果我们修改示例而不将其序列化为Perspective怎么办?而且我们对此有些懒惰?
What if we modified example to not serialize it to Perspective? And we were somewhat lazy about it?
public class Example
{
public string id { get; set; }
public int number { get; set; }
public string configured_perspective { get; set; }
private Perspective _configuredPespective;
[JsonIgnore]
public Perspective ConfiguredPerspective => _configuredPerspective == null ? new Perspective(configured_persective) : _configuredPerspective;
}
这不是完美的,我们保留了字符串浪费的内存,但是它可能对您有效.
It's not perfect, and we hold onto the string wasting memory, but it might work for you as a work-around.
这篇关于使用JSON.Net将字符串属性值反序列化为类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!