private static Method findBuildObjectMapperForDeserializationMethod(Class<? extends BasicMessage> clazz) { try { Method m = clazz.getDeclaredMethod("buildObjectMapperForDeserialization"); return m; } catch (NoSuchMethodException e) { // the given subclass doesn't have a method to build a mapper, maybe its superclass does. // eventually we'll get to the AbstractMessage class and we know it does have one. return findBuildObjectMapperForDeserializationMethod((Class<? extends BasicMessage>) clazz.getSuperclass()); } }
private static Method findBuildObjectMapperForDeserializationMethod(Class<? extends BasicMessage> clazz) { try { Method m = clazz.getDeclaredMethod("buildObjectMapperForDeserialization"); return m; } catch (NoSuchMethodException e) { // the given subclass doesn't have a method to build a mapper, maybe its superclass does. // eventually we'll get to the AbstractMessage class and we know it does have one. return findBuildObjectMapperForDeserializationMethod((Class<? extends BasicMessage>) clazz.getSuperclass()); } }
/** * Convenience static method that converts a JSON string to a particular message object. * * @param json the JSON string * @param clazz the class whose instance is represented by the JSON string * * @return the message object that was represented by the JSON string */ public static <T extends BasicMessage> T fromJSON(String json, Class<T> clazz) { try { Method buildObjectMapperForDeserializationMethod = findBuildObjectMapperForDeserializationMethod(clazz); final ObjectMapper mapper = (ObjectMapper) buildObjectMapperForDeserializationMethod.invoke(null); if (FailOnUnknownProperties.class.isAssignableFrom(clazz)) { mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); } return mapper.readValue(json, clazz); } catch (Exception e) { throw new IllegalStateException("JSON message cannot be converted to object of type [" + clazz + "]", e); } }
/** * Convenience static method that converts a JSON string to a particular message object. * * @param json the JSON string * @param clazz the class whose instance is represented by the JSON string * * @return the message object that was represented by the JSON string */ public static <T extends BasicMessage> T fromJSON(String json, Class<T> clazz) { try { Method buildObjectMapperForDeserializationMethod = findBuildObjectMapperForDeserializationMethod(clazz); final ObjectMapper mapper = (ObjectMapper) buildObjectMapperForDeserializationMethod.invoke(null); if (FailOnUnknownProperties.class.isAssignableFrom(clazz)) { mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); } return mapper.readValue(json, clazz); } catch (Exception e) { throw new IllegalStateException("JSON message cannot be converted to object of type [" + clazz + "]", e); } }
final byte[] remainder; try (JsonParser parser = new JsonFactory().configure(Feature.AUTO_CLOSE_SOURCE, false).createParser(in)) { Method buildObjectMapperForDeserializationMethod = findBuildObjectMapperForDeserializationMethod(clazz);
final byte[] remainder; try (JsonParser parser = new JsonFactory().configure(Feature.AUTO_CLOSE_SOURCE, false).createParser(in)) { Method buildObjectMapperForDeserializationMethod = findBuildObjectMapperForDeserializationMethod(clazz);