1

I am using JavaScriptSerializer class for convert json string to object

If I append(10.), for int value, symbols like '.','@' etc still it getting correct json, Actually its not valid json file. I have check this json string using different ways, but it is not valid all the way.

Not understand its behavior.

It should take only '10' for integer.

Code snippet :

class Emp 
{        
    public string firstName
    {
        get;set;
    }
    public string LastName
    {
        set;get;
    }
    public int Id
    {
        set;get;
    }
}

var serializer = new JavaScriptSerializer();
Type t = typeof(Emp);
var returnType = serializer.Deserialize<Emp>("{'firstName': 'John','lastName':'Doe','id': 10.}");
7
  • 1
    I don’t understand what you are asking. Can you clarify? Commented May 19, 2016 at 6:45
  • "{'firstName': 'John','lastName':'Doe','id': 10.}" this is not valid json ojbect, But still its getting parse. Commented May 19, 2016 at 6:46
  • The only invalid part are the single quotes. I assume that the JavaScriptSerializer knowingly accepts that and still parses it when it can. Commented May 19, 2016 at 6:47
  • Yes that I can understand, But still this is not valid input for int(10.). But JavaScriptSerializer should throw error. Commented May 19, 2016 at 6:49
  • 2
    JSON, like JavaScript, does not differentiate between integers and other non-integer numbers. It only knows numbers and for them 10. is the same as 10. So the deserializer only sees the “number” (actually it’s always like a double), and checks whether it could write it into the target int property. If that works (because the double happens to be an integer), then that’s fine. Commented May 19, 2016 at 6:52

3 Answers 3

1

There are two things that come into play here:

The only actually invalid part of that JSON are its quotation marks. JSON actually only allows strings enquoted by double quotation marks, so it’s maybe a bit odd that the JavaScriptSerializer supports deserializing single quotation marks.

This is a deliberate choice to support multiple alternative formats. The underlying deserializer actually even supports leaving out the quotation marks for property names, so the following would be valid too (which makes sense given that it’s also a valid JavaScript expression):

{ firstName: 'John', lastName: 'Doe', id: 10. }

The other part which you mentioned is that 10. number which is not an integer but is deserialized just fine as an integer. This is because JSON—like JavaScript—only knows about numbers. There is no difference between integers and non-integers; there is only a single number type which is an IEEE-754 floating point number, like .NET’s double.

So when deserializing, the base type cannot be taken into account since that would always be a double. But the underlying deserializer actually does attempt to get some information from the actual string representation. So a value of 10 would return an integer, while a value of 10. would return a decimal.

However, when that information is then used and applied to the target type that restriction is loosened, and a converter is used to convert the decimal into the int—since the decimal 10.0 would fit in an int.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all a valid JSON has double quotes so the input should be following:

var returnType2 = serializer.Deserialize<Emp>(@"{""firstName"": ""John"",""lastName"":""Doe"",""id"": 10}");

As for the validation part please check the MSDN JavaScriptSerializer Class documentation. You can see that all the primitive numeric (or numeric-compatible) types like Byte, Ints, uInts etc. are equivalent to JSON Number. While Number (as per JSON schema) is a double precision floating-point format in Javascript thus "id":10. is perfectly valid and JavaScriptSerializer must be able to handle that.

Comments

0

Json object format isnt correct. Simple quotes outside for string format and, double inside the object. Read http://www.w3schools.com/json/ You can validate here https://jsonformatter.curiousconcept.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.