简体   繁体   中英

Java Map<String, Map<String, Object>> convert to String and back

I have class field

Map<String, Map<String, Object>> myMap;

I need to implement it for ORMlite, i want create custom Persister, but don't know good way to convert it to string and back.

My persister class:

import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.field.SqlType;
import com.j256.ormlite.field.types.StringType;

import java.sql.SQLException;
import java.util.Map;

public class UserPersister extends StringType {

private static UserPersister INSTANCE;

private UserPersister() {
    super(SqlType.STRING, new Class<?>[] {Map.class});
}

public static UserPersister getInstance() {
    if (INSTANCE == null)
        INSTANCE = new UserPersister();
    return INSTANCE;
}

@Override
public Object javaToSqlArg(FieldType fieldType, Object javaObject) throws SQLException {
    Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) javaObject;
    return map != null ? getString(map) : null;
}

@Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) throws SQLException {
    return sqlArg != null ? getFromString((String) sqlArg) : null;
}

private String getString(Map<String, Map<String, Object>> map) {
    //implement
}

private Map<String, Map<String, Object>> getFromString(String json) {
    //implement
}

Use

new JSONObject(map);

Other functions you can get from its documentation http://www.json.org/javadoc/org/json/JSONObject.html . But, this only works for a String,String map and not a complex String,Object.

Gson can also be used to serialize arbitrarily complex objects.

Here is how you use it:

Gson gson = new Gson(); 
String json = gson.toJson(myObject); 

Gson will automatically convert collections to JSON arrays. Gson can serialize private fields and automatically ignores transient fields

or use serialization, and encode it to base64.

whatever solution you take, objects must be serializables (see below)

result is not readable, but it's safe and portable.

// encoding
Map<Integer, Map<String,String>> mimss =new HashMap<Integer, Map<String,String>>();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(mimss);
oos.flush();
oos.close();
bos.close();
byte[] byteData = bos.toByteArray();
String serial= DatatypeConverter.printBase64Binary(byteData);

// decoding
byte[] byteData_reverse=DatatypeConverter.parseBase64Binary(serial);
ByteArrayInputStream bais = new ByteArrayInputStream(byteData_reverse);
Map<Integer, Map<String,String>> mimss_copy=(Map<Integer,Map<String,String>>) new ObjectInputStream(bais).readObject();

to be serializable, your class must be like that

public class myclass implements Serializable

and you should (not mandatory) declare inside

private static final long serialVersionUID = 6569837232917408380L;

If anything inside in serializable too, it's OK (standard types are, collections, ...)

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