import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.ion.IonObjectMapper; import com.fasterxml.jackson.dataformat.ion.IonReader; import com.fasterxml.jackson.dataformat.ion.IonWriter; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; public class IonExample { public static void main(String[] args) throws IOException { ObjectMapper ionMapper = new IonObjectMapper(); MyObject myObject = new MyObject("Hello", 123); StringWriter writer = new StringWriter(); IonWriter ionWriter = ionMapper.writer().writeValue(writer); ionWriter.writeObject(myObject); ionWriter.close(); String ionString = writer.toString(); System.out.println(ionString); IonReader ionReader = ionMapper.reader().withType(MyObject.class).readValue(new StringReader(ionString)); MyObject parsedObject = ionReader.readValueAs(MyObject.class); ionReader.close(); System.out.println(parsedObject.getName()); System.out.println(parsedObject.getValue()); } static class MyObject { private String name; private int value; public MyObject() {} public MyObject(String name, int value) { this.name = name; this.value = value; } } }


上一篇:
下一篇:
切换中文