我试图理解Jackson(JSON),并首先创建了一个简单的POJO,并使用Jackson的ObjectMapper
来回了。
之后,我想检查是否可以使用byte[]
将“普通” Serialization
(使用Java JSON
创建)转换为Jackson
。
这是完整的代码:
package com.example.domain;
import java.io.Serializable;
import java.util.List;
public class Student implements Serializable {
private String studentName;
private int studentID;
List<String> subjects;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public List<String> getSubjects() {
return subjects;
}
public void setSubjects(List<String> subjects) {
this.subjects = subjects;
}
@Override
public String toString() {
return "Student [studentName=" + studentName + ", studentID=" + studentID + ", subjects=" + subjects + "]";
}
}
以下是主要应用
package com.example.json;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import com.example.domain.Student;
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainApp1 {
public static void main(String[] args) throws IOException {
Student s = new Student();
s.setStudentID(1);
s.setStudentName("Student1");
List<String> subs = new ArrayList<>();
subs.add("Physics");
subs.add("Chemistry");
subs.add("Maths");
s.setSubjects(subs);
ObjectMapper mapper = new ObjectMapper();
// Here, we are converting Student object
// into bytes[] using Java Serialization
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
out = new ObjectOutputStream(bos);
out.writeObject(s);
out.flush();
byte[] objBytes = bos.toByteArray();
bos.close();
// Now, seeing, if jackson's readValue() can "deserialize"
// the bytes[] which we created using Java Serialization
// as above.
Student s3 = mapper.readValue(objBytes, Student.class);
System.out.println("s3 is " + s3.toString());
}
}
我已经创建了一个POJO Student
,我想使用Jackson API's
将其转换为JSON。
我没有使用直接的POJO传递给Jackson的readValue()
,而是首先使用Java Serialization
将POJO序列化为byte []。
但是,当我运行它时,它给了我以下异常:
Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unexpected character ('¬' (code 172)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: (byte[])"��srcom.example.domain.Studentb���O*�I studentIDLstudentNametLjava/lang/String;LsubjectstLjava/util/List;xptStudent1srjava.util.ArrayListx����a�IsizexpwtPhysicst ChemistrytMathsx"; line: 1, column: 2]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:693)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:591)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2630)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:832)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:729)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4141)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4000)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3091)
at com.example.json.MainApp1.main
所以我的问题是:
我们不能使用Java Serialization
使用byte []将Jackson
转换为JSON吗?
我要问byte[]
,如果要求是将字节从一个应用程序通过网络传输到另一应用程序,那该怎么办,那么接收应用程序将如何重构JSON?
对此有任何见解都会有很大帮助。