2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.mso.adapters.json;
22 import org.codehaus.jackson.JsonNode;
23 import org.codehaus.jackson.JsonParser;
24 import org.codehaus.jackson.JsonProcessingException;
25 import org.codehaus.jackson.map.DeserializationContext;
26 import org.codehaus.jackson.map.JsonDeserializer;
27 import org.codehaus.jackson.map.ObjectMapper;
29 import java.io.IOException;
30 import java.util.Iterator;
31 import java.util.LinkedHashMap;
35 * Custom JSON Deserializer for Map<String, String>.
36 * In MSO with Jackson 1.9.12 and RestEasy 3.0.8, maps in JSON are serialized as
41 * {"key": "P1", "value": "V1"},
42 * {"key": "P2", "value": "V2"},
44 * {"key": "PN", "value": "VN"}
47 * The implementation uses a LinkedHashMap to preserve the ordering of entries.
50 public class MapDeserializer extends JsonDeserializer<Map<String, String>> {
52 public Map<String, String> deserialize(JsonParser parser,
53 DeserializationContext context) throws IOException,
54 JsonProcessingException {
55 ObjectMapper mapper = new ObjectMapper();
56 JsonNode tree = mapper.readTree(parser);
58 Map<String, String> map = new LinkedHashMap<String, String>();
59 Iterator<JsonNode> iterator = tree.iterator();
61 while (iterator.hasNext()) {
62 JsonNode element = iterator.next();
63 Iterator<JsonNode> arrayIterator = element.iterator();
64 while (arrayIterator.hasNext()) {
65 JsonNode arrayElement = arrayIterator.next();
66 String key = arrayElement.get("key").getTextValue();
67 String value = arrayElement.get("value").getTextValue();