简体   繁体   中英

Deserialize a serialized object to map in java

I want to deserialize a serialized object to map

class Person {
    private String name;
    private int age;

    // getters setters
}

I want to read this object in another application, but i don't have this class in classpath( and I can't control the serialization), so it will throws a ClassNotFoundException. But i want to know the fields information, like name=hendrix, age=27 . How can I make this, is there a library do this?

Java serialize a object, it have to write the fields and value to the byte array, there must be an approach to deserialize it to a map, I wonder is there anybody already do this work for us...

Before you write it to a file, you could covert it to a Map and then serialize it.

public static Map<String, String> convertPersonToMap(Person person){
    Map<String, String> returnedMap = new HashMap<>();
    returnedMap.put("name", person.getName());
    returnedMap.put("age", Integer.toString(person.getAge()));
    return returnedMap;
}

And then serialize the Map object. On the program reading the file in you can deserialize the file back into the Map Object. This is due to the fact that Map implements Serializable

You need to have the class file in your class path when deserializing the class.

If you want to transmit a class from one end of a stream to another, you could possibly push the .class file itself over the stream, save it to a file, and then use the ClassLoader :

Please See this:

http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#defineClass%28java.lang.String,%20byte%5B%5D,%20int,%20int%29

As already written in the comments, you can use JSON as programming language and system independent data format.

Writing JSON

If your code runs in a Java EE environment, you can use the built-in Java API for JSON-Processing (JSR-353). Unfortunately it does support object-mapping, so you will have to do it by yourself:

// get Person to serialize
Person p = ...
// create intermediate JsonObject
JsonObject obj = Json.createObjectBuilder()
        .add("name", p.getName())
        .add("age", p.getAge())
        .build();
// write Json Object to Writer or OutputStream
Json.createWriter(outputStream).write(obj);

On the Java SE side, you can either download a library that implements JSR-353, eg the reference implementation at https://jsonp.java.net , or use any other JSON library.

For example, using Jackson , the code above simplifies to:

// get Person to serialize
Person p = ...
// create ObjectMapper
ObjectMapper mapper = new ObjectMapper();
// serialize Person to File, Writer or OutputStream
mapper.writeValue(file, p);

Reading JSON

On the reading side, you have the same possibilities as mentioned above.

JSON-P:

// create JsonReader from file InputStream
JsonReader r = Json.createReader(inputStream);
// parse JSON into intermediate JsonObject
JsonObject obj = r.readObject();
// get fields directly from JsonObject:
String name = obj.getString("name");
String age  = obj.getInt("age");

Jackson :

// create ObjectMapper
ObjectMapper mapper = new ObjectMapper();
// read file and parse JSON into java.util.Map
Map<String, Object> map = mapper.readValue(file, Map.class);
// get fields from map
String name = (String)map.get("name");
int age  = (Integer)map.get("age");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM