com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver Java Examples

The following examples show how to use com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XStreamObject2Json.java    From maven-framework-project with MIT License 6 votes vote down vote up
@Test
public void test() {
	Product product = new Product();
	
	product.setId("123");
	product.setName("Banana");
	product.setPrice(23.00);
	
	XStream xstream = new XStream(new JettisonMappedXmlDriver());
	
       /*XStream xstream = new XStream(new JsonHierarchicalStreamDriver());*/
       
       /*XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
           public HierarchicalStreamWriter createWriter(Writer writer) {
               return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
           }
       });*/
       
       xstream.setMode(XStream.NO_REFERENCES);
       xstream.alias("product", Product.class);
       System.out.println(xstream.toXML(product));	
}
 
Example #2
Source File: XMLToFromJSONXStream.java    From levelup-java-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void convert_json_to_xml() {
	
	
	// create a new xstream object w/json provider
	XStream xstreamForJson = new XStream(new JettisonMappedXmlDriver());
	xstreamForJson.setMode(XStream.NO_REFERENCES);
	xstreamForJson.alias("status", Status.class);		
	Status status = (Status) xstreamForJson.fromXML(JSON);

	// create xstream object for reading xml
	XStream xstream = new XStream();
	xstream.setMode(XStream.NO_REFERENCES);

	assertEquals(XML, xstream.toXML(status));
}
 
Example #3
Source File: XStreamMarshallerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void jettisonDriver() throws Exception {
	marshaller.setStreamDriver(new JettisonMappedXmlDriver());
	Writer writer = new StringWriter();
	marshaller.marshal(flight, new StreamResult(writer));
	assertEquals("Invalid result", "{\"flight\":{\"flightNumber\":42}}", writer.toString());
	Object o = marshaller.unmarshal(new StreamSource(new StringReader(writer.toString())));
	assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
	Flight unflight = (Flight) o;
	assertNotNull("Flight is null", unflight);
	assertEquals("Number is invalid", 42L, unflight.getFlightNumber());
}
 
Example #4
Source File: XStreamMarshallerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void jettisonDriver() throws Exception {
	marshaller.setStreamDriver(new JettisonMappedXmlDriver());
	Writer writer = new StringWriter();
	marshaller.marshal(flight, new StreamResult(writer));
	assertEquals("Invalid result", "{\"flight\":{\"flightNumber\":42}}", writer.toString());
	Object o = marshaller.unmarshal(new StreamSource(new StringReader(writer.toString())));
	assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
	Flight unflight = (Flight) o;
	assertNotNull("Flight is null", unflight);
	assertEquals("Number is invalid", 42L, unflight.getFlightNumber());
}
 
Example #5
Source File: XStreamMarshallerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void jettisonDriver() throws Exception {
	marshaller.setStreamDriver(new JettisonMappedXmlDriver());
	Writer writer = new StringWriter();
	marshaller.marshal(flight, new StreamResult(writer));
	assertEquals("Invalid result", "{\"flight\":{\"flightNumber\":42}}", writer.toString());
	Object o = marshaller.unmarshal(new StreamSource(new StringReader(writer.toString())));
	assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
	Flight unflight = (Flight) o;
	assertNotNull("Flight is null", unflight);
	assertEquals("Number is invalid", 42L, unflight.getFlightNumber());
}
 
Example #6
Source File: JsonSerializer.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private static <K, V> XStream xStream(Map<K, V> map, List jsonConverters) {
    try {
        final XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("cmap", map.getClass());

        registerChronicleMapConverter(map, xstream);
        xstream.registerConverter(new ByteBufferConverter());
        xstream.registerConverter(new ValueConverter());
        xstream.registerConverter(new StringBuilderConverter());
        xstream.registerConverter(new CharSequenceConverter());

        for (Object c : jsonConverters) {
            if (c instanceof Converter) {
                xstream.registerConverter((Converter) c);
            } else {
                LOG.warn("Skipping Converter of type class=" + c.getClass().getName() + " as " +
                        " expecting an object of type com.thoughtworks.xstream.converters" +
                        ".Converter");
            }
        }

        return xstream;
    } catch (NoClassDefFoundError e) {
        throw new RuntimeException(logErrorSuggestXStreem, e);
    }
}
 
Example #7
Source File: ChronicleMapImportExportTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromHashMap() throws IOException, InterruptedException {

    File file = new File(TMP + "/chronicle-map-" + System.nanoTime() + ".json");
    System.out.println(file.getCanonicalFile());

    File file2 = new File(TMP + "/chronicle-map-2" + System.nanoTime() + ".json");
    System.out.println(file2.getCanonicalFile());

    HashMap<Integer, String> map = new HashMap<Integer, String>();
    map.put(1, "one");
    map.put(2, "two");

    final XStream xstream = new XStream(new JettisonMappedXmlDriver());
    xstream.setMode(XStream.NO_REFERENCES);

    xstream.toXML(map, new FileOutputStream(file));

    try (ChronicleMap<Integer, String> expected = ChronicleMapBuilder
            .of(Integer.class, String.class)
            .averageValueSize(10)
            .entries(1000)
            .create()) {

        expected.put(1, "one");
        expected.put(2, "two");

        expected.getAll(file2);
        expected.putAll(file2);

        Assert.assertEquals(2, expected.size());
        Assert.assertEquals("one", expected.get(1));
        Assert.assertEquals("two", expected.get(2));
    }

    file.deleteOnExit();
}
 
Example #8
Source File: XStreamJson2Object.java    From maven-framework-project with MIT License 5 votes vote down vote up
@Test
public void test() {
	String json = "{\"product\":{\"name\":\"Banana\",\"id\":123"
		    + ",\"price\":23.0}}";
	XStream xstream = new XStream(new JettisonMappedXmlDriver());
	xstream.alias("product", Product.class);
	Product product = (Product)xstream.fromXML(json);
	System.out.println(product.getName());
}
 
Example #9
Source File: XMLToFromJSONXStream.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void convert_xml_to_json() {

	// create xstream object for reading xml
	XStream xstream = new XStream();
	xstream.setMode(XStream.NO_REFERENCES);
	Status status = (Status) xstream.fromXML(XML);

	// create a new xstream object w/json provider
	XStream xstreamForJson = new XStream(new JettisonMappedXmlDriver());
	xstreamForJson.setMode(XStream.NO_REFERENCES);
	xstream.alias("status", Status.class);
	
	assertEquals(JSON, xstreamForJson.toXML(status));
}
 
Example #10
Source File: SimpleXstreamInitializer.java    From tutorials with MIT License 4 votes vote down vote up
public XStream getXstreamJettisonMappedInstance() {
    return new XStream(new JettisonMappedXmlDriver());
}