google / gson

A Java serialization/deserialization library to convert Java Objects into JSON and back
Apache License 2.0
23.37k stars 4.28k forks source link

I think we can add some methods for validation json or getValue. #1171

Closed HalfWater closed 2 years ago

HalfWater commented 7 years ago

I want to use basic gson method to parse my jsonstring without JavaBeans,but i found the results is not my need,maybe i used it with wrong, or it's bugs.

Sample 1. There is no validation method to check the arguments str is or not Json String, and i validation it like this, public static boolean isJsonString(String json) {

       try {    
           new JsonParser().parse(json);  
           return true;    
       } catch (JsonParseException e) {    
           System.out.println("bad json: " + json);    
           return false;    
       } catch( NullPointerException e){
           return false;
       } catch( Exception e ){
           return false;
       }
    }

Sample 2. if i used toString ,the result will add "",it will start with " and end with ",i must check the json type first,and parse it by type. I think we can add a method like getValue(String key) , and it can casted by java basic type, like (String)getValue("Type"); If this, i can define a var like Class[] classes=new Class[]{String.class, int.class, Number.class, char.class};

Cannot cast from JsonElement to String like String aa= (String)obj.get(nodeName); it's only used like String aa= obj.get(nodeName).getAsString();

Sometimes, i know all objects type,and i want loop it to get value in JsonObject.entrySet(); ,but not getValue for every nodeName.

And about used with toString, like {"AA":"BB"} A: nodeValue = obj.get("AA").getAsJsonPrimitive().toString(); B: nodeValue = obj.get("AA").getAsJsonPrimitive().getAsString();

A is "BB" ,start with " and end with". B is BB ,it's right.

Sample 3. I have a json like {"HEADER":{"id":18,"amount":200},"LINE":[{"id":109,"amount":35,"type":null},{"id":110,"amount":90,"type":"INV"}]}

i think we can create a method like (int)getComplexValue("HEADER/id") , the result is 18. (int)getComplexValue("LINE/2@amount"), the result is 90.

Maybe we can define the args with other simple style,because it will occupied two characters.

Sample 4. In Gson lib, the string start and end with " or ',like "SAMPLE" or 'SAMPLE' , it parse active, and GsonType is JsonPrimitive。 But in org.json define, In JSON, they take on these forms: An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket).

start and end with " or ' pairs is not JSON, so it's not JsonPrimitive.

inder123 commented 6 years ago

Your Sample 1 works.

For Sample 4, you are asking for a way to extract specific fields (probably from a DOM tree of JsonElements returned by JsonParser) using something like JsonPath. It is a good idea. Can you consider writing a PR that implements it?

Marcono1234 commented 2 years ago

Sample 1: Unfortunately JsonParser always parses in 'lenient mode'. If you just want to verify that the input is valid JSON without using the parsed result, you can create a JsonReader then call skipValue() and afterwards check that peek() == JsonToken.END_DOCUMENT. See this Stack Overflow answer for more details.

Sample 2: In general do not rely on toString() returning a JSON value, this behavior is not guaranteed at the moment.

Sample 3: That sounds like a feature request to all looking up values by JSON path, see #1443.

Sample 4: It sounds like a top level string value, such as "value". This is allowed in the latest JSON specifications (since RFC 7159). Previously only JSON arrays and objects were permitted as top level values.


I am closing this issue because I hope all of the questions / suggestions have been answered.