4

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:

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

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

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

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
georgepiva
  • 874
  • 9
  • 29
  • Ouch, all those backslashes are hurting my eyes. – Joe Enos Mar 20 '13 at 16:39
  • @JoeEnos I've just updated my question. There are fewer backslashes this time. May you help me to solve this issue? I just need to serialize any string that an user inputs inside a JsonValue and be able to get it back. – georgepiva Mar 20 '13 at 16:42
  • All I can think of is to debug line-by-line and view the "real" strings all for `content` and `serializedJson` (without all the backslashes - use the Text Visualizer or a Console.WriteLine or something to see the real string). There must be something a little out of whack with all the `JsonPrimitive` and `JsonValue` stuff. I tend to use JSON.NET or even `JavaScriptSerializer`, so I haven't had much experience with these. And I don't have VS2012 with me right now to try it. – Joe Enos Mar 20 '13 at 17:09
  • @JoeEnos I understand. Actually I am developing a Monotouch application using the new Xamarin Studio on my Mac. It is great but I cannot use any System.Web class so JavaScriptSerializer is unable. Maybe my only choice is to use the JSON.NET but I am almost releasing my application and this is the last JSON issue I need to deal with. – georgepiva Mar 20 '13 at 17:16
  • @JoeEnos I have just updated my question again. I will be using an work around until finding a better solution. But thank you for helping me during this initial stage. – georgepiva Mar 20 '13 at 17:47
  • 1
    Looks like you're actually testing the framework, not your own code. Does that sound right? So what are the results of the failure? When `Assert.AreEqual` fails, it should tell you what the two strings are, and that will tell you what went wrong. Assuming the framework is broken, you'll probably need to work around the issue like you're doing, and just report it to whoever built the framework (the Mono project? not sure who's responsible for that). Or if it also occurs in real .NET, I'm sure Microsoft would want to know. – Joe Enos Mar 20 '13 at 18:02
  • @JoeEnos I am not sure what is making some TestCases to fail, I have just updated the question with the output presented on the application console. I am going to get in touch with someone from Xamarin which is the main sponsor of the Mono project. Probably they will know if it is a bug from the framework. But I don't believe that this is the case. Let's see. Thank you again Joe. – georgepiva Mar 20 '13 at 18:17
  • 1
    Right before deserialization, see what the value of `serializedJson` is, maybe with a `Console.WriteLine` to avoid the debugger mess. It should be: `{"Content":"Hi \"\"!"}`. That's the only thing that would pass your test. If it's anything else, then the serializer is broken. If it is this, then the deserializer is broken, since it's valid, and should successfully deserialize. – Joe Enos Mar 20 '13 at 19:22
  • @JoeEnos Hello again dude. I have just updated the question as you pointed me. You are right, there is a problem during the serialization proccess. Otherwise, I am trying to do something wrong or unexpected by the API. – georgepiva Mar 20 '13 at 20:09
  • @JoeEnos is it possible for you to run this same TestCases on VS (Windows/.NET) just to see if we will receive different results? I just have a MBP without an Windows installation. Thanks. – georgepiva Mar 20 '13 at 21:16
  • 1
    The Json classes are only available on .NET 4.5, and I'm not in front of a machine with 4.5 right now. I'll definitely run this some time tonight (probably will be late tonight though, maybe 8 hours from now). I'm really curious to see what happens. I'll reply as soon as I try it. – Joe Enos Mar 20 '13 at 21:29
  • @JoeEnos Perfect. Thank Joe. Depending on your test results we can generate a Bug report at http://www.mono-project.com/Bugs. – georgepiva Mar 20 '13 at 23:11
  • 1
    May be a day or two before I can reproduce on VS2012. My home machine went kablooey yesterday, and I'm not having a whole lot of luck with alternatives. – Joe Enos Mar 21 '13 at 05:48
  • 1
    It's looking like this isn't part of the .NET 4.5 framework. According to [MSDN](http://msdn.microsoft.com/en-us/library/System.Json.JsonObject), it should be in System.Runtime.Serialization, but that namespace isn't there - the MSDN article says it's preview only, so maybe they didn't include it in the real framework, except in Silverlight. So I think you're on your own with the Mono version of this - it doesn't look like Microsoft even implements the thing. – Joe Enos Mar 22 '13 at 04:02
  • @JoeEnos Thank you again Joe for the given information. I've just submitted a bug report at https://bugzilla.xamarin.com/show_bug.cgi?id=11384. So let's wait for an answer. – georgepiva Mar 25 '13 at 01:32

1 Answers1

1

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.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • Actually I am testing with Json strings to be sure it will not affect my application behaviour. 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 {\"\":\"\"}!"}` an be able to parse it back. That is all. – georgepiva Mar 20 '13 at 17:00