Handling Structured Meta Data

In Android, the ORM we use (GreenDAO) can’t store arrays or maps. It can only store key, value pairs. The ORM we use for iOS (CoreData) on the other hand can store maps and dictionaries. That means that to store structured data for Android we need to take another approach.

Imagine the following data that we want to add to a message's meta payload:

{
    key1: {
        sub1: β€œa”,
        sub2: β€œb”
    },
    key2: β€œc"
}

To store this in Android we would need to do the following:

message.setValueForKey(β€œa", "key1/sub1”);
message.setValueForKey(β€œb", "key1/sub2");
message.setValueForKey(β€œc", "key2");

You can also use the HashMapHelper class to convert between these two representations:

Map<String, String> sub = new HashMap<String, String>() {{
            put("sub1", "a");
            put("sub2", "b");
        }};
Map<String, Object> map = new HashMap<String, Object>() {{
    put("key1", sub);
    put("key2", "c");
}};

You can then flatten the map:

Map<String, Object> flat = HashMapHelper.flatten(map);

This yields:

{
  key2=c, 
  key1/sub2=b, 
  key1/sub1=a
}

You can also expand the map:

Map<String, Object> expanded = HashMapHelper.expand(flat);

Which yields:

{
  key2=c, 
  key1={
    sub1=a, 
    sub2=b
  }
}

Last updated