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 com.google.gson.GsonBuilder;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import java.util.Map.Entry;
30 * This static final class contains utility methods for Avro schemas.
32 * @author Liam Fallon (liam.fallon@ericsson.com)
34 public final class AvroSchemaKeyTranslationUtilities {
35 // Constants for key replacements
36 private static final String DOT_STRING = "\\.";
37 private static final String DOT_STRING_REPLACEMENT = "_DoT_";
38 private static final String DASH_STRING = "-";
39 private static final String DASH_STRING_REPLACEMENT = "_DasH_";
42 * Default constructor to avoid subclassing.
44 private AvroSchemaKeyTranslationUtilities() {
45 // Private constructor to prevent subclassing
49 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
50 * subsequently contain only [A-Za-z0-9_]
52 * @param jsonString The JSON string to translate
53 * @param revert True if we want to revert the field names to their original values
54 * @return the translated JSON string
56 public static String translateIllegalKeys(final String jsonString, final boolean revert) {
57 if (jsonString == null) {
61 // Create a JSON element for the incoming JSON string
62 final JsonElement jsonElement = new GsonBuilder().serializeNulls().create().fromJson(jsonString,
65 final JsonElement translatedJsonElement = translateIllegalKeys(jsonElement, revert);
67 return new GsonBuilder().serializeNulls().create().toJson(translatedJsonElement);
71 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
72 * subsequently contain only [A-Za-z0-9_]
74 * @param jsonElement The JSON element to translate
75 * @param revert True if we want to revert the field names to their original values
76 * @return the translated JSON element
78 public static JsonElement translateIllegalKeys(final JsonElement jsonElement, final boolean revert) {
79 // We only act on JSON objects and arrays
80 if (jsonElement.isJsonObject()) {
81 return translateIllegalKeys(jsonElement.getAsJsonObject(), revert);
82 } else if (jsonElement.isJsonArray()) {
83 return translateIllegalKeys(jsonElement.getAsJsonArray(), revert);
90 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
91 * subsequently contain only [A-Za-z0-9_]
93 * @param jsonObject The JSON object to translate
94 * @param revert True if we want to revert the field names to their original values
95 * @return the translated JSON element
97 public static JsonElement translateIllegalKeys(final JsonObject jsonObject, final boolean revert) {
98 final JsonObject newJsonObject = new JsonObject();
100 for (final Entry<String, JsonElement> jsonObjectEntry : jsonObject.entrySet()) {
101 newJsonObject.add(translateIllegalKey(jsonObjectEntry.getKey(), revert),
102 translateIllegalKeys(jsonObjectEntry.getValue(), revert));
105 return newJsonObject;
109 * Translate characters in JSON keys to values that are legal in Avro. Avro names must start with [A-Za-z_] and
110 * subsequently contain only [A-Za-z0-9_]
112 * @param jsonArray The JSON array to translate
113 * @param revert True if we want to revert the field names to their original values
114 * @return the translated JSON element
116 public static JsonElement translateIllegalKeys(final JsonArray jsonArray, final boolean revert) {
117 final JsonArray newJsonArray = new JsonArray();
119 for (int i = 0; i < jsonArray.size(); i++) {
120 newJsonArray.add(translateIllegalKeys(jsonArray.get(i), revert));
127 * Translate characters in a single JSON key to values that are legal in Avro. Avro names must start with [A-Za-z_]
128 * and subsequently contain only [A-Za-z0-9_]
130 * @param key The key to translate
131 * @param revert True if we want to revert the field names to their original values
132 * @return the translated key
134 private static String translateIllegalKey(final String key, final boolean revert) {
136 return key.replaceAll(DOT_STRING_REPLACEMENT, DOT_STRING).replaceAll(DASH_STRING_REPLACEMENT, DASH_STRING);
138 return key.replaceAll(DOT_STRING, DOT_STRING_REPLACEMENT).replaceAll(DASH_STRING, DASH_STRING_REPLACEMENT);