Reputation: 11
My task is to describe the classes Student and Academic group (with the field - an array of students). Create objects. Provide file creation and reading from files using three approaches:
serialization and deserialization in JSON (by means of XStream).
I serialize an Academic group that has students array list :
public class AcademicGroup {
private List<Student> students;
public AcademicGroup() {
this.students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public Student[] getStudents() {
return students.toArray(new Student[0]);
}
}
and student:
public class Student {
private String name;
private String group;
public Student(String name, String group) {
this.name = name;
this.group = group;
}
public String getName() {
return name;
}
public String getGroup() {
return group;
}
@Override
public String toString() {
return name + " " + group;
}
}
serialize function
public static void studentJsonSerialization() {
XStream xStream = new XStream(new JsonHierarchicalStreamDriver());
List<Student> students = Arrays.asList(
new Student("Student1", "KN-221A"),
new Student("Student2", "KN-221A"),
new Student("Student3", "KN-221A")
);
AcademicGroup academicGroup = new AcademicGroup();
for (Student student : students) {
academicGroup.addStudent(student);
}
xStream.alias("academicGroup", AcademicGroup.class);
String xml = xStream.toXML(academicGroup);
try (FileWriter fw = new FileWriter("AcademicGroup.json");
PrintWriter out = new PrintWriter(fw)) {
out.println(xml);
}
catch (IOException e) {
e.printStackTrace();
}
}
the file AcademicGroup.json that I get:
{"academicGroup": {
"students": [
{
"name": "Student1",
"group": "KN-221A"
},
{
"name": "Student2",
"group": "KN-221A"
},
{
"name": "Student3",
"group": "KN-221A"
}
]
}}
deserialization function:
public static void studentJsonDeserialization() {
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.addPermission(AnyTypePermission.ANY);
xStream.alias("academicGroup", AcademicGroup.class);
AcademicGroup newAcademicGroup = (AcademicGroup) xStream.fromXML(new File("AcademicGroup.json"));
for (Student student : newAcademicGroup.getStudents()) {
System.out.println(student.toString());
}
}
I need to print json objects in console, but I get an error message
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException:
---- Debugging information ----
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : name
class : java.util.ArrayList
required-type : java.util.ArrayList
converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter
path : /academicGroup/students/name
line number : -1
class[1] : org.example.AcademicGroup
required-type[1] : org.example.AcademicGroup
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version : 1.4.20
Upvotes: 1
Views: 297
Reputation: 501
You try adding xStream.autodetectAnnotations(true). This should work.
public static void studentJsonDeserialization() {
XStream xStream = new XStream(new JettisonMappedXmlDriver());
xStream.autodetectAnnotations(true);
xStream.addPermission(AnyTypePermission.ANY);
xStream.alias("academicGroup", AcademicGroup.class);
AcademicGroup newAcademicGroup = (AcademicGroup) xStream.fromXML(new File("AcademicGroup.json"));
for (Student student : newAcademicGroup.getStudents()) {
System.out.println(student.toString());
}
}
Upvotes: 0