2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.pnfsimulator.template.search;
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;
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
36 public class JsonUtils {
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();
42 public JsonObject flatten(JsonObject original) {
43 return flattenWithPrefixedKeys(DEFAULT_PARENT_KEY_TO_OBJECT_KEY_SEPARATOR, original.deepCopy(), SEED_PREFIX, new JsonObject());
46 public JsonObject flatten(String parentKeyToKeySeparator, JsonObject original) {
47 return flattenWithPrefixedKeys(parentKeyToKeySeparator, original.deepCopy(), SEED_PREFIX, new JsonObject());
50 public Document flatten(Document original) {
51 return flatten(DEFAULT_PARENT_KEY_TO_OBJECT_KEY_SEPARATOR, original);
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());
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);
68 acc.add(prefix, topLevelElem.getAsJsonNull());
70 return acc.deepCopy();
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()) {
78 String keyPrefix = String.format("%s%s%s", prefix, parentKeyToKeySeparator, key);
79 flattenWithPrefixedKeys(parentKeyToKeySeparator, thisToplevelObj.get(key), keyPrefix, acc);
81 if (isEmpty && !Strings.isNullOrEmpty(prefix)) {
82 acc.add(prefix, new JsonObject());
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());
91 for (int i = 0; i < asJsonArray.size(); i++) {
92 flattenWithPrefixedKeys(parentKeyToKeySeparator, asJsonArray.get(i), String.format("%s[%s]", prefix, i), acc);
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()));
101 acc.add(prefix, topLevelElem.getAsJsonPrimitive());