georgepiva
georgepiva

Reputation: 874

How to serialize a JsonPrimitive inside a JsonValue

I would like to have the next Test passing for all 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);
}

But I am getting this result:

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

Let's say that an user have just typed a message like It is a nice day {"":""}!. I would like to get a JsonValue with the following output string {"Content": "It is a nice day {\"\":\"\"}!"} and be able to parse it back. That is all.

What am I doing wrong?

Edit

I put a breakpoint right after the Debug.WriteLine line and so I could grab the next outputs from both console and debugger:

I don't know why the second " isn't replaced with \" on the TestCases that are failing.

Console output:

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

Upvotes: 4

Views: 1085

Answers (1)

Joe Enos
Joe Enos

Reputation: 40383

Not sure if this is the cause of your problem, but your empty JSON object: {"":""} is not valid - you can't have an empty key. An empty JSON object would be {} if that's what you're going for.

Upvotes: 1

Related Questions