4

我想Test为所有人下一次传球TestCases

[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\": \"{\\\"foo\\\":\\\"bar\\\"}\"}"

  • "{\"Content\": \"C:\\\\temp\"}"
  • "{\"Content\": \"Hi \\\"\"!\"}"
  • "{\"Content\": \"{\\\"\":\\\"\"}\"}"
  • "{\"Content\": \"It is a nice day {\\\"\":\\\"\"}!\"}"

我不知道为什么第二个在失败的 TestCases 上"没有被替换。\"

控制台输出:

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
4

1 回答 1

1

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

于 2013-03-20T16:52:11.353 回答