rpgmaker / NetJSON

Faster than Any Binary? Benchmark: http://theburningmonk.com/2014/08/json-serializers-benchmarks-updated-2/
MIT License
231 stars 29 forks source link

Deserializing issue with string value in json containing double quotes - Exception thrown #182

Closed jigneshkvp closed 7 years ago

jigneshkvp commented 7 years ago

Hello,

NetJSON is unable to deserialize string value containing double quotes.

Data to replicate:

public class Test
{
   public string Val { get; set; }
}

Program.cs
class Program
    {
        static void Main(string[] args)
        {
           var settings = new NetJSON.NetJSONSettings
            {
                CaseSensitive=false
            };

             // Actual string { Val : "\"sampleValue\""} before escape characters
             var stringToDeserialize = "{ Val : \"\\\"sampleValue\\\"\"}";

            // Deserialization fails.
           // Exception is throw - Invalid JSON
            var result = NetJSON.NetJSON.Deserialize<Test>(stringToDeserialize, settings); 
        }
}
jigneshkvp commented 7 years ago

Invalid for now, looking more into the exact issue.

rpgmaker commented 7 years ago

Are you still encountering issues with the deserialization? And be sure to try using the latest code in the repo rather than using the nuget package if possible.

Thanks,

jigneshkvp commented 7 years ago

Hello @rpgmaker,

I am still encountering the issue. You can test it as per my comments. I am currently on version - 1.2.1.10

I can deserialize the same string using Newtonsoft.Json

rpgmaker commented 7 years ago

Thanks for the update. I will look into why it thinks it's an invalid JSON.

rpgmaker commented 7 years ago

Sorry I have not had a chance to work on this. I will try to work on it this upcoming week. Thanks

jigneshkvp commented 7 years ago

Hello @rpgmaker, Any update on the fix for this issue?

rpgmaker commented 7 years ago

Sorry, was busy through the week. I would try to resolve it tonight.

Thanks,

rpgmaker commented 7 years ago

So you do have an invalid json. I don't support non quoted string for the key in the json.

So instead of this

"{ Val : \"\\"sampleValue\\"\"}"

use this:

"{ \"Val\" : \"\\"sampleValue\\"\"}"

I did find a bug in the code with quoted string which i fixed and will be pushing soon.

rpgmaker commented 7 years ago

In order for me to support non quoted string I will have to play with the code to see what changes needs to be done. According to standards, keys are suppose to be string to make it easier to read them.

https://stackoverflow.com/a/949476/607310

jigneshkvp commented 7 years ago

Hi @rpgmaker,

I tried as per your comments, modified the code to be:

var stringToDeserialize = "{ \"Val\" : \"\\\"sampleValue\\\"\"}";

There was no exception, but the result was wierd: the string "Val" contained this value: "\"sampleValue\"}\0\0\0耀ﳌJ\a\0Run {0}\0\0\0ш\u0a7d\0\0風ӓ\0\0\0\0\0\0\u0005\0\0\0"

In case of Newtonsoft.Json, the output was as expected: "\"sampleValue\""

rpgmaker commented 7 years ago

Did you use the latest code too? I fixed it in this commit for this issue

Thanks,

jigneshkvp commented 6 years ago

I updated to latest code and tested it, works fine and lightning fast! :+1: