use of com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver in project camel by apache.
the class XStreamConfigurationTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
XStreamDataFormat xstreamDefinition = new XStreamDataFormat();
Map<String, String> aliases = new HashMap<String, String>();
aliases.put("purchase-order", PurchaseOrder.class.getName());
xstreamDefinition.setAliases(aliases);
xstreamDefinition.setPermissions(PurchaseOrder.class, PurchaseHistory.class);
List<String> converters = new ArrayList<String>();
converters.add(PurchaseOrderConverter.class.getName());
converters.add(CheckMethodInjection.class.getName());
converters.add(CheckConstructorInjection.class.getName());
xstreamDefinition.setConverters(converters);
Map<String, String[]> implicits = new HashMap<String, String[]>();
implicits.put(PurchaseHistory.class.getName(), new String[] { "history" });
xstreamDefinition.setImplicitCollections(implicits);
from("direct:marshal").marshal(xstreamDefinition).to("mock:result");
from("direct:unmarshal").unmarshal(xstreamDefinition).to("mock:result");
xstreamDefinition = new XStreamDataFormat();
xstreamDefinition.setDriver("json");
aliases = new HashMap<String, String>();
aliases.put("purchase-order", PurchaseOrder.class.getName());
xstreamDefinition.setAliases(aliases);
xstreamDefinition.setPermissions(PurchaseOrder.class, PurchaseHistory.class);
converters = new ArrayList<String>();
converters.add(PurchaseOrderConverter.class.getName());
xstreamDefinition.setConverters(converters);
from("direct:marshal-json").marshal(xstreamDefinition).to("mock:result");
from("direct:unmarshal-json").unmarshal(xstreamDefinition).to("mock:result");
org.apache.camel.dataformat.xstream.XStreamDataFormat xStreamDataFormat = new org.apache.camel.dataformat.xstream.XStreamDataFormat();
xStreamDataFormat.setXstreamDriver(new JsonHierarchicalStreamDriver());
xStreamDataFormat.setPermissions("+6org.apache.camel.dataformat.xstream.*");
from("direct:myDriver").marshal(xStreamDataFormat).to("mock:result");
}
};
}
use of com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver in project camel by apache.
the class XStreamDataFormatDriverConfigTest method testJson.
@Test
public void testJson() {
PurchaseOrder purchaseOrder = new PurchaseOrder();
purchaseOrder.setName("foo");
XStreamDataFormat xStreamDataFormat = new XStreamDataFormat();
xStreamDataFormat.setXstreamDriver(new JsonHierarchicalStreamDriver());
XStream xStream = xStreamDataFormat.createXStream(context.getClassResolver(), context.getApplicationContextClassLoader());
String marshalledOrder = xStream.toXML(purchaseOrder);
assertEquals("{", marshalledOrder.substring(0, 1));
}
use of com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver in project spring-framework by spring-projects.
the class XStreamMarshallerTests method jsonDriver.
@Test
public void jsonDriver() throws Exception {
marshaller.setStreamDriver(new JsonHierarchicalStreamDriver() {
@Override
public HierarchicalStreamWriter createWriter(Writer writer) {
return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE, new JsonWriter.Format(new char[0], new char[0], JsonWriter.Format.SPACE_AFTER_LABEL | JsonWriter.Format.COMPACT_EMPTY_ELEMENT));
}
});
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
assertEquals("Invalid result", "{\"flightNumber\": 42}", writer.toString());
}
Aggregations