解析Java的Jackson库中对象的序列化与数据泛型绑定
程序员文章站
2024-03-31 19:46:34
jackson对象序列化
这里将介绍将java对象序列化到一个json文件,然后再读取json文件获取转换为对象。在这个例子中,创建了student类。创建将有学生对象以...
jackson对象序列化
这里将介绍将java对象序列化到一个json文件,然后再读取json文件获取转换为对象。在这个例子中,创建了student类。创建将有学生对象以json表示在一个student.json文件。
创建一个名为jacksontester在java类文件在 c:\>jackson_workspace.
文件: jacksontester.java
import java.io.file; import java.io.ioexception; import org.codehaus.jackson.jsongenerationexception; import org.codehaus.jackson.jsonparseexception; import org.codehaus.jackson.map.jsonmappingexception; import org.codehaus.jackson.map.objectmapper; public class jacksontester { public static void main(string args[]){ jacksontester tester = new jacksontester(); try { student student = new student(); student.setage(10); student.setname("mahesh"); tester.writejson(student); student student1 = tester.readjson(); system.out.println(student1); } catch (jsonparseexception e) { e.printstacktrace(); } catch (jsonmappingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } private void writejson(student student) throws jsongenerationexception, jsonmappingexception, ioexception{ objectmapper mapper = new objectmapper(); mapper.writevalue(new file("student.json"), student); } private student readjson() throws jsonparseexception, jsonmappingexception, ioexception{ objectmapper mapper = new objectmapper(); student student = mapper.readvalue(new file("student.json"), student.class); return student; } } class student { private string name; private int age; public student(){} public string getname() { return name; } public void setname(string name) { this.name = name; } public int getage() { return age; } public void setage(int age) { this.age = age; } public string tostring(){ return "student [ name: "+name+", age: "+ age+ " ]"; } }
验证结果
使用 javac 编译如下类:
复制代码 代码如下:
c:\jackson_workspace>javac jacksontester.java
现在运行jacksontester看到的结果:
复制代码 代码如下:
c:\jackson_workspace>java jacksontester
验证输出结果
复制代码 代码如下:
student [ name: mahesh, age: 10 ]
jackson数据绑定泛型
在简单的数据绑定中,我们使用string作为关键对象,并作为一个值对象映射类。相反,我们可以使用具体的java对象和类型强制转换到json使用。
考虑下面的例子使用一个类的userdata来保存用户专用数据。
创建一个名为jacksontester在java类文件目录 c:\>jackson_workspace.
文件名: jacksontester.java
import java.io.file; import java.io.ioexception; import java.util.arrays; import java.util.hashmap; import java.util.list; import java.util.map; import org.codehaus.jackson.jsongenerationexception; import org.codehaus.jackson.jsonparseexception; import org.codehaus.jackson.map.jsonmappingexception; import org.codehaus.jackson.map.objectmapper; import org.codehaus.jackson.type.typereference; public class jacksontester { public static void main(string args[]){ jacksontester tester = new jacksontester(); try { objectmapper mapper = new objectmapper(); map userdatamap = new hashmap(); userdata studentdata = new userdata(); int[] marks = {1,2,3}; student student = new student(); student.setage(10); student.setname("mahesh"); // java object studentdata.setstudent(student); // java string studentdata.setname("mahesh kumar"); // java boolean studentdata.setverified(boolean.false); // array studentdata.setmarks(marks); typereference ref = new typereference>() { }; userdatamap.put("studentdata1", studentdata); mapper.writevalue(new file("student.json"), userdatamap); //{ // "studentdata1": // { // "student": // { // "name":"mahesh", // "age":10 // }, // "name":"mahesh kumar", // "verified":false, // "marks":[1,2,3] // } //} userdatamap = mapper.readvalue(new file("student.json"), ref); system.out.println(userdatamap.get("studentdata1").getstudent()); system.out.println(userdatamap.get("studentdata1").getname()); system.out.println(userdatamap.get("studentdata1").getverified()); system.out.println(arrays.tostring(userdatamap.get("studentdata1").getmarks())); } catch (jsonparseexception e) { e.printstacktrace(); } catch (jsonmappingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } } class student { private string name; private int age; public student(){} public string getname() { return name; } public void setname(string name) { this.name = name; } public int getage() { return age; } public void setage(int age) { this.age = age; } public string tostring(){ return "student [ name: "+name+", age: "+ age+ " ]"; } } class userdata { private student student; private string name; private boolean verified; private int[] marks; public userdata(){} public student getstudent() { return student; } public void setstudent(student student) { this.student = student; } public string getname() { return name; } public void setname(string name) { this.name = name; } public boolean getverified() { return verified; } public void setverified(boolean verified) { this.verified = verified; } public int[] getmarks() { return marks; } public void setmarks(int[] marks) { this.marks = marks; } }
验证输出
使用javac编译如下类:
复制代码 代码如下:
c:\jackson_workspace>javac jacksontester.java
现在运行jacksontester看到的结果:
复制代码 代码如下:
c:\jackson_workspace>java jacksontester
验证输出
student [ name: mahesh, age: 10 ] mahesh kumar false [1, 2, 3]