我有一个包含enum属性的类,在使用时序列化对象时JavaScriptSerializer,我的json结果包含枚举的整数值而不是它的string"name".有没有办法让枚举作为string我的json而不必创建自定义JavaScriptConverter?也许有一个属性,我可以装饰enum定义,或对象属性,?
举个例子:
enum Gender { Male, Female }
class Person
{
int Age { get; set; }
Gender Gender { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
期望的json结果:
{ "Age": 35, "Gender": "Male" }
Run Code Online (Sandbox Code Playgroud) 在.NET世界中,当谈到对象序列化时,它通常用于在运行时检查对象的字段和属性.对此作业使用反射通常很慢,并且在处理大量对象时是不合需要的.另一种方法是使用IL发射或构建表达树,这些表现树相对于反射提供显着的性能增益.而后者是处理序列化时最现代化的库.但是,在运行时构建和发送IL需要花费时间,并且只有在将此信息缓存并重用于相同类型的对象时才会回收投资.
当使用Json.NET时,我不清楚使用上述哪种方法,如果确实使用了后者,是否使用了缓存.
例如,当我这样做时:
JsonConvert.SerializeObject(new Foo { value = 1 });
Run Code Online (Sandbox Code Playgroud)
Json.NET是否构建了Foo的成员访问信息并缓存以便以后重用它?
在这个链接上,在备注部分提到了" TypeNameHandling".在什么情况下,如果使用序列化/反序列化来自外部源的JSON会有害SerializationBinder?一个工作的例子将不胜感激.
I'm attempting to use Json.NET with the System.Net.Http.HttpClient to send an object with an enum property, however the enum is always serialized as an integer value rather than the string equivalent.
I've tried following the instructions here:
By both adding an instance of StringEnumConverter to the JsonSerializerSettings and also tried to decorate the enum property with [JsonProperty(ItemConverterType = typeof(StringEnumConverter))] neither of which appear to be working in my example.
I'm using Json.NET version 5.0.8
Can anyone tell me what …
假设我有一个这样的类:
public class Example {
public int TypedProperty { get; set; }
public object UntypedProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
假设有人出现并写道:
var example = new Example
{
TypedProperty = 5,
UntypedProperty = Guid.NewGuid()
}
Run Code Online (Sandbox Code Playgroud)
如果我用 序列化它JsonConvert.SerializeObject(example),我得到
{
"TypedProperty": 5,
"UntypedProperty": "24bd733f-2ade-4374-9db6-3c9f3d97b12c"
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想得到这样的东西:
{
"TypedProperty": 5,
"UntypedProperty":
{
"$type": "System.Guid,mscorlib",
"$value": "24bd733f-2ade-4374-9db6-3c9f3d97b12c"
}
}
Run Code Online (Sandbox Code Playgroud)
但TypeNameHandling在这种情况下不起作用。如何(反)序列化非类型化属性?
我有以下字典,我非常想使用 Json.Net 序列化它。字典包含IConvertible接口的项目,允许我将所需的任何原始类型添加到字典中。
var dic = new Dictionary<string, IConvertible>();
dic.Add("bool2", false);
dic.Add("int2", 235);
dic.Add("string2", "hellohello");
Run Code Online (Sandbox Code Playgroud)
我有以下使用 Json.net 序列化列表的实现:
var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Objects;
var dicString = JsonConvert.SerializeObject(dic, Newtonsoft.Json.Formatting.Indented, settings);
Run Code Online (Sandbox Code Playgroud)
这给了我以下输出:
{
"$type": "System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.IConvertible, mscorlib]], mscorlib",
"bool2": false,
"int2": 235,
"string2": "hellohello"
}
Run Code Online (Sandbox Code Playgroud)
然而。当尝试这样反序列化时:
var dic2 = JsonConvert.DeserializeObject<Dictionary<string, IConvertible>>(dicString);
Run Code Online (Sandbox Code Playgroud)
...我收到以下错误:
Error converting value False to type 'System.IConvertible'. Path 'bool2', line 3, position 16.
Run Code Online (Sandbox Code Playgroud)
我环顾四周,发现了以下内容;但设置 typeNameHandling 并没有解决问题。我也不能用IConvertible类型名称属性来装饰该值,因为它是一个字典。
我还没有找到有关该主题的任何其他信息,因此我们将不胜感激!
我也找到了这个解决方案,但它涉及创建一个 ExpandableObjectConverter 这不是一个非常优雅的解决方案。 …
按照官方文档:
string jsonTypeNameAll = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
Console.WriteLine(jsonTypeNameAll);
// {
// "$type": "Newtonsoft.Json.Samples.Stockholder, Newtonsoft.Json.Tests",
// "FullName": "Steve Stockholder",
// "Businesses": {
// "$type": "System.Collections.Generic.List`1[[Newtonsoft.Json.Samples.Business, Newtonsoft.Json.Tests]], mscorlib",
// "$values": [
// {
// "$type": "Newtonsoft.Json.Samples.Hotel, Newtonsoft.Json.Tests",
// "Stars": 4,
// "Name": "Hudson Hotel"
// }
// ]
// }
// }
Run Code Online (Sandbox Code Playgroud)
我已经复制粘贴了这段代码。
public static string Convert<T>(T cacheObject)
{
return JsonConvert.SerializeObject(cacheObject, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我用 调用它Convert(DateTime.Now),我会得到一个序列化的 …