b595b4f3630fa2a3e4a059ee6602f6f36fc9397a
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. 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
21 package org.onap.pnfsimulator.template.search;
22
23 import com.google.common.base.Strings;
24 import com.google.gson.Gson;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonPrimitive;
29 import org.bson.Document;
30
31 /**
32  * This util flattens nested json and produces json with keys transformed to form of json path
33  * where default separator between parent object key and object key is ':'
34  * For easing searching of boolean values, they are converted to its string representation
35  */
36 public class JsonUtils {
37
38     private static final String DEFAULT_PARENT_KEY_TO_OBJECT_KEY_SEPARATOR = ":";
39     private static final String SEED_PREFIX = "";
40     private static final Gson GSON = new Gson();
41
42     public JsonObject flatten(JsonObject original) {
43         return flattenWithPrefixedKeys(DEFAULT_PARENT_KEY_TO_OBJECT_KEY_SEPARATOR, original.deepCopy(), SEED_PREFIX, new JsonObject());
44     }
45
46     public JsonObject flatten(String parentKeyToKeySeparator, JsonObject original) {
47         return flattenWithPrefixedKeys(parentKeyToKeySeparator, original.deepCopy(), SEED_PREFIX, new JsonObject());
48     }
49
50     public Document flatten(Document original) {
51         return flatten(DEFAULT_PARENT_KEY_TO_OBJECT_KEY_SEPARATOR, original);
52     }
53
54     public Document flatten(String parentKeyToKeySeparator, Document original) {
55         JsonObject originalJsonObject = GSON.fromJson(original.toJson(), JsonObject.class);
56         JsonObject flattenedJson = flatten(parentKeyToKeySeparator, originalJsonObject);
57         return Document.parse(flattenedJson.toString());
58     }
59
60     private JsonObject flattenWithPrefixedKeys(String parentKeyToKeySeparator, JsonElement topLevelElem, String prefix, JsonObject acc) {
61         if (topLevelElem.isJsonPrimitive()) {
62             handleJsonPrimitive(topLevelElem, prefix, acc);
63         } else if (topLevelElem.isJsonArray()) {
64             handleJsonArray(parentKeyToKeySeparator, topLevelElem, prefix, acc);
65         } else if (topLevelElem.isJsonObject()) {
66             handleJsonObject(parentKeyToKeySeparator, topLevelElem, prefix, acc);
67         } else {
68             acc.add(prefix, topLevelElem.getAsJsonNull());
69         }
70         return acc.deepCopy();
71     }
72
73     private void handleJsonObject(String parentKeyToKeySeparator, JsonElement topLevelElem, String prefix, JsonObject acc) {
74         boolean isEmpty = true;
75         JsonObject thisToplevelObj = topLevelElem.getAsJsonObject();
76         for (String key : thisToplevelObj.keySet()) {
77             isEmpty = false;
78             String keyPrefix = String.format("%s%s%s", prefix, parentKeyToKeySeparator, key);
79             flattenWithPrefixedKeys(parentKeyToKeySeparator, thisToplevelObj.get(key), keyPrefix, acc);
80         }
81         if (isEmpty && !Strings.isNullOrEmpty(prefix)) {
82             acc.add(prefix, new JsonObject());
83         }
84     }
85
86     private void handleJsonArray(String parentKeyToKeySeparator, JsonElement topLevelElem, String prefix, JsonObject acc) {
87         JsonArray asJsonArray = topLevelElem.getAsJsonArray();
88         if (asJsonArray.size() == 0) {
89             acc.add(prefix, new JsonArray());
90         }
91         for (int i = 0; i < asJsonArray.size(); i++) {
92             flattenWithPrefixedKeys(parentKeyToKeySeparator, asJsonArray.get(i), String.format("%s[%s]", prefix, i), acc);
93         }
94     }
95
96     private void handleJsonPrimitive(JsonElement topLevelElem, String prefix, JsonObject acc) {
97         JsonPrimitive jsonPrimitive = topLevelElem.getAsJsonPrimitive();
98         if (jsonPrimitive.isBoolean()) {
99             acc.add(prefix, new JsonPrimitive(jsonPrimitive.getAsString()));
100         } else {
101             acc.add(prefix, topLevelElem.getAsJsonPrimitive());
102         }
103     }
104 }