2

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.

1 Answer 1

7

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

3
  • Thank you for your answer, I'll check this as soon as i have some time. Can you tell me whether this is automatic or not, in my case, if I call save on the hashmap's holder class - Control, than it is automatically handled, and saved into db? Sep 16, 2014 at 6:15
  • Yes, its automatic. When you save ActiveAndroid iterate over all fields and serialize then. In special with the map he will use this TypeSerializer.
    – Wakim
    Sep 16, 2014 at 11:23
  • How will activeandriod know which TypeSerializer to use for which object?
    – Codevalley
    May 21, 2016 at 9:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.