gpt4 book ai didi

c# - 如何在 JsonValue 中序列化 JsonPrimitive

转载 作者:行者123 更新时间:2023-11-30 12:47:53 25 4
gpt4 key购买 nike

我想让下一个测试通过所有测试用例:

[TestCase("{\"foo\":\"bar\"}")]             // [PASS]
[TestCase("C:\\temp")] // [PASS]
[TestCase("Hi \"\"!")] // [FAIL]
[TestCase("{\"\":\"\"}")] // [FAIL]
[TestCase("It is a nice day {\"\":\"\"}!")] // [FAIL]
public void JsonValueTests_JsonPrimitiveSerialization_ReturnsEqual (string content)
{
JsonValue json = new JsonObject ();
json ["Content"] = new JsonPrimitive (content);

string serializedJson = json.ToString ();
System.Diagnostics.Debug.WriteLine (serializedJson);

JsonValue deserializedJson = JsonValue.Parse (serializedJson);

string deserializedContent = deserializedJson["Content"];
Assert.AreEqual (content, deserializedContent);
}

但我得到的结果是:

Tests run: 5 Passed: 2 Inconclusive: 0 Failed: 3 Ignored: 0

假设用户刚刚输入了一条消息,如 It is a nice day {"":""}!。我想获得具有以下输出字符串的 JsonValue {"Content": "It is a nice day {\"\":\"\"}!"} 并能够解析它后退。就是这样。

我做错了什么?

编辑

我在 Debug.WriteLine 行之后放置了一个断点,这样我就可以从控制台和 debugger 获取下一个输出:

  • {"内容": "{\"foo\":\"bar\"}"}
  • {"内容": "C:\temp"}
  • {"Content": "嗨\""!"}
  • {"内容": "{\"":\""}"}
  • {"Content": "今天天气不错{\"":\""}!"}

  • "{\"内容\":\"{\\\"foo\\\":\\\"bar\\\"}\"}"

  • "{\"Content\":\"C:\\\\temp\"}"
  • "{\"Content\":\"嗨\\\"\"!\"}"
  • "{\"内容\":\"{\\\"\":\\\"\"}\"}"
  • "{\"Content\":\"今天天气不错 {\\\"\":\\\"\"}!\"}"

我不知道为什么失败的测试用例上的第二个 " 没有替换为 \"

控制台输出:

JsonValueTests_JsonPrimitiveSerialization_ReturnsEqual
[FAIL] JsonValueTests_JsonPrimitiveSerialization_ReturnsEqual("{\"\":\"\"}") : System.ArgumentException : Invalid JSON string literal format. At line 1, column 18
at System.Runtime.Serialization.Json.JavaScriptReader.ReadStringLiteral () [0x0000d] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JavaScriptReader.cs:261
at System.Runtime.Serialization.Json.JavaScriptReader.ReadCore () [0x00118] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JavaScriptReader.cs:73
at System.Runtime.Serialization.Json.JavaScriptReader.Read () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JavaScriptReader.cs:27
at System.Json.JsonValue.Load (System.IO.TextReader textReader) [0x00011] in /Developer/MonoTouch/Source/mono/mcs/class/System.Json/System.Json/JsonValue.cs:28
at System.Json.JsonValue.Parse (System.String jsonString) [0x00011] in /Developer/MonoTouch/Source/mono/mcs/class/System.Json/System.Json/JsonValue.cs:101

[FAIL] JsonValueTests_JsonPrimitiveSerialization_ReturnsEqual("It is a nice day {\"\":\"\"}!") : System.ArgumentException : Invalid JSON string literal format. At line 1, column 35
at System.Runtime.Serialization.Json.JavaScriptReader.ReadStringLiteral () [0x0000d] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JavaScriptReader.cs:261
at System.Runtime.Serialization.Json.JavaScriptReader.ReadCore () [0x00118] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JavaScriptReader.cs:73
at System.Runtime.Serialization.Json.JavaScriptReader.Read () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JavaScriptReader.cs:27
at System.Json.JsonValue.Load (System.IO.TextReader textReader) [0x00011] in /Developer/MonoTouch/Source/mono/mcs/class/System.Json/System.Json/JsonValue.cs:28
at System.Json.JsonValue.Parse (System.String jsonString) [0x00011] in /Developer/MonoTouch/Source/mono/mcs/class/System.Json/System.Json/JsonValue.cs:101

[FAIL] JsonValueTests_JsonPrimitiveSerialization_ReturnsEqual("Hi \"\"!") : System.ArgumentException : JSON string is not closed. At line 1, column 23
at System.Runtime.Serialization.Json.JavaScriptReader.ReadStringLiteral () [0x0003a] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JavaScriptReader.cs:268
at System.Runtime.Serialization.Json.JavaScriptReader.ReadCore () [0x00118] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JavaScriptReader.cs:73
at System.Runtime.Serialization.Json.JavaScriptReader.Read () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JavaScriptReader.cs:27
at System.Json.JsonValue.Load (System.IO.TextReader textReader) [0x00011] in /Developer/MonoTouch/Source/mono/mcs/class/System.Json/System.Json/JsonValue.cs:28
at System.Json.JsonValue.Parse (System.String jsonString) [0x00011] in /Developer/MonoTouch/Source/mono/mcs/class/System.Json/System.Json/JsonValue.cs:101

[PASS] JsonValueTests_JsonPrimitiveSerialization_ReturnsEqual("{\"foo\":\"bar\"}")
[PASS] JsonValueTests_JsonPrimitiveSerialization_ReturnsEqual("C:\\temp")

JsonValueTests_JsonPrimitiveSerialization_ReturnsEqual : 74,711 ms

Tests run: 5 Passed: 2 Inconclusive: 0 Failed: 3 Ignored: 0

最佳答案

不确定这是否是您的问题的原因,但您的空 JSON 对象:{"":""} 无效 - 您不能有空键。一个空的 JSON 对象将是 {} 如果这是您想要的。

关于c# - 如何在 JsonValue 中序列化 JsonPrimitive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15528112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com