Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.github.jreddit.utils.restclient;
 
 public class JsonUtils {

    
Safely converts an object into string (used because sometimes JSONObject's get() method returns null).

Parameters:
obj The object to convert.
Returns:
The string.
    public static String safeJsonToString(Object obj) {
        return (obj == null ? null : obj.toString());
    }

    
Safely converts an object into an integer

Parameters:
obj The object to convert.
Returns:
an Integer representing the integer value of the Object (null if the object cannot be converted to an Integer)
    public static Integer safeJsonToInteger(Object obj) {
        Integer intValue;
        try {
            intValue = Integer.parseInt(safeJsonToString(obj));
        } catch (NumberFormatException e) {
            intValue = null;
        }
        return intValue;
    }
    
    
Safely converts an object into an double

Parameters:
obj The object to convert.
Returns:
a Double representing the double value of the Object (null if the object cannot be converted to Double)
    public static Double safeJsonToDouble(Object obj) {
        Double doubleValue;
        try {
            doubleValue = Double.parseDouble(safeJsonToString(obj));
        } catch (NumberFormatException e) {
            doubleValue = null;
        }
        return doubleValue;
    }
    
    
    
Safely converts an object into an boolean

Parameters:
obj The object to convert.
Returns:
a Boolean representing the boolean value of the Object (null if the object cannot be converted to Boolean)
    public static Boolean safeJsonToBoolean(Object obj) {
        Boolean booleanValue;
        try {
            booleanValue = Boolean.parseBoolean(safeJsonToString(obj));
        } catch (NumberFormatException e) {
            booleanValue = null;
        }
        return booleanValue;
    }
    
    
Safely converts an object into an long

Parameters:
obj The object to convert.
Returns:
a Long representing the long value of the Object (null if the object cannot be converted to Long)
    public static Long safeJsonToLong(Object obj) {
        Long longValue;
        try {
            longValue = Long.parseLong(safeJsonToString(obj));
        } catch (NumberFormatException e) {
            longValue = null;
        }
        return longValue;
    }
New to GrepCode? Check out our FAQ X