Merge "Reorder modifiers"
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / openecomp / mso / adapters / json / MapDeserializer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.mso.adapters.json;
21
22 import org.codehaus.jackson.JsonNode;
23 import org.codehaus.jackson.JsonParser;
24 import org.codehaus.jackson.map.DeserializationContext;
25 import org.codehaus.jackson.map.JsonDeserializer;
26 import org.codehaus.jackson.map.ObjectMapper;
27
28 import java.io.IOException;
29 import java.util.LinkedHashMap;
30 import java.util.Map;
31
32 /**
33  * Custom JSON Deserializer for Map<String, String>.
34  * In MSO with Jackson 1.9.12 and RestEasy 3.0.8, maps in JSON are serialized as
35  * follows:
36  * <pre>
37  * "params": {
38  *   "entry": [
39  *     {"key": "P1", "value": "V1"},
40  *     {"key": "P2", "value": "V2"},
41  *     ...
42  *     {"key": "PN", "value": "VN"}
43  *   ]
44  * }
45  * The implementation uses a LinkedHashMap to preserve the ordering of entries.
46  * </pre>
47  */
48 public class MapDeserializer extends JsonDeserializer<Map<String, String>> {
49
50         @Override
51         public Map<String, String> deserialize(JsonParser parser,
52                         DeserializationContext context) throws IOException {
53                 ObjectMapper mapper = new ObjectMapper();
54                 JsonNode tree = mapper.readTree(parser);
55                 Map<String, String> map = new LinkedHashMap<>();
56                 if (tree == null)
57                         return map;
58                 for (JsonNode element : tree) {
59                         for (JsonNode arrayElement : element) {
60                                 String key = arrayElement.get("key").getTextValue();
61                                 String value = arrayElement.get("value").getTextValue();
62                                 map.put(key, value);
63                         }
64                 }
65                 return map;
66         }
67 }