/** * Returns an instance of the mapper for the default type property. * * @return A mapper. */ public static ObjectMapper getMapper() { return getMapper(null); }
/** * Deserializes a list of objects. * * @param <T> The list elements' class. * @param typeProperty The name of the property signifying the data type. * @param data Serialized form of the list in JSON format. * @param clazz The class of objects found in the list. * @return A list of objects of the specified type. */ public static <T> List<T> deserializeList(String typeProperty, String data, Class<T> clazz) { try { return getMapper(typeProperty).readValue(data, new TypeReference<List<T>>() {}); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Serializes an object to JSON format. * * @param typeProperty The name of the property signifying the data type. * @param object Object to be serialized. * @param prettyPrint If true, format output for display. * @return Serialized form of the object in JSON format. */ public static String serialize(String typeProperty, Object object, boolean prettyPrint) { try { ObjectMapper mapper = getMapper(typeProperty); return prettyPrint ? mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object) : mapper.writeValueAsString(object); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Creates a SmartManifest object from an input stream. * * @param stream Input stream. * @throws IOException An IO exception. */ @SuppressWarnings("unchecked") public SmartManifest(InputStream stream) throws IOException { try (InputStream is = stream) { init(JSONUtil.getMapper().readValue(is, Map.class)); } }
/** * Sets the date format to be used when serializing dates. * * @param typeProperty The name of the property signifying the data type. * @param dateFormat Date format to use. */ public static void setDateFormat(String typeProperty, DateFormat dateFormat) { getMapper(typeProperty).setDateFormat(dateFormat); }
/** * Deserializes an object from JSON format. * * @param typeProperty The name of the property signifying the data type. * @param data Serialized form of the object. * @return An instance of the deserialized object. */ public static Object deserialize(String typeProperty, String data) { if (data == null) { return null; } if (data.startsWith("[")) { return deserializeList(typeProperty, data, Object.class); } try { return getMapper(typeProperty).readValue(data, Object.class); } catch (Exception e) { throw new RuntimeException(e); } }