return Response.ok(mapper.writeValueAsString(object)).build(); } catch (JsonProcessingException e) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
/** * Creates an error response using the given exception. * * @param e the exception to be converted into {@link Error} and encoded into XML * @return the response */ private static Response createErrorResponse(S3Exception e) { S3Error errorResponse = new S3Error(e.getResource(), e.getErrorCode()); // Need to explicitly encode the string as XML because Jackson will not do it automatically. XmlMapper mapper = new XmlMapper(); try { return Response.status(e.getErrorCode().getStatus()) .entity(mapper.writeValueAsString(errorResponse)).build(); } catch (JsonProcessingException e2) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR) .entity("Failed to encode XML: " + e2.getMessage()).build(); } }
public String postXml(String url, Object object) { try { httpClient.getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost post = new HttpPost(url); StringEntity entity = new StringEntity( new XmlMapper().writeValueAsString(object), "utf-8"); entity.setContentType("application/xml; charset=utf-8"); post.setEntity(entity); post.releaseConnection(); // Here we go! return EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
public String toXml(Object o) throws JsonProcessingException { return xmlMapper.writeValueAsString(o); }
public static String toString(Object object, String mediaType) { try { if (MediaType.APPLICATION_XML.equals(mediaType)) { return xmlMapper.writeValueAsString(object); } else if (MediaType.APPLICATION_JSON.equals(mediaType)) { return mapper.writeValueAsString(object); } } catch (JsonProcessingException e) { log.error(e.getMessage()); } return null; }
public String toXml(Object o) throws JsonProcessingException { String xml = xmlMapper.writeValueAsString(o); return xml; }
public static String toXml(Object message) { try { return mapper.writeValueAsString(message); } catch (Exception e) { log.error("generate text message xml exception", e); return null; } } }
@Override public String serialize(Object obj) { try { return _mapper.writeValueAsString(obj); } catch (RuntimeException | Error ex) { throw ex; } catch (Exception ex) { throw new SerializationException(ex); } }
public static String toXmlStatic(Object o) throws XmlUtilsException { String xml; try { xml = staticXmlMapper.writeValueAsString(o); } catch (Exception e) { throw new XmlUtilsException("Error encoding object to XML", e); } return xml; }
@Override public String toXml(Object obj, boolean pretty) { try { if (pretty) { return getXmlMapperPretty().writeValueAsString(obj); } else { return getXmlMapper().writeValueAsString(obj); } } catch (Exception ex) { throw SpincastStatics.runtimize(ex); } }
public class JacksonXmlArray { static class Document { @JsonProperty @JacksonXmlElementWrapper(localName = "events") @JacksonXmlProperty(localName = "event") List<String> events = Arrays.asList("a", "b"); } public static void main(String[] args) throws JsonProcessingException { final XmlMapper xmlMapper = new XmlMapper(); System.out.println(xmlMapper.writeValueAsString(new Document())); } }
public static String toXml(Object obj) { if (obj instanceof JreJsonElement) { obj = ((JreJsonElement) obj).toNative(); } try { return mapper.writeValueAsString(obj); } catch (JsonProcessingException e) { throw new JsonException("Failed to encode as XML: " + e.getMessage()); } }
public class Application { public static void main(String[] args) throws Exception{ User user = new User(); user.setName("nitin"); String n = "1111"; //this value comes from API SimpleModule module = new SimpleModule(); module.addSerializer(User.class, new UserSerializer(n)); XmlMapper xmlMapper = new XmlMapper(); xmlMapper.registerModule(module); String xml = xmlMapper.writeValueAsString(user); System.out.println(xml); }
XmlSerializerProvider provider = new XmlSerializerProvider(new XmlRootNameLookup()); provider.setNullValueSerializer(new NullSerializer()); XmlMapper xmlMapper = new XmlMapper(); xmlMapper.setSerializerProvider(provider); System.out.println(xmlMapper.writeValueAsString(new Entity()));
// read fields final PdfReader reader = new PdfReader("/path/to/my.pdf"); final AcroFields fields = reader.getAcroFields(); final Map<String, Object> values = new HashMap<>(); for (String fieldName : (Set<String>) fields.getFields().keySet()) { values.put(fieldName, fields.getField(fieldName)); } // write final XmlMapper mapper = new XmlMapper(); final String result = mapper.writeValueAsString(values); System.out.println(result);
@Test(groups = "fast") public void testSerializationNullValue() throws Exception { Adjustment adjustment = TestUtils.createRandomAdjustment(); adjustment.setRevenueScheduleType(null); String xml = xmlMapper.writeValueAsString(adjustment); assert(!xml.contains("<revenue_schedule_type>")); }
@Test(groups = "fast") public void testSerializationNullValue() throws Exception { Adjustment adjustment = TestUtils.createRandomAdjustment(); adjustment.setRevenueScheduleType(null); String xml = xmlMapper.writeValueAsString(adjustment); assert(!xml.contains("<revenue_schedule_type>")); }
@Test public void serializeUnwrappedList () throws Exception { // then assertThat(xmlMapper.writeValueAsString(givenRootObject())).isEqualTo(rootXml); }
private void testSerializationCommon(RevenueScheduleType revenueScheduleType, String expectedSerializedValue) throws Exception { Adjustment adjustment = TestUtils.createRandomAdjustment(); adjustment.setRevenueScheduleType(revenueScheduleType); String xml = xmlMapper.writeValueAsString(adjustment); assert(xml.contains("<revenue_schedule_type>" + expectedSerializedValue + "</revenue_schedule_type>")); }
private void testSerializationCommon(RevenueScheduleType revenueScheduleType, String expectedSerializedValue) throws Exception { Adjustment adjustment = TestUtils.createRandomAdjustment(); adjustment.setRevenueScheduleType(revenueScheduleType); String xml = xmlMapper.writeValueAsString(adjustment); assert(xml.contains("<revenue_schedule_type>" + expectedSerializedValue + "</revenue_schedule_type>")); }