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:
1
{
2
key1: {
3
sub1: β€œa”,
4
sub2: β€œb”
5
},
6
key2: β€œc"
7
}
Copied!
To store this in Android we would need to do the following:
1
message.setValueForKey(β€œa", "key1/sub1”);
2
message.setValueForKey(β€œb", "key1/sub2");
3
message.setValueForKey(β€œc", "key2");
Copied!
You can also use the HashMapHelper class to convert between these two representations:
1
Map<String, String> sub = new HashMap<String, String>() {{
2
put("sub1", "a");
3
put("sub2", "b");
4
}};
5
Map<String, Object> map = new HashMap<String, Object>() {{
6
put("key1", sub);
7
put("key2", "c");
8
}};
Copied!
You can then flatten the map:
1
Map<String, Object> flat = HashMapHelper.flatten(map);
Copied!
This yields:
1
{
2
key2=c,
3
key1/sub2=b,
4
key1/sub1=a
5
}
Copied!
You can also expand the map:
1
Map<String, Object> expanded = HashMapHelper.expand(flat);
Copied!
Which yields:
1
{
2
key2=c,
3
key1={
4
sub1=a,
5
sub2=b
6
}
7
}
Copied!
Copy link