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
}
llaerto
  • 251
  • 1
  • 2
  • 11
  • Possible duplicate of [How to convert hashmap to json object in java](http://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java) – Rohit Batta Dec 02 '15 at 07:14

2 Answers2

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

Rohit Batta
  • 482
  • 4
  • 16
  • Thx for response, i've already worked with Gson, but don't remember GSON library can convert Map> and Map> to string and back? – llaerto Dec 02 '15 at 07:19
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, ...)