lucidquiet
lucidquiet

Reputation: 6558

This seems like a bug in .NET's JavaScript deserialize... is it?

Basically, I found that a JSON string that is a JavaScript object (associative array) with an empty string for one of its properties/keys will cause the built in .NET Serializer to throw an exception.

For example this code:

string json = "{ \"\" : [\"b\"] }";

JavaScriptSerializer js = new JavaScriptSerializer();

var map = js.Deserialize<Dictionary<string, List<string>>>(json);

Will throw an ArgumentException, suggesting that maybe proper JSON doesn’t allow empty strings for property names. Just glancing at the Backaus Naur Form Diagram / Syntax Diagram on Crockford’s site suggests that an empty string is allowed.

object => '{'  string ':' value '}'
string => ""
       => " chars "

Upvotes: 4

Views: 176

Answers (1)

aquinas
aquinas

Reputation: 23796

Yep. That sure looks like a bug to me. This is definitely valid:

var x = {"" : ["b"]}
alert(x[""]); 

I'm not saying you should do this, but you can do it in JS.

Edit Having said that. Your code works for me. What version of .NET are you using?

Edit Confirmed. It breaks in .NET 3.5 but works in 4.0.

Upvotes: 4

Related Questions