I've recently been faced with a tough json (that I don't control, so I have to deal with it):
{
"someOtherParam":"someValue",
"attributes":
{
"language":["fr", "en"],
"otherParam":["value1", "value2"]
}
}
attributes is a map - I don't know what attributes it may contain, so I can't just map it to an object. In Java I believe I need to map is as a Map<String,List<String>> somehow.
I've found a very helpful post that allowed me to write an adapter like this:
public class MapAdapter extends XmlAdapter<MapAdapter.AdaptedMap, Map<String, List<String>>> {
public static class AdaptedMap {
#XmlVariableNode("key")
List<AdaptedEntry> entries = new ArrayList<>();
}
public static class AdaptedEntry {
#XmlTransient
public String key;
#XmlValue
public List<String> value;
}
#Override
public AdaptedMap marshal(Map<String, List<String>> map) throws Exception {
AdaptedMap adaptedMap = new AdaptedMap();
for(Map.Entry<String, List<String>> entry : map.entrySet()) {
AdaptedEntry adaptedEntry = new AdaptedEntry();
adaptedEntry.key = entry.getKey();
adaptedEntry.value = entry.getValue();
adaptedMap.entries.add(adaptedEntry);
}
return adaptedMap;
}
#Override
public Map<String, List<String>> unmarshal(AdaptedMap adaptedMap) throws Exception {
List<AdaptedEntry> adaptedEntries = adaptedMap.entries;
Map<String, List<String>> map = new HashMap<>(adaptedEntries.size());
for(AdaptedEntry adaptedEntry : adaptedEntries) {
map.put(adaptedEntry.key, adaptedEntry.value);
}
return map;
}
}
And while this general approach would work for simple values (so a Map<String,String> for instance), here on marshalling it insists on mapping the list as a simple element
{
"someOtherParam":"someValue",
"attributes":
{
"language":"fr en",
"otherParam":"value1 value2"
}
}
So how do I do this correctly?
Related
I am not able get the code to go to else block. The code always goes inside if block. Is there a way to validate the value of a Map<String, Map<String,String>>.
I get compile warning Map<String, Map<String, String>> may not contain values of type String.
Could anyone please tell me how to validate this?
public class TestClass {
Map<String, String> map = new HashMap<String, String>();
Map<String, Map<String, String>> val= new HashMap<String, Map<String, String>>();
public void setData() {
map = new HashMap<String, String>();
map.put("10", "1000");
map.put("11", "alphabet");
map.put("12", "1002");
map.put("13", "1003");
val.put("1", map);
val.put("2", map);
val.put("3", map);
val.put("4", map);
}
public void showData() {
if (!this.val.containsValue("alphabet")) {
System.out.println("inside if ");
} else {
System.out.println("else");
}
}
public static void main(String args[]) {
TestClass obj = new TestClass();
obj.setData();
obj.showData();
}
}
What is the best way to avoid multiple parallel if-else loop. I tried with switch statement as well, but again that doesn't look readable. I have hundreds of such statements:
public static Map getKqvSecureNodeResponse(Sample secureNodeData, Map<String, Object> map) {
if(map.containsKey(Constants.NAME_KQV)) {
map.put(Constants.NAME_KQV, secureNodeData.getNodename());
}
if(map.containsKey(Constants.SPOV)) {
map.put(Constants.SPOV, secureNodeData.getOverride());
}
if(map.containsKey(Constants.SPEP)) {
map.put(Constants.SPEP, secureNodeData.getEnabledProtocol());
}
if(map.containsKey(Constants.SPTO)) {
map.put(Constants.SPTO, secureNodeData.getAuthTimeout());
}
if(map.containsKey(Constants.TLCN)) {
map.put(Constants.TLCN, secureNodeData.getCommonName());
}
if(map.containsKey(Constants.SEDT)) {
map.put(Constants.SEDT, secureNodeData.getEncryptData());
}
if(map.containsKey(Constants.TLCF)) {
map.put(Constants.TLCF, secureNodeData.getKeyCertLabel());
}
if(map.containsKey(Constants.TLCL)) {
map.put(Constants.TLCL, secureNodeData.getCipherSuites());
}
return map;
}
Please note that I have to invoke different getter of secureNodeData for every check.
For each Constants value (e.g. Constants.NAME_KQV), you can provide a Function<Sample, Object> (e.g. sample -> sample.getNodename()).
If you organised it in a structure like Map or enum (here, I used a enum), you could end up with a simple loop:
public static Map<String, Object> getKqvSecureNodeResponse(Sample secureNodeData, Map<String, Object> map) {
for (Constant constant : Constant.values()) {
final String name = constant.getName();
if (map.containsKey(name)) {
map.put(name, constant.getFunction().apply(secureNodeData));
}
}
return map;
}
The enum was defined as:
enum Constant {
NAME_KQV(Constants.NAME_KQV, Sample::getNodename);
// other definitions
final String name;
final Function<Sample, Object> function;
Constant(String name, Function<Sample, Object> function) {
this.name = name;
this.function = function;
}
public String getName() {
return name;
}
public Function<Sample, Object> getFunction() {
return function;
}
}
It seems like this method does a lot. (1) It's unclear why it overrides existing values. (2) The method name is obscure. (3) You are using a raw Map, replace it with Map<String, Object> at least, and figure out how to substitute the Object part. (4)
I feel rethinking the design would help much more than the above approach and these small corrections.
You can try to take advantage of method references:
public static Map getKqvSecureNodeResponse(Sample node, Map<String, Object> map) {
applyParam(Constants.NAME_KQV, map, node::getNodename);
applyParam(Constants.SPOV, map, node::getOverride);
// ...
}
public static void applyParam(String key, Map<String, Object> data, Supplier<Object> getter) {
if (data.containsKey(key)) {
data.put(key, getter.get());
}
}
Alternatively you can use Function references that are instance independent:
private static final Map<String, Function<Sample, Object>> MAPPING;
static {
MAPPING = new LinkedHashMap<>();
MAPPING.put(Constants.NAME_KQV, Sample::getNodename);
MAPPING.put(Constants.SPOV, Sample::getOverride);
}
public static Map getKqvSecureNodeResponse(Sample node, Map<String, Object> map) {
for (String key : MAPPING.keySet()) {
if (map.containsKey(key)) {
map.put(key, MAPPING.get(key).apply(node));
}
}
}
There are many ways how you can approach your specific use case, but method references in general makes developer's life much much easier.
I have this class on which I have a method and will return a map, using dozens of methods inside it using the same arguments.
My class has thousand of lines and it will increase even more.
I can create several classes and inside create methods, but I´m asking if there is a special design for this scenario
Actual Scenario:
public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {
Map<String, Object> lastMap = new HashMap<String, Object>(firstMap);
lastMap = do1(firstMap, lastMap);
lastMap = do2(firstMap, lastMap);
lastMap = do3(firstMap, lastMap);
lastMap = do4(firstMap, lastMap);
//more 20 methods
return lastMap;
}
Try without design:
public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {
Map<String, Object> lastMap = new HashMap<String, Object>(firstMap);
Do1 do1 = new Do1();
lastMap = do1.do1(firstMap, lastMap);
Do2 do2 = new Do2();
lastMap = do2.do2(firstMap, lastMap);
// more 20 methods
return lastMap;
}
If do1, etc., are just implementation, not part of the public interface of your class, it would probably make sense to create a private class (perhaps even a nested one) with a constructor accepting the two maps which it retains as instance variables:
private static class Worker {
Worker(Map firstMap, Map secondMap) {
this.firstMap = firstMap;
this.secondMap = secondMap;
}
void do1() {
// ...update `this.lastMap` if appropriate...
}
void do2() {
// ...update `this.lastMap` if appropriate...
}
}
There I've made Worker static so it's a static nested class, not an inner class, but it could be an inner class instead (no static) if you need access to the internals of the surrounding class.
Then
public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {
Worker worker = new Worker(firstMap, new HashMap<String, Object>(firstMap));
worker.do1();
worker.do2();
worker.do3();
worker.do4();
//more 20 methods
return worker.lastMap;
}
You can use interface and make every "do" an implementation of functional interface.
interface Do {
Map<String, Object> apply(Map<String, Object> firstMap, Map<String, Object> lastMap);
}
Then you can initialize static dos:
static Do[] allDos = {
(firstMap, lastMap) -> {
// do0
},
(firstMap, lastMap) -> {
// do1
},
// ...do2, do3, ...
};
If you need to invoke do0 -> do2 -> do4 for example:
public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {
Map<String, Object> lastMap = new HashMap<String, Object>(firstMap);
int[] ids = { 0, 2, 4 };
for (int id : ids)
lastMap = allDos[id].apply(firstMap, lastMap);
return lastMap;
}
Why don't take more functional approach?
define interface
interface MapTransformation{
void transformation(Map<String, Object> first, Map<String, Object> last);
}
then during class creation, class which has definition of transformMapOnAnotherMap takes list of transformations as parameter then you could do
class SomeClass {
final private List<MapTransformation> transformations;
//constructor omitted
public Map<String, Object> transformMapOnAnotherMap(Map<String, Object> firstMap) {
Map<String, Object> lastMap = new HashMap<String, Object>(firstMap);
transformations.forEach(t->t.transformation(firstMap,lastMap);
return lastMap;
}
}
I've some server response (a long one) which I've converted to POJO (by using moshi library).
Eventually I have list of "Items" , each "Item" looks like follow :
public class Item
{
private String aa;
private String b;
private String abc;
private String ad;
private String dd;
private String qw;
private String arew;
private String tt;
private String asd;
private String aut;
private String id;
...
}
What I actually need, is to pull all properties which start with "a" , and then I need to use their values for further req ...
Any way to achieve it without Reflection ? (usage of streams maybe ?)
Thanks
With guava-functions tranformation you might transform your items with somethng following:
public static void main(String[] args) {
List<Item> items //
Function<Item, Map<String, Object>> transformer = new Function<Item, Map<String, Object>>() {
#Override
public Map<String, Object> apply(Item input) {
Map<String, Object> result = new HashMap<String, Object>();
for (Field f : input.getClass().getDeclaredFields()) {
if(! f.getName().startsWith("a")) {
continue;
}
Object value = null;
try {
value = f.get(input);
} catch (IllegalAccessException e) {
throw new RuntimeException("failed to cast" + e)
}
result.put(f.getName(), value);
}
return result
};
Collection<Map<String, Object> result
= Collections2.transform(items, transformer);
}
Sounds like you may want to perform your filtering on a regular Java map structure.
// Dependencies.
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Map<String, String>> itemAdapter =
moshi.adapter(Types.newParameterizedType(Map.class, String.class, String.class));
String json = "{\"aa\":\"value1\",\"b\":\"value2\",\"abc\":\"value3\"}";
// Usage.
Map<String, String> value = itemAdapter.fromJson(json);
Map<String, String> filtered = value.entrySet().stream().filter(
stringStringEntry -> stringStringEntry.getKey().charAt(0) == 'a')
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
You could wrap up the filtering logic in a custom JsonAdapter, but validation and business logic tends to be nice to leave to the application usage layer.
I am getting json from dynamoDb that looks like this -
{
"id": "1234",
"payment": {
"payment_id": "2345",
"user_defined": {
"some_id": "3456"
}
}
}
My aim is to get the user_defined field in a Java HashMap<String, Object> as user_defined field can contain any user defined fields, which would be unknown until the data arrives. Everything works fine except my DynamoDBMapper cannot convert the user_defined field to a Java HashMap. It is throwing this error -
Exception occured Response[payment]; could not unconvert attribute
This is how the classes looks like -
#DynamoDBTable(tableName = "PaymentDetails")
public class Response {
private String id;
public Response() {
}
private Payment payment = new Payment();
#DynamoDBHashKey(attributeName="id")
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public Payment getPayment() {
return payment;
}
public void setPayment(Payment payment) {
this.payment = payment;
}
}
The payment field mapper -
#DynamoDBDocument
public class Payment {
private String payment_id:
private HashMap<String, Object> user_defined;
public Payment() {}
public getPayment_id() {
return payment_id;
}
public setPayment_id(String payment_id) {
this.payment_id = payment_id;
}
#DynamoDBTypeConverted(converter = HashMapMarshaller.class)
public HashMap<String, Object> getUser_defined() {
return user_defined;
}
public void setUser_defined(HashMap<String, Object> user_defined) {
this.user_defined = user_defined;
}
}
The HashMapMarshaller(Just to check if Hashmap marshaller wasn't working with gson, I just defined a Hashmap, put in a value and return it, but seems to still not working) -
public class HashMapMarshaller implements DynamoDBTypeConverter<String, HashMap<String, Object>> {
#Override
public String convert(HashMap<String, Object> hashMap) {
return new Gson().toJson(hashMap);
}
#Override
public HashMap<String, Object> unconvert(String jsonString) {
System.out.println("jsonString received for unconverting is " + jsonString);
System.out.println("Unconverting attribute");
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("key", "value");
return hashMap;
//return new Gson().fromJson(jsonString, new TypeToken<HashMap<String, Object>>(){}.getType());
}
}
Marshaller approach is till now not working for me. It is also not printing any of the printlns I've put in there. I've also tried using #DynamoDBTyped(DynamoDBMapperFieldModel.DynamoDBAttributeType.M) and using Map instead of HashMap above my user_defined getter to no avail.
I want to find out how to convert the user_defined field to Java HashMap or Map. Any help is appreciated. Thank you!
Make Map<String, Object> to Map<String, String>. It should work without any custom converters. Otherwise be specific about Map's value type. For example, Map<String, SimplePojo> should work. Don't forget to annotate SimplePojo class with #DynamoDBDocument.
With Object as a type of Map's value, DynamoDB will not able to decide which object it has to create while reading entry from DynamoDB. It should know about specific type like String, Integer, SimplePojo etc.