f# - Newtonsoft Converter FromJson - 意外标记

标签 f# json.net

我已经尝试编写 JSON 反序列化器有一段时间了,但一直无法找到我的错误。反序列化后,为什么 Newtonsoft 告诉我,反序列化对象时出现意外标记:StartObject

type ThisFails =
  { a : string * string
    m : Map<string, string> }

type ThisWorks =
  { y : Map<string, string>
    z : string * string }

testCase "failing test - array before object" <| fun _ ->
  let res = deserialise<ThisFails> Serialisation.converters
                                   """{"a":["xyz","zyx"],"m":{}}"""
  Assert.Equal("should be eq to res", { a = "xyz", "zyx"; m = Map.empty }, res)

testCase "passing test - array after object" <| fun _ ->
  let res = deserialise<ThisWorks> Serialisation.converters
                                   """{"y":{},"z":["xyz","zyx"]}"""
  Assert.Equal("should be eq to res", { y = Map.empty; z = "xyz", "zyx" }, res)

主题is the TupleArrayConverter .

该转换器的轨迹是:

reading json [Newtonsoft.Json.FSharp.TupleArrayConverter]
  type => System.Tuple`2[System.String,System.String]

value token, pre-deserialise [Newtonsoft.Json.FSharp.TupleArrayConverter]
  path => "a[0]"
  token_type => String

value token, post-deserialise [Newtonsoft.Json.FSharp.TupleArrayConverter]
  path => "a[1]"
  token_type => String

value token, pre-deserialise [Newtonsoft.Json.FSharp.TupleArrayConverter]
  path => "a[1]"
  token_type => String

value token, post-deserialise [Newtonsoft.Json.FSharp.TupleArrayConverter]
  path => "a"
  token_type => EndArray

after EndArray token, returning [Newtonsoft.Json.FSharp.TupleArrayConverter]
  path => "m"
  token_type => PropertyName

在转换器中,我正在使用最后一个 token ,即结束数组,正如您在终止情况中看到的那样:

match reader.TokenType with
| JsonToken.EndArray ->
  read JsonToken.EndArray |> req |> ignore
  acc

我一开始就消耗了 StartArray token ...

那么:为什么这段代码不起作用? (Newtonsoft.Json 6.0.8)

这是错误:

map tests/failing test - array before object: Exception: Newtonsoft.Json.JsonSerializationException: Unexpected token when deserializing object: StartObject. Path 'm', line 1, position 24.
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndCreatorValues (Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.JsonReader reader, System.Type objectType, IDictionary`2& extensionData) [0x00000] in <filename unknown>:0
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty containerProperty, Newtonsoft.Json.Serialization.ObjectConstructor`1 creator, System.String id) [0x00000] in <filename unknown>:0
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract objectContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id, System.Boolean& createdFromNonDefaultCreator) [0x00000] in <filename unknown>:0
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in <filename unknown>:0
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x00000] in <filename unknown>:0
  at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, Boolean checkAdditionalContent) [0x00000] in <filename unknown>:0  (00:00:00.1500996)
1 tests run: 0 passed, 0 ignored, 0 failed, 1 errored (00:00:00.2482623)

最佳答案

一直在与 JSON.Net 一起调试您的代码。

事实证明,在失败的情况下使用 EndArray token 后,读取器会指向 PropertyName token ,这一切都很好。

然后,在转换器完成执行后,JSON.Net 将执行此操作。

} while (!exit && reader.Read());

Read() 然后将读取器移动到下一个 token ,在失败的情况下,该 token 是 StartObject 导致解串器失败。

所以,我不是 JSON.Net 的专家,但考虑在 JSON.Net 中提供字符串值的提供程序,我可能不会在转换后推进读者,这意味着读者仍然指向字符串值。沿着同样的思路,在使用数组时让读者留下数组值的最后一个标记(即 EndArray 标记)是有意义的。

所以我的建议只是这样:

match reader.TokenType with
| JsonToken.EndArray ->
//    read JsonToken.EndArray |> req |> ignore

  Logger.debug logger <| fun _ ->
    LogLine.sprintf
      [ "path", reader.Path |> box
        "token_type", reader.TokenType |> box ]
      "after EndArray token, returning"

  acc

这使得我的测试程序:

[<EntryPoint>]
let main argv = 
    let works = deserialize<ThisWorks> """{"y":{},"z":["xyz","zyx"]}"""
    printfn "%A" works

    let fails = deserialize<ThisFails> """{"a":["xyz","zyx"],"m":{}}"""
    printfn "%A" fails

    0

打印

{y = map [];
 z = ("xyz", "zyx");}
{a = ("xyz", "zyx");
 m = map [];}

希望这可以帮助您解决此错误(您可能已经这样做了)

关于f# - Newtonsoft Converter FromJson - 意外标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29719613/

相关文章:

f# - 编译器如何处理扩展记录

c# - 我可以在属性中指定路径以将我的类中的属性映射到我的 JSON 中的子属性吗?

c# - Json.NET:如何从生成的 json 字符串中的类型中删除程序集信息?

functional-programming - 函数式编程: How to model relationships?

linq - 将 Linq 转换为 FSharp 表达式

F# 尾递归函数示例

c# - 反序列化到 C# 中类的现有实例

c# - 无法使用 Json.Net 反序列化指数数值

c# - JSON.NET 序列化在 ApiController 中的行为不同

string - F# 字符串到枚举的转换