/** * Converts this message to its JSON string representation. * * @return JSON encoded data that represents this message. */ @Override public String toJSON() { final ObjectMapper mapper = buildObjectMapperForSerialization(); try { return mapper.writeValueAsString(this); } catch (JsonProcessingException e) { throw new IllegalStateException("Object cannot be parsed as JSON.", e); } }
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()); } }
@Override public String toString() { StringBuilder str = new StringBuilder(this.getClass().getSimpleName() + ": ["); str.append("message-id="); str.append(getMessageId()); str.append(", correlation-id="); str.append(getCorrelationId()); str.append(", headers="); str.append(getHeaders()); str.append(", json-body=["); str.append(toJSON()); str.append("]]"); return str.toString(); } }
BasicMessageWithExtraData<T> results = AbstractMessage.fromJSON(input, pojo); return results; } catch (Exception e) {
/** * Deserializes a JSON string in {@link #toHawkularFormat(BasicMessage) Hawkular format}. * The JSON object is returned. * * @param nameAndJson the string to be deserialized * @return the object represented by the JSON */ public <T extends BasicMessage> BasicMessageWithExtraData<T> deserialize(String nameAndJson) { String[] nameAndJsonArray = fromHawkularFormat(nameAndJson); String name = nameAndJsonArray[0]; String json = nameAndJsonArray[1]; // The name is the actual name of the POJO that is used to deserialize the JSON. // If not fully qualified with a package then assume it is in our package. if (name.indexOf(".") == -1) { name = String.format("%s.%s", API_PKG, name); } try { Class<T> pojo = (Class<T>) Class.forName(name); return new BasicMessageWithExtraData(AbstractMessage.fromJSON(json, pojo), null); } catch (Exception e) { throw new RuntimeException("Cannot deserialize: [" + nameAndJson + "]", e); } }
@Override public String toString() { StringBuilder str = new StringBuilder(this.getClass().getSimpleName() + ": ["); str.append("message-id="); str.append(getMessageId()); str.append(", correlation-id="); str.append(getCorrelationId()); str.append(", headers="); str.append(getHeaders()); str.append(", json-body=["); str.append(toJSON()); str.append("]]"); return str.toString(); } }
T basicMessage = AbstractMessage.fromJSON(receivedBody, basicMessageClass); retVal = new BasicMessageWithExtraData<T>(basicMessage, null); retVal = AbstractMessage.fromJSON(receivedBody, basicMessageClass); } else { throw new Exception("Unexpected implementation of " + Message.class.getName() + ": "
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()); } }
/** * Converts this message to its JSON string representation. * * @return JSON encoded data that represents this message. */ @Override public String toJSON() { final ObjectMapper mapper = buildObjectMapperForSerialization(); try { return mapper.writeValueAsString(this); } catch (JsonProcessingException e) { throw new IllegalStateException("Object cannot be parsed as JSON.", e); } }
T basicMessage = AbstractMessage.fromJSON(receivedBody, basicMessageClass); retVal = new BasicMessageWithExtraData<T>(basicMessage, null); retVal = AbstractMessage.fromJSON(receivedBody, basicMessageClass); } else { throw new Exception("Unexpected implementation of " + Message.class.getName() + ": "
/** * 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);