I'm using the flexjson library to serialize / deserialize JSON files with Netbeans 8.2. If I try to deserialize the previously created JSON from the instance of class Xxx, an error occurs, indicating that it can not deserialize class Xxx.
java.lang.ClassCastException: java.util.HashMap can not be cast flexjson_test.Main $ Xxx
Note: the JSONs are created correctly, I printed them on the screen.
I did several tests and it's always the same.
Test code:
package flexjson_test;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.util.ArrayList;
public class Main {
private JSONSerializer serializer;
private JSONDeserializer<Project> projectDeserializer;
private JSONDeserializer<Task> taskDeserializer;
private String jsonProject, jsonTask;
/**
* Main method
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main();
}
/**
* Constructor
*
*/
public Main() {
serializer = new JSONSerializer().exclude("*.class");
projectDeserializer = new JSONDeserializer<>();
taskDeserializer = new JSONDeserializer<>();
this.testSerialize();
this.testDeserialize();
}
/**
* Tests
*
*/
private void testDeserialize() {
System.out.println("Deserialization tests...");
Project project = this.deserialiceProject(this.jsonProject);
Task task = this.deserialiceTask(this.jsonTask);
System.out.println("-------------------------------------\n\n");
}
private void testSerialize() {
System.out.println("Serialization tests...");
Project project = getProject();
Task task = getTask();
this.jsonTask = serializer.serialize( task );
System.out.println("Task: \n" + jsonTask);
this.jsonProject = serializer.serialize( project );
System.out.println("Project: \n" + jsonProject);
project.addTask(task);
this.jsonProject = serializer.include( "tasks" ).serialize( project );
System.out.println("Project: \n" + jsonProject);
System.out.println("-------------------------------------\n\n");
}
/**
* Deserialices
*
*/
private Project deserialiceProject(String json) {
if ( json == null || json.equals("")) {
System.err.println("ERR args-> deserializing Project");
return null;
}
Project p = null;
try {
p = projectDeserializer.deserialize(json);
} catch(Exception e) {
System.err.println(
"ERR -> deserializing Project -> " + e.toString( ));
}
return p;
}
private Task deserialiceTask(String json) {
if ( json == null || json.equals("")) {
System.err.println("ERR args-> deserializing Task");
return null;
}
Task t = null;
try {
t = taskDeserializer.deserialize(json);
} catch(Exception e) {
System.err.println(
"ERR -> deseriazing Task -> " + e.toString( ));
}
return t;
}
/**
* Create entities
*
*/
private Task getTask() {
Task t = new Task( 1, "task one" );
System.out.println(t.toString( ));
return t;
}
private Project getProject() {
Project p = new Project( 1, "test" );
System.out.println(p.toString( ));
return p;
}
/**
* Internal classes
*
*/
private class Project {
private int id;
private String name;
private ArrayList<Task> tasks;
public Project(int id, String name) {
this.id = id;
this.name = name;
this.tasks = new ArrayList<>();
}
public Project(int id, String name, ArrayList<Task> tasks) {
this.id = id;
this.name = name;
this.tasks = tasks;
}
public void addTask(Task t) {
if (t == null) {
System.err.println("Error storing task: 'null' given");
return;
}
this.tasks.add( t );
}
public void clearTasks() {
this.tasks.clear();
}
// Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Task> getTasks() {
return tasks;
}
public void setTasks(ArrayList<Task> tasks) {
this.tasks = tasks;
}
} // class
private class Task {
private int id;
private String name;
public Task(int id, String name) {
this.id = id;
this.name = name;
}
// Getters and setters
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
} // class
} // class