2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.context.schema.avro;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import java.util.Map.Entry;
31 * This static final class contains utility methods for Avro schemas.
33 * @author Liam Fallon (liam.fallon@ericsson.com)
35 public final class AvroSchemaKeyTranslationUtilities {
36 // Constants for key replacements
37 private static final String DOT_STRING = ".";
38 private static final String DOT_STRING_REPLACEMENT = "_DoT_";
39 private static final String DASH_STRING = "-";
40 private static final String DASH_STRING_REPLACEMENT = "_DasH_";
43 * Default constructor to avoid subclassing.
45 private AvroSchemaKeyTranslationUtilities() {
46 // Private constructor to prevent subclassing
50 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
51 * subsequently contain only [A-Za-z0-9_]
53 * @param jsonString The JSON string to translate
54 * @param revert True if we want to revert the field names to their original values
55 * @return the translated JSON string
57 public static String translateIllegalKeys(final String jsonString, final boolean revert) {
58 if (jsonString == null) {
62 // Create a JSON element for the incoming JSON string
63 final JsonElement jsonElement = new GsonBuilder().serializeNulls().create().fromJson(jsonString,
66 final JsonElement translatedJsonElement = translateIllegalKeys(jsonElement, revert);
68 return new GsonBuilder().serializeNulls().create().toJson(translatedJsonElement);
72 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
73 * subsequently contain only [A-Za-z0-9_]
75 * @param jsonElement The JSON element to translate
76 * @param revert True if we want to revert the field names to their original values
77 * @return the translated JSON element
79 public static JsonElement translateIllegalKeys(final JsonElement jsonElement, final boolean revert) {
80 // We only act on JSON objects and arrays
81 if (jsonElement.isJsonObject()) {
82 return translateIllegalKeys(jsonElement.getAsJsonObject(), revert);
83 } else if (jsonElement.isJsonArray()) {
84 return translateIllegalKeys(jsonElement.getAsJsonArray(), revert);
91 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
92 * subsequently contain only [A-Za-z0-9_]
94 * @param jsonObject The JSON object to translate
95 * @param revert True if we want to revert the field names to their original values
96 * @return the translated JSON element
98 public static JsonElement translateIllegalKeys(final JsonObject jsonObject, final boolean revert) {
99 final JsonObject newJsonObject = new JsonObject();
101 for (final Entry<String, JsonElement> jsonObjectEntry : jsonObject.entrySet()) {
102 newJsonObject.add(translateIllegalKey(jsonObjectEntry.getKey(), revert),
103 translateIllegalKeys(jsonObjectEntry.getValue(), revert));
106 return newJsonObject;
110 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
111 * subsequently contain only [A-Za-z0-9_]
113 * @param jsonArray The JSON array to translate
114 * @param revert True if we want to revert the field names to their original values
115 * @return the translated JSON element
117 public static JsonElement translateIllegalKeys(final JsonArray jsonArray, final boolean revert) {
118 final JsonArray newJsonArray = new JsonArray();
120 for (int i = 0; i < jsonArray.size(); i++) {
121 newJsonArray.add(translateIllegalKeys(jsonArray.get(i), revert));
128 * Translate characters in a single JSON key to values that are legal in Avro. Avro names must start with [A-Za-z_]
129 * and subsequently contain only [A-Za-z0-9_]
131 * @param key The key to translate
132 * @param revert True if we want to revert the field names to their original values
133 * @return the translated key
135 private static String translateIllegalKey(final String key, final boolean revert) {
137 return key.replace(DOT_STRING_REPLACEMENT, DOT_STRING).replace(DASH_STRING_REPLACEMENT, DASH_STRING);
139 return key.replace(DOT_STRING, DOT_STRING_REPLACEMENT).replace(DASH_STRING, DASH_STRING_REPLACEMENT);