0
public static void main(String[] args){
        String str = "{\"a\":\"48.0\", \"b\":48.0, \"c\":\"this is a string\"}";
        Gson gson = new Gson();
        JsonObject obj = gson.fromJson(str, JsonObject.class);
        for (Object key : obj.keySet()){
            String keystr = key.toString();
            if (obj.get(keystr).isJsonPrimitive()){
                if (obj.get(keystr).getAsJsonPrimitive().isNumber()){
                    System.out.println(keystr+", getting as Number: "+obj.get(keystr).getAsFloat());
                }else if (obj.get(keystr).getAsJsonPrimitive().isString()){
                    try{
                        System.out.println(keystr+", getting as String-Number: "+obj.get(keystr).getAsFloat());
                    }catch (NumberFormatException e){
                        System.out.println(keystr+", getting as String-String: "+obj.get(keystr).getAsString());
                    }
                }
            }
        }
    }

Output:

a, getting as String-Number: 48.0
b, getting as Number: 48.0
c, getting as String-String: this is a string

I have a JsonObject which is much more complicated than the above test case. The gist of it is that the data is stored as floats in a string (example a). Occasionally, there are actual strings inside the data string such as "null" or "undef". Is there a better way to test that the string is truly a string without relying on try-catch NumberFormatException or external libraries?

  • 1
    You must parse it anyway (it does not matter if it's explicit or implicit; straight flow or try/catch flow). Not sure how Gson relates to the issue, but you try/catch seems to be the most natural way of doing that if not involving any other third-party code: https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java . Additionally, you might want to check if the given string _looks like_ a float and use regular expressions (`java.util.Pattern` or self-made FSM to check if might be a float within a real range). I don't anything wrong with your current approach. – terrorrussia-keeps-killing Jul 21 '21 at 16:26
  • Additionally, https://guava.dev/releases/22.0/api/docs/com/google/common/primitives/Floats.html#tryParse-java.lang.String- - Floats.tryParse that either returns a boxed float or a null. Also, what about BigDecimal? – terrorrussia-keeps-killing Jul 21 '21 at 16:28
  • well, i m trying to not overuse try catches. kind of like develop coding skills so i dont keep taking the easy way out? The Pattern matching sounds like a good idea. Would pattern matching be more expensive than a try-catch? – experiment unit 1998X Jul 21 '21 at 16:29
  • 1
    Using regular expressions may be insufficient covering not all cases or have false positives. Anyway, I would stick to your current approach as I don't see anything wrong with it. UPD: `Floats.tryParse` at least for Guava 19, also uses try/catch encapsulated in a method. JDK built-ins are more mature and robust to use, so this is also why I find their approach good. – terrorrussia-keeps-killing Jul 21 '21 at 16:33

0 Answers0