Esempio n. 1
0
 /**
  * Converts a model to JSON.
  *
  * @return model JSON, or empty
  */
 public String toJSON() {
   try {
     return buildObjectMapper().writeValueAsString(this.entity);
   } catch (Exception e) {
     Log.error(e.getMessage(), e);
     return Empty;
   }
 }
Esempio n. 2
0
  /**
   * Returns a new model instance.
   *
   * @param modelJSON a model in JSON format
   * @return a new model, or null
   */
  public ModelType fromJSON(String modelJSON) {
    if (StringUtils.defaultString(modelJSON).isEmpty()) {
      return null;
    }

    try {
      return buildObjectMapper().readValue(modelJSON, this.entityClass);
    } catch (Exception e) {
      Log.error(e.getMessage(), e);
      return null;
    }
  }
Esempio n. 3
0
 /**
  * Converts a model to XML.
  *
  * @return model XML, or empty
  */
 public String toXML() {
   try {
     ByteArrayOutputStream stream = new ByteArrayOutputStream();
     Marshaller m = buildJAXBContext().createMarshaller();
     m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
     m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
     m.marshal(this.entity, stream);
     return stream.toString(XML_ENCODING).trim();
   } catch (Exception e) {
     Log.error(e.getMessage(), e);
     return Empty;
   }
 }
Esempio n. 4
0
  /**
   * Returns a new model instance.
   *
   * @param modelXML a model in XML format
   * @return a new model, or null
   */
  public ModelType fromXML(String modelXML) {
    if (StringUtils.defaultString(modelXML).isEmpty()) {
      return null;
    }

    try {
      byte[] xmlData = modelXML.getBytes(XML_ENCODING);
      ByteArrayInputStream stream = new ByteArrayInputStream(xmlData);
      return (ModelType) buildJAXBContext().createUnmarshaller().unmarshal(stream);
    } catch (Exception e) {
      Log.error(e.getMessage(), e);
      return null;
    }
  }