0

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
}
1

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thx for response, i've already worked with Gson, but don't remember GSON library can convert Map<String, Map<String, Object>> and Map<String, Map<String, Boolean>> to string and back?
0

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, ...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.