我一直无法找到合理的实现,JsonConvert.WriteJson这允许我在序列化特定类型时插入JSON属性.我的所有尝试都导致"JsonSerializationException:使用类型XXX检测到自引用循环".
关于我正在尝试解决的问题的更多背景:我使用JSON作为配置文件格式,我使用a JsonConverter来控制我的配置类型的类型解析,序列化和反序列化.$type我想使用更有意义的JSON值来解决正确的类型,而不是使用属性.
在我的简化示例中,这是一些JSON文本:
{
"Target": "B",
"Id": "foo"
}
Run Code Online (Sandbox Code Playgroud)
其中JSON属性"Target": "B"用于确定此对象应序列化为类型B.考虑到这个简单的例子,这种设计似乎并不那么引人注目,但它确实使配置文件格式更加有用.
我还希望配置文件是可循环访问的.我有反序列化的工作,我无法工作的是序列化案例.
我的问题的根源是我找不到JsonConverter.WriteJson使用标准JSON序列化逻辑的实现,并且不会抛出"自引用循环"异常.这是我的实现:
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JProperty typeHintProperty = TypeHintPropertyForType(value.GetType());
//BUG: JsonSerializationException : Self referencing loop detected with type 'B'. Path ''.
// Same error occurs whether I use the serializer parameter or a separate serializer.
JObject jo = JObject.FromObject(value, serializer);
if (typeHintProperty != null)
{
jo.AddFirst(typeHintProperty);
}
writer.WriteToken(jo.CreateReader());
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这是Json.NET中的一个错误,因为应该有办法做到这一点.不幸的是 …
我有一些.NET代码反序列JSON化由webservice运行动态语言创建的对象.因为源是动态的,所以它有时会以float格式序列化整数值(例如2被序列化为"2.0").
随着Json.NET 4.0.4,这无缝地工作(似乎在反序列化时应用舍入).Json.NET 4.5但是,升级后,反序列化2.0现在会抛出一个FormatException.这是代码:
// works as expected in both versions
var s = "2";
Console.WriteLine(JsonConvert.DeserializeObject<int>(s));
// throws FormatException in 4.5 only
var s = "2.0";
Console.WriteLine(JsonConvert.DeserializeObject<int>(s));
// throws FormatException in 4.5, rounds to 3 in 4.0.4
var s = "2.6";
Console.WriteLine(JsonConvert.DeserializeObject<int>(s));
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来恢复原来的行为?理想的行为是仅反序列化具有整数值的数字,但是以任何格式(例如2.0,1e10,但不是2.5),但我会满足于4.0.4行为.
我正在使用 Newtonsoft.Json 程序集将 Json 字符串反序列化为动态对象 (ExpandoObject)。我遇到的问题是 int 值总是以 Int64 形式返回,而我期待的是 Int32。代码如下所示。
namespace Serialization
{
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static class JsonSerializer
{
#region Public Methods
public static string Serialize(dynamic obj)
{
return JsonConvert.SerializeObject(obj);
}
public static dynamic Deserialize(string s)
{
var obj = JsonConvert.DeserializeObject(s);
return obj is string ? obj as string : Deserialize((JToken)obj);
}
#endregion
#region Methods
private static dynamic Deserialize(JToken token)
{
// FROM : http://blog.petegoo.com/archive/2009/10/27/using-json.net-to-eval-json-into-a-dynamic-variable-in.aspx
// Ideally in …Run Code Online (Sandbox Code Playgroud) 如果我将一些 JSON 反序列化为对象字典,如下所示:
var properties = "{\"property1\":\"\",\"property2\":2}";
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(properties);
Console.WriteLine(dictionary["property2"].GetType()); // Prints System.Int64
Run Code Online (Sandbox Code Playgroud)
整数值属性property2会自动反序列化为 along而不是int。
我可以自动转换为 吗int?
我通过这样做将 Json 转换为 ExpandoObject
JsonConvert.DeserializeObject<ExpandoObject>(jsonText)
Run Code Online (Sandbox Code Playgroud)
我得到的整数是 Int64,但我更喜欢 Int32。
JsonConvert 中是否有任何设置可用于更改默认行为?