@Test public void iaasMessageToJSONTest() { IaaSMessage im = new IaaSMessage(); im.setName("name"); im.setTimestamp(22l); im.setUnits("units"); im.setValue(0.1); String out = Converter.iaasMessageToJSON(im); im = Converter.iaasMessageFromJSONToObject(json); assertEquals("name", im.getName()); assertEquals("units", im.getUnits()); assertEquals(22l, im.getTimestamp()); assertEquals(0.1, im.getValue(), 0.00001); }
public static String iaasMessageToJSON(IaaSMessage im) { try { JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[] {IaaSMessage.class}, null); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); marshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, false); ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.marshal(im, out); String output = out.toString(); logger.debug("Converting object to JSON: "); logger.debug(output); return output; } catch(Exception exception) { logger.info("Error converting IaaSMessage object to JSON: " + exception.getMessage()); return null; } }
Converter { public static String iaasMessageToJSON(IaaSMessage im) { try { JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[] {IaaSMessage.class}, null); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); marshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, false); ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.marshal(im, out); String output = out.toString(); logger.debug("Converting object to JSON: "); logger.debug(output); return output; } catch(Exception exception) { logger.info("Error converting IaaSMessage object to JSON: " + exception.getMessage()); return null; } } }
Converter { public static String iaasMessageToJSON(IaaSMessage im) { try { JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[] {IaaSMessage.class}, null); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); marshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, false); ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.marshal(im, out); String output = out.toString(); logger.debug("Converting object to JSON: "); logger.debug(output); return output; } catch(Exception exception) { logger.info("Error converting IaaSMessage object to JSON: " + exception.getMessage()); return null; } }  }
Converter { public static String iaasMessageToJSON(IaaSMessage im) { try { JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[] {IaaSMessage.class}, null); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); marshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, false); ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.marshal(im, out); String output = out.toString(); logger.debug("Converting object to JSON: "); logger.debug(output); return output; } catch(Exception exception) { logger.info("Error converting IaaSMessage object to JSON: " + exception.getMessage()); return null; } }  static IaaSMessage iaasMessageFromJSONToObject(String json); static String iaasMessageToJSON(IaaSMessage im); }
Converter { public static String iaasMessageToJSON(IaaSMessage im) { try { JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[] {IaaSMessage.class}, null); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); marshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, false); ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.marshal(im, out); String output = out.toString(); logger.debug("Converting object to JSON: "); logger.debug(output); return output; } catch(Exception exception) { logger.info("Error converting IaaSMessage object to JSON: " + exception.getMessage()); return null; } }  static IaaSMessage iaasMessageFromJSONToObject(String json); static String iaasMessageToJSON(IaaSMessage im);  }