Good day everyone. Help me to understand.
There is json:

{"length":"6" ,"width":"20" ,"height":"25" ,"count":"30" } 

It works fine for me and without incident (I digest it on a servlet as one of the request parameters). But if, for example, in the " length " key - to add a letter or add it on the contrary, then the other parameters pass normally, and this one with a value of 0 . That is, too, everything is good, but the wrong value. Why is this option? Is it

 DetailSimply_json detail = json.fromJson(JsonString, DetailSimply_json.class); 

Shouldn't it crash with Exception somehow? Or do such checks need to be done with pens?

  • @dimchuk, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

2 answers 2

My telepathy module suggests that you use the Gson library to deserialize JSON into an object, and DetailSimply_json - your custom class, whose length field has a numeric type. I failed to reproduce your situation, the following code correctly throws a NumberFormatException :

 public class App { public static void main( String[] args ) { String json = "{\"number\": \"X\", \"text\": \"text\"}"; Gson gson = new GsonBuilder().create(); Data data = gson.fromJson(json, Data.class); System.out.println(data); } public static class Data { int number; String text; // getters and setters } } 

This suggests the idea of ​​a custom deserializer for numeric values, which was registered when creating a parser instance. Check where you get the json deserializer from and if it is configured there.

    Everything is much simpler. The deserializer uses as default values ​​in the absence of a field the values ​​specified by the default class constructor, that is, the constructor without parameters. The default constructor, as you know, exists implicitly or explicitly always. Implicit initializes all fields 0/null - accordingly you need to create a default constructor with hands and specify the value of the length field with your ilk there:

     public DetailSimply_json() { length=1; width=-1; height=0; //и т.д. } 

    Then, when deserialization fails, the deserializer will use the fields specified in the constructor.

    • @Barmaley ♦, the theory is good, but, unfortunately, wrong (at least for the latest version of Gson). Here is [my code] [1] for verification. If in line json instead of X to put number, everything works, if the text - NumberFormatException takes NumberFormatException . [1]: drive.google.com/… - fori1ton
    • @ fori1ton, I use the same option, and correctly throws out NumberFormatException - dimchuk
    • @ fori1ton, I'm sorry for the incorrectly posed question. You are right, I use Gson. With the value {\ "X \"} was parsed, but if in the json string itself instead of "number" we write "numberrrr" - the key itself is incorrectly written, then the interception can be made only as an option, as indicated by @Barmaley, with hands then the object itself is checked for valid values. Ie, if it meets somewhere in the "-1" parameters (there is no such length), then the object will not be processed further. Is it possible to somehow handle an incorrect key in Gson, and not to check the instance of the class already? - dimchuk