c#Json.net不可变类
我看到了其他类似的问题/答案,但没有一个同时显示序列化/反序列化 例如:c#Json.net不可变类,c#,json.net,C#,Json.net,我看到了其他类似的问题/答案,但没有一个同时显示序列化/反序列化 例如: public class DeepNested { [JsonProperty] int X { get; } [JsonProperty] int Y { get; } public DeepNested(int x, int y) { X = x; Y = y; } [JsonConstructor] public DeepNested(DeepNested
public class DeepNested {
[JsonProperty]
int X { get; }
[JsonProperty]
int Y { get; }
public DeepNested(int x, int y) { X = x; Y = y; }
[JsonConstructor]
public DeepNested(DeepNested dn) { X = dn.X; Y = dn.Y; }
}
public class Nested {
[JsonProperty]
DeepNested DN { get; }
[JsonProperty]
int Z { get; }
[JsonProperty]
int K { get; }
[JsonConstructor]
public Nested(DeepNested dn, int z, int k) { DN = new DeepNested(dn); Z = z; K = k; }
}
public class C {
[JsonProperty]
Nested N { get; }
[JsonConstructor]
public C(Nested n) { N = n; }
}
class Program {
static void Main(string[] args) {
var deepNested = new DeepNested(1,2);
var nested = new Nested(deepNested, 3, 4);
C c = new C(nested);
string json = JsonConvert.SerializeObject(c);
C c2 = JsonConvert.DeserializeObject<C>(json);
Console.WriteLine(json);
}
}
调试器显示dn为null
这似乎是Json.NET的一个严重限制,除非我遗漏了什么?@IpsitGaur是正确的,通常您应该有一个默认的公共构造函数和可公开访问的属性(可变)。但是JSON.Net是一个非常强大的工具 如果需要处理非默认构造函数,可以使用。对于您的代码,示例可能如下所示:
public class Nested
{
public int X { get; }
public int Y { get; }
[JsonConstructor]
Nested(int x, int y) { X=x; Y=y; }
}
public class C
{
public Nested N { get; }
[JsonConstructor]
public C(Nested n) { N = n; }
}
var c1 = new C(new Nested(1, 2));
var json = JsonConvert.SerializeObject(c1); // produce something like "{\"n\":{\"x\":1,\"y\":2}}";
var c2 = JsonConvert.DeserializeObject<C>(json);
公共类嵌套
{
公共整数X{get;}
公共整数Y{get;}
[JsonConstructor]
嵌套的(intx,inty){x=x;y=y;}
}
公共C类
{
公共嵌套N{get;}
[JsonConstructor]
公共C(嵌套n){n=n;}
}
var c1=新的C(新的嵌套(1,2));
var json=JsonConvert.SerializeObject(c1);//产生类似于“{\'n\”:{\'x\”:1、\'y\”:2}”的东西;
var c2=JsonConvert.DeserializeObject(json);
以下工作
public class Nested
{
[JsonProperty]
int X { get; }
[JsonProperty]
int Y { get; }
public Nested(int x, int y) { X = x; Y = y; }
}
public class C
{
[JsonProperty]
Nested N { get; }
public C(Nested n) { N = n; }
}
class Program
{
static void Main(string[] args)
{
Nested nested = new Nested(1, 2);
C c = new C(nested);
string json = JsonConvert.SerializeObject(c);
C c2 = JsonConvert.DeserializeObject<C>(json);
}
}
公共类嵌套
{
[JsonProperty]
int X{get;}
[JsonProperty]
int Y{get;}
公共嵌套(intx,inty){x=x;y=y;}
}
公共C类
{
[JsonProperty]
嵌套N{get;}
公共C(嵌套n){n=n;}
}
班级计划
{
静态void Main(字符串[]参数)
{
嵌套=新嵌套(1,2);
C=新的C(嵌套);
字符串json=JsonConvert.SerializeObject(c);
c2=JsonConvert.DeserializeObject(json);
}
}
你可以发布你试图反序列化的JSON吗?例如,我正在尝试将C对象序列化为JSON,然后反序列化它。你是否尝试过创建无参数构造函数,并使属性具有getter和setter,这不是非常不变的。..classDeepNested
->move[JsonConstructor]
到公共DeepNested(int x,int y)
Constructor我建议在您的答案中包含一个示例,而不是依赖可能会导致链接崩溃的非现场资源。@john Ok,我将添加一个示例,但我也将此链接保留到官方JSON.Net文档(供进一步阅读).谢谢!我编辑了我的问题来展示第二层嵌套是如何打破这一点的,请有一个look@kofifus,我添加了两个(序列化/反序列化)谢谢!我编辑了我的问题,以显示第二级嵌套如何打破这一点,请看一看
public class Nested
{
[JsonProperty]
int X { get; }
[JsonProperty]
int Y { get; }
public Nested(int x, int y) { X = x; Y = y; }
}
public class C
{
[JsonProperty]
Nested N { get; }
public C(Nested n) { N = n; }
}
class Program
{
static void Main(string[] args)
{
Nested nested = new Nested(1, 2);
C c = new C(nested);
string json = JsonConvert.SerializeObject(c);
C c2 = JsonConvert.DeserializeObject<C>(json);
}
}