Deserializes a JSON formatted string to a specific class type Note: If you get mapping exceptions you may need to provide a TypeReference @param json The string to deserialize @param pojo The class type of the object used for deserialization @return An object of the pojo type @throws
(final String json,
final Class<T> pojo)
| 98 | * @throws JSONException if the data could not be parsed |
| 99 | */ |
| 100 | public static final <T> T parseToObject(final String json, |
| 101 | final Class<T> pojo) { |
| 102 | if (json == null || json.isEmpty()) |
| 103 | throw new IllegalArgumentException("Incoming data was null or empty"); |
| 104 | if (pojo == null) |
| 105 | throw new IllegalArgumentException("Missing class type"); |
| 106 | |
| 107 | try { |
| 108 | return jsonMapper.readValue(json, pojo); |
| 109 | } catch (JsonParseException e) { |
| 110 | throw new IllegalArgumentException(e); |
| 111 | } catch (JsonMappingException e) { |
| 112 | throw new IllegalArgumentException(e); |
| 113 | } catch (IOException e) { |
| 114 | throw new JSONException(e); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Deserializes a JSON formatted byte array to a specific class type |