0

When I send JSON request with integer value, it gets converted to string on the Java side automatically where the accepting field is of String type. I am using Jersey Jackson API for JSON handling. e.g.,

JSON request:

{
   "executable":1
}

Here I am sending integer value to string type executable variable. But it is accepting the integer value and converting it to string.

What my requirement is strict validation for data types.

3
  • 1
    You are sending a JSON String. So everything in that JSON is on String. You can convert that to int in JAVA side. Commented May 5, 2016 at 6:40
  • Is there any way of this validation without explicit type conversion on the JAVA side? Commented May 5, 2016 at 6:43
  • Yeah you can create an Entity depending on your JSON and you can use ObjectMapper(for eg) , which wil convert your json to the entity. No need to use any conversion. Commented May 5, 2016 at 6:45

2 Answers 2

1

All values of a json object are strings. You need to cast it yourself.

Integer.valueOf(STRING_YOU_WANT_TO_CONVERT);

You can use JsonDeserializer as well. There is a example here:Error in Android JsonDeserializer when returning response

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

3 Comments

True but I need validation of the json string sent.
Hhmm you can validate in both ways. What do you mean by validating by schema?
I mean is there a way to fail the validation if an integer value is passed in the JSON request which is mapped to a string value in Java POJO class?
0

Finally I could make changes according to my requirements. My requirement was to validate the JSON request against the JSON schema. It is true that JSON request comes as string so I cannot validate directly, so I accept the request as string and validate against schema using json-schema-validator.

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.