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\":\"バー\"}"}
  • {"コンテンツ": "C:\temp"}
  • {"コンテンツ": "こんにちは \""!"}
  • {"コンテンツ": "{\"":\""}"}
  • {"Content": "いい日ですね {\"":\""}!"}

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

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

失敗している TestCases で2 番目"が置き換えられない理由がわかりません。\"

コンソール出力:

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 に答える