Undefined behaviour in Json

主宰稳场 提交于 2019-12-12 00:30:36

问题


What should happen when the value of a property is set to undefined in a json string. i.e:

{"Name":undefined}

The following example is using the json.net library. An exception is thrown when de-serializing the object.

JsonConvert.DeserializeObject<SimpleObject>("{\"Name\":undefined}");

public class SimpleObject
{
  public string Name { get; set; }
}

Newtonsoft.Json.JsonReaderException was unhandled
  Message=Error reading string. Unexpected token: Undefined. Path 'Value', line 1, position 18.
  Source=Newtonsoft.Json
  LineNumber=1
  LinePosition=18
  Path=Value

回答1:


I think the error is fine.

Jsonlint.org throws an error too.

And reading the documentation on json.org the "value" element may have the following variants:

string number object array true false null

As you can see, undefined is NOT listed. Object does also not count as undefined.



来源:https://stackoverflow.com/questions/10455675/undefined-behaviour-in-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!