Works with complex and nested Map<Key, Value> easily.
At metrix.ir we are doing a lot of interseting thing. Our microservice architecture heavily relis on JSON:
- Collecting data from input sources that push JSON to us
- Inter communication between microservices that talk JSON over HTTP
- Using document-based databases which accept a document as a JSON
So working with JSON data effectively is our priority and one way of representing Json data is through Map data structure. Here are some use cases:
Accessing your document from elasticsearch is accomplished through these methods:
SearchHit.getSourceAsString()
SearchHit.getSourceAsMap
getSourceAsMap
returns a nested map. So if your document looks like this:
{
"a" : {
"b": {
"c": 10
}
}
}
To get the value of c
you should write code something like this:
Map<String, Object> a = (Map<String, Object>)doc.get("a");
if (a != null) {
Map<String, Object> b = (Map<String, Object>)doc.get("b");
if (b != null) {
int c = (Integer) doc.get("c");
}
}
But with DotDot it is as easy as:
import static com.github.DotDot.*
int c = getInt("a.b.c", doc)
Most of the time, you parse the JSON to your data model, and will use that model later. But sometimes you do not have a data model defined for specific response, or response is so big and you are interested to just few fields. In these cases, Map<K,V> and DotDot are perfect match. Here is the sample code:
Map<String, Object> response = new Gson().fromJson(jsonStr, Map.class);
int c = getInt("a.b.c", response)
Sometimes you only need to ensure that some fields are provided. In this case you can use ensureHaveValue method. It returns true or false:
boolean ok = ensureHaveValue("a.b.c", response)
If you want to check that some field has some value you can use mustEqual.
Instead of writng this code:
Map<String, Object> addressMap = new HashMap<String, Object>();
addressMap.put("street", "A");
addressMap.put("no", "7");
Map<String, Object> locationMap = new HashMap<String, Object>();
locationMap.put("country", "Germany");
locationMap.put("address", addressMap);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userId","A431");
map.put("location", locationMap);
easily write this:
Map<String, Object> map = new HashMap<String, Object>();
put("userId", "A431", map);
put("userId.location.country", "Germany", map);
put("userId.location.address.street", "A", map);
put("userId.location.address.no", "7", map);
You can reference to this library by either of java build systems
(Maven, Gradle, SBT or Leiningen) using snippets from this jitpack link: