2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.schema.avro;
23 import java.util.Map.Entry;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
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() {}
48 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start
49 * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]
51 * @param jsonString The JSON string to translate
52 * @param revert True if we want to revert the field names to their original values
53 * @return the translated JSON string
55 public static String translateIllegalKeys(final String jsonString, final boolean revert) {
56 if (jsonString == null) {
60 // Create a JSON element for the incoming JSON string
61 final JsonElement jsonElement =
62 new GsonBuilder().serializeNulls().create().fromJson(jsonString, JsonElement.class);
64 final JsonElement translatedJsonElement = translateIllegalKeys(jsonElement, revert);
66 return new GsonBuilder().serializeNulls().create().toJson(translatedJsonElement);
70 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start
71 * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]
73 * @param jsonElement The JSON element to translate
74 * @param revert True if we want to revert the field names to their original values
75 * @return the translated JSON element
77 public static JsonElement translateIllegalKeys(final JsonElement jsonElement, final boolean revert) {
78 // We only act on JSON objects and arrays
79 if (jsonElement.isJsonObject()) {
80 return translateIllegalKeys(jsonElement.getAsJsonObject(), revert);
81 } else if (jsonElement.isJsonArray()) {
82 return translateIllegalKeys(jsonElement.getAsJsonArray(), revert);
89 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start
90 * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]
92 * @param jsonObject The JSON object to translate
93 * @param revert True if we want to revert the field names to their original values
94 * @return the translated JSON element
96 public static JsonElement translateIllegalKeys(final JsonObject jsonObject, final boolean revert) {
97 final JsonObject newJsonObject = new JsonObject();
99 for (final Entry<String, JsonElement> jsonObjectEntry : jsonObject.entrySet()) {
100 newJsonObject.add(translateIllegalKey(jsonObjectEntry.getKey(), revert),
101 translateIllegalKeys(jsonObjectEntry.getValue(), revert));
104 return newJsonObject;
108 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start
109 * with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]
111 * @param jsonArray The JSON array to translate
112 * @param revert True if we want to revert the field names to their original values
113 * @return the translated JSON element
115 public static JsonElement translateIllegalKeys(final JsonArray jsonArray, final boolean revert) {
116 final JsonArray newJsonArray = new JsonArray();
118 for (int i = 0; i < jsonArray.size(); i++) {
119 newJsonArray.add(translateIllegalKeys(jsonArray.get(i), revert));
126 * Translate characters in a single JSON key to values that are legal in Avro. Avro names must
127 * start with [A-Za-z_] and subsequently contain only [A-Za-z0-9_]
129 * @param key The key to translate
130 * @param revert True if we want to revert the field names to their original values
131 * @return the translated key
133 private static String translateIllegalKey(final String key, final boolean revert) {
135 return key.replaceAll(DOT_STRING_REPLACEMENT, DOT_STRING).replaceAll(DASH_STRING_REPLACEMENT, DASH_STRING);
137 return key.replaceAll(DOT_STRING, DOT_STRING_REPLACEMENT).replaceAll(DASH_STRING, DASH_STRING_REPLACEMENT);