Search code examples
androidjsondatabaseactiveandroid

Storing HashMap<String, Object> with using ActiveAndroid library


I have a class:

@Table(name = "Control")
public class Control extends Model{

   @Column
   private String name;

   [...]

   @Column
   private Map<String, Object> properties;

}

I have other fields in this class, but i wrote here only those which is enough to show what is my problem.

I got an exception about insertion:

09-14 22:11:34.542: E/SQLiteLog(11482): (1) table Control has no column named properties

Object can be either String[] or String.

If I dont place @Column on HashMap data, than everything works "fine" only the problem they are not stored.

So my question is how should I solve storing this hashmap.

My data comes from API, and it has not the same properties always:

controls: [
    {
        bundle: "CloseButton",    
        frame: "0,0,25,25"
    },
    {
        referenceKey: "AccelerationGyro",
        name: "Acceleration Sensor",
        bundle: "Gyro",
        properties: {
            axis: "Z"
            observedKey: "brakeState",
            requiedValue: "1"
        },
        channels: {
            CH: "Acceleration"
        }
    },
    {
        referenceKey: "SteeringGyro",
        name: "Steering Sensor",
        bundle: "Gyro",
        properties: {
            axis: "Y"
            allowedOnI: [
                "@Lights",
                "@Rack",
                "@ReversingLamp"
            ],
    },
        channels: {
            CH: "Steering"
    }
},

I want to store this class with using ActiveAndroid lib. But unfortunatelly I could not find any solution to this on the web.

I'm not sure if my question is clear, I can provide additional stuff if needed. I just want to store the HashMap too. :P Is it maybe connected somehow to TypeSerializers?

Thanks your answer in advance.


Solution

  • ActiveAndroid don't persist Map type by default.

    Yes, you are right, you must write your own TypeSerializer, to serialize this Map as an String in some format (e.g: JSON) and deserialize the String to your Map.

    Maybe this code can help you to start this:

    final public class UtilMapSerializer extends TypeSerializer {
        @Override
        public Class<?> getDeserializedType() {
            return Map.class;
        }
    
        @Override
        public Class<?> getSerializedType() {
            return String.class;
        }
    
        @Override
        public String serialize(Object data) {
            if (data == null) {
                return null;
            }
    
            // Transform a Map<String, Object> to JSON and then to String
            return new JSONObject((Map<String, Object>) data).toString();
        }
    
        @Override
        public Map<String, Object> deserialize(Object data) {
            if (data == null) {
                return null;
            }
    
            // Properties of Model
            Map<String, Object> map = new HashMap<String, Object>();
    
            try {
                JSONObject json = new JSONObject((String) data);
    
                for(Iterator it = json.keys(); it.hasNext();) {
                    String key = (String) it.next();
    
                    map.put(key, json.get(key));
                }
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
            return map;
        }
    }
    

    And register then as <meta-data> in your <Application>.

    <meta-data android:name="AA_SERIALIZERS" android:value="my.package.UtilMapSerializer" />
    

    More information: https://github.com/pardom/ActiveAndroid/wiki/Type-serializers