I have a C# Application.
我有一个c#应用程序。
I have a class that is generated from an xsd. The class looks as below
我有一个由xsd生成的类。类如下所示
public class Transaction
{
public bool amountSpecified {get; set;}
public double amount {get; set;}
}
If you notice in the class above, along with the property amount, the generator has also generated a property called amountSpecified.
如果您在上面的类中注意到,以及属性数量,那么生成器还生成了一个名为amountSpecified的属性。
I know that the properties with suffix "Specified" are required for all non-nullable field/property, because this is the requirement of XML Serializer as mentioned in this [article][1].
我知道,所有非空字段/属性都需要带有“指定”后缀的属性,因为这是[文章][1]中提到的XML序列化器的要求。
However I only use JSON serialization and deserialization(with JSON.NET), do I still need those fields with "Specified" suffix? If I remove them should I make my fields/properties nullable as shown below?
但是我只使用JSON序列化和反序列化(使用JSON.NET),我还需要那些带有“指定”后缀的字段吗?如果我删除它们,我是否应该将字段/属性设为空,如下所示?
double? amount;
My question being is all of this internally handled by JSON.Net? Can I safely remove all the fields with suffix "specified" and not make my fields nullable?
我的问题是所有这些都是JSON.Net内部处理的吗?我可以安全地删除带有“指定”后缀的所有字段,而不使字段为空吗?
I would be very glad if someone can point me in the right direction. Thanks in Advance.
如果有人能给我指出正确的方向,我会很高兴。提前谢谢。
1 个解决方案
#1
1
As discussed since 2008, they fixed it to support nullable type. Also I tried with this code
正如2008年以来所讨论的,他们将其固定为支持可空类型。我也尝试了这个代码
using System;
using Newtonsoft.Json;
namespace TestJson
{
class Test {
public double? amount { get; set; }
}
class MainClass
{
public static void Main(string[] args)
{
string jsonStr = JsonConvert.SerializeObject(new Test());
string jsonStr2 = JsonConvert.SerializeObject(new Test { amount = 5 } );
Console.WriteLine(jsonStr);
Console.WriteLine(jsonStr2);
Console.ReadLine();
}
}
}
It works just fine:
它工作得很好:
{"amount":null}
{"amount":5.0}
And the properties with Specified
suffix are not required.
并且不需要具有指定后缀的属性。
#1
1
As discussed since 2008, they fixed it to support nullable type. Also I tried with this code
正如2008年以来所讨论的,他们将其固定为支持可空类型。我也尝试了这个代码
using System;
using Newtonsoft.Json;
namespace TestJson
{
class Test {
public double? amount { get; set; }
}
class MainClass
{
public static void Main(string[] args)
{
string jsonStr = JsonConvert.SerializeObject(new Test());
string jsonStr2 = JsonConvert.SerializeObject(new Test { amount = 5 } );
Console.WriteLine(jsonStr);
Console.WriteLine(jsonStr2);
Console.ReadLine();
}
}
}
It works just fine:
它工作得很好:
{"amount":null}
{"amount":5.0}
And the properties with Specified
suffix are not required.
并且不需要具有指定后缀的属性。