如何在JsonValue中序列化JsonPrimitive

时间:2013-03-20 15:39:14

标签: c# json serialization mono system.json

我希望所有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 {"":""}!这样的消息。我想得到一个带有以下输出字符串{"Content": "It is a nice day {\"\":\"\"}!"}的JsonValue,并能够将其解析回来。就是这样。

我做错了什么?

修改

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

  • {“Content”:“{\”foo \“:\”bar \“}”}
  • {“Content”:“C:\ temp”}
  • {“Content”:“嗨”“!”}}
  • {“Content”:“{\”“:\”“}”}
  • {“内容”:“这是美好的一天{\”“:\”“}}”}}

  • "{\"Content\": \"{\\\"foo\\\":\\\"bar\\\"}\"}"

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

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

控制台输出:

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

1 个答案:

答案 0 :(得分:1)

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