diff options
Diffstat (limited to 'catalog-model/src/main/java')
2 files changed, 45 insertions, 68 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java index 1d0354f749..9ee287b333 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java @@ -43,8 +43,7 @@ import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; public class ToscaMapValueConverter extends ToscaValueBaseConverter implements ToscaValueConverter { private static final Logger log = Logger.getLogger(ToscaMapValueConverter.class.getName()); - private static ToscaMapValueConverter mapConverter = new ToscaMapValueConverter(); - private JsonParser jsonParser = new JsonParser(); + private static final ToscaMapValueConverter mapConverter = new ToscaMapValueConverter(); private ToscaMapValueConverter() { } @@ -56,7 +55,7 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T @Override public Object convertToToscaValue(String value, String innerType, Map<String, DataTypeDefinition> dataTypes) { if (value == null) { - return value; + return null; } try { ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType); @@ -85,12 +84,12 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T return value; } } - JsonElement jsonElement = null; + JsonElement jsonElement; try { StringReader reader = new StringReader(value); JsonReader jsonReader = new JsonReader(reader); jsonReader.setLenient(true); - jsonElement = jsonParser.parse(jsonReader); + jsonElement = JsonParser.parseReader(jsonReader); } catch (JsonSyntaxException e) { log.debug("convertToToscaValue failed to parse json value :", e); return null; diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java index b0598971df..ee254811b1 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaValueBaseConverter.java @@ -26,11 +26,9 @@ import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; @@ -82,74 +80,54 @@ public class ToscaValueBaseConverter { return result; } - public Object handleComplexJsonValue(JsonElement elementValue) { - Object jsonValue = null; - Map<String, Object> value = new HashMap<>(); - if (elementValue.isJsonObject()) { - JsonObject jsonOb = elementValue.getAsJsonObject(); - Set<Entry<String, JsonElement>> entrySet = jsonOb.entrySet(); - Iterator<Entry<String, JsonElement>> iteratorEntry = entrySet.iterator(); - while (iteratorEntry.hasNext()) { - Entry<String, JsonElement> entry = iteratorEntry.next(); - if (entry.getValue().isJsonArray()) { - List<Object> array = handleJsonArray(entry.getValue()); - value.put(entry.getKey(), array); - } else { - Object object; - if (entry.getValue().isJsonPrimitive()) { - object = json2JavaPrimitive(entry.getValue().getAsJsonPrimitive()); - } else { - object = handleComplexJsonValue(entry.getValue()); - } - value.put(entry.getKey(), object); - } - } - jsonValue = value; - } else { - if (elementValue.isJsonArray()) { - jsonValue = handleJsonArray(elementValue); - } else { - if (elementValue.isJsonPrimitive()) { - jsonValue = json2JavaPrimitive(elementValue.getAsJsonPrimitive()); - } else { - log.debug("not supported json type "); - } - } + public Object handleComplexJsonValue(final JsonElement jsonElement) { + if (jsonElement.isJsonNull()) { + return null; + } + if (jsonElement.isJsonObject()) { + return handleJsonObject(jsonElement); } - return jsonValue; + if (jsonElement.isJsonArray()) { + return handleJsonArray(jsonElement); + } + if (jsonElement.isJsonPrimitive()) { + return json2JavaPrimitive(jsonElement.getAsJsonPrimitive()); + } + log.debug("JSON type '{}' not supported", jsonElement); + return null; } - private List<Object> handleJsonArray(JsonElement entry) { - List<Object> array = new ArrayList<>(); - JsonArray jsonArray = entry.getAsJsonArray(); - Iterator<JsonElement> iterator = jsonArray.iterator(); - while (iterator.hasNext()) { - Object object; - JsonElement element = iterator.next(); - if (element.isJsonPrimitive()) { - object = json2JavaPrimitive(element.getAsJsonPrimitive()); - } else { - object = handleComplexJsonValue(element); - } - array.add(object); + private Map<String, Object> handleJsonObject(final JsonElement jsonElement) { + final Map<String, Object> jsonObjectAsMap = new HashMap<>(); + final JsonObject jsonObject = jsonElement.getAsJsonObject(); + for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) { + jsonObjectAsMap.put(entry.getKey(), handleComplexJsonValue(entry.getValue())); } - return array; + return jsonObjectAsMap; } - public Object json2JavaPrimitive(JsonPrimitive prim) { - if (prim.isBoolean()) { - return prim.getAsBoolean(); - } else if (prim.isString()) { - return prim.getAsString(); - } else if (prim.isNumber()) { - String strRepesentation = prim.getAsString(); - if (strRepesentation.contains(".")) { - return prim.getAsDouble(); - } else { - return prim.getAsInt(); + private List<Object> handleJsonArray(final JsonElement entry) { + final List<Object> jsonAsArray = new ArrayList<>(); + final JsonArray jsonArray = entry.getAsJsonArray(); + for (final JsonElement jsonElement : jsonArray) { + jsonAsArray.add(handleComplexJsonValue(jsonElement)); + } + return jsonAsArray; + } + + public Object json2JavaPrimitive(final JsonPrimitive jsonPrimitive) { + if (jsonPrimitive.isBoolean()) { + return jsonPrimitive.getAsBoolean(); + } + if (jsonPrimitive.isString()) { + return jsonPrimitive.getAsString(); + } + if (jsonPrimitive.isNumber()) { + if (jsonPrimitive.getAsString().contains(".")) { + return jsonPrimitive.getAsDouble(); } - } else { - throw new IllegalStateException(); + return jsonPrimitive.getAsInt(); } + throw new IllegalStateException(String.format("JSON primitive not supported: %s", jsonPrimitive)); } } |