欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

解析Java的Jackson库中对象的序列化与数据泛型绑定

时间:2021-01-12 13:19:48|栏目:JAVA代码|点击:

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]

上一篇:浅谈SpringBoot2.3 新特配置文件属性跟踪

栏    目:JAVA代码

下一篇:java web个人通讯录系统设计

本文标题:解析Java的Jackson库中对象的序列化与数据泛型绑定

本文地址:http://www.codeinn.net/misctech/44223.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有