/** * Serializes an object to JSON format. * * @param object Object to be serialized. * @return Serialized form of the object in JSON format. */ public static String serialize(Object object) { return serialize(object, false); }
/** * Deserializes an object from JSON format. * * @param data Serialized form of the object. * @return An instance of the deserialized object. */ public static Object deserialize(String data) { return deserialize(null, data); }
/** * Deserializes a list of objects. * * @param <T> The list elements' class. * @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 data, Class<T> clazz) { return deserializeList(null, data, clazz); }
private void testSerializer(String typeProperty) { TestPerson obj = new TestPerson(); String s = JSONUtil.serialize(typeProperty, obj); print(s); TestPerson obj2 = (TestPerson) JSONUtil.deserialize(typeProperty, s); assertTrue(obj.equals(obj2)); List<TestPerson> list = new ArrayList<>(); list.add(obj); list.add(obj); s = JSONUtil.serialize(typeProperty, list); print(s); List<TestPerson> list2 = JSONUtil.deserializeList(typeProperty, s, TestPerson.class); assertEquals(list, list2); @SuppressWarnings("unchecked") List<TestPerson> list3 = (List<TestPerson>) JSONUtil.deserialize(typeProperty, s); assertEquals(list, list3); }
Recipient requestor = new Recipient(RecipientType.CONSUMER, "testRequestor"); PingRequest pingRequest = new PingRequest("TEST.RESPONSE", filters, requestor); String data = JSONUtil.serialize(pingRequest); pingRequest = (PingRequest) JSONUtil.deserialize(data); assertEquals("TEST.RESPONSE", pingRequest.responseEvent); assertEquals(filters, pingRequest.filters); publisherInfo.setUserId("userId"); publisherInfo.setUserName("userName"); data = JSONUtil.serialize(publisherInfo); publisherInfo = (PublisherInfo) JSONUtil.deserialize(data); assertEquals("appName", publisherInfo.getAppName()); assertEquals("consumerId", publisherInfo.getConsumerId()); data = JSONUtil.serialize(message); Message message2 = (Message) JSONUtil.deserialize(data); assertEquals(message.getType(), message2.getType());
/** * 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); } }
/** * Returns an instance of the mapper for the default type property. * * @return A mapper. */ public static ObjectMapper getMapper() { return getMapper(null); }
/** * Merges one JSON tree (srcNode) into another (destNode). * * @param destNode The tree receiving the merged node. * @param srcNode The tree supplying the nodes to merge. * @return The destination node post merging. */ public static JsonNode merge(JsonNode destNode, JsonNode srcNode) { return merge(destNode, srcNode, false); }
/** * Returns an instance of the mapper for the specified type property. * * @param typeProperty The name of the property signifying the data type. * @return A mapper. */ public static ObjectMapper getMapper(String typeProperty) { typeProperty = typeProperty == null ? DEFAULT_TYPE_PROPERTY : typeProperty; ObjectMapper mapper = mappers.get(typeProperty); return mapper == null ? initMapper(typeProperty) : mapper; }
@Test public void testSerializer() { JSONUtil.registerAlias("TestPerson", TestPerson.class); testSerializer(null); testSerializer("resourceType"); }
/** * 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); } }
merge(jsonNode, srcNode.get(fieldName), deleteOnNull); } else if (destNode instanceof ObjectNode) {
/** * Serializes an object to JSON format. * * @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(Object object, boolean prettyPrint) { return serialize(null, object, prettyPrint); }
/** * 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); }
/** * Settings in the final configuration may be overridden in systemjs.config.json files. * * @param path The path to search for configuration override files. * @param parser The JSON parser. */ private void doConfigOverrides(String path, ObjectMapper parser) { try { Resource[] resources = applicationContext.getResources(path + "systemjs.config.json"); for (Resource resource : resources) { try (InputStream is = resource.getInputStream()) { JSONUtil.merge(config, parser.readTree(is), true); } } } catch (FileNotFoundException e) { // ignore } catch (IOException e) { throw MiscUtil.toUnchecked(e); } }
/** * Serializes an object to JSON format. * * @param typeProperty The name of the property signifying the data type. * @param object Object to be serialized. * @return Serialized form of the object in JSON format. */ public static String serialize(String typeProperty, Object object) { return serialize(typeProperty, object, false); }
/** * 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); } }
JSONUtil.merge(config, webjar.getConfig(), true); } else { log.warn(() -> "No configuration information found for web jar: " + webjar.getName());
@Override public void eventCallback(String eventName, Object eventData) { String s = tboxEventResults.getValue(); if (!(eventData instanceof String)) { try { eventData = JSONUtil.serialize(eventData, true); } catch (Exception e) {} } s += "\n\n" + eventName + ":\n" + eventData; tboxEventResults.setValue(s); info("Received", eventName); if (!chkScrollLock.isChecked()) { String js = StrUtil.formatMessage("$('#%1$s').scrollTop($('#%1$s')[0].scrollHeight);", tboxEventResults.getId()); ClientUtil.eval(js); } }
/** * 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)); } }