diff options
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/coder')
8 files changed, 453 insertions, 99 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java index bb51f2b9..3049a5c2 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/Coder.java @@ -1,8 +1,8 @@ /* * ============LICENSE_START======================================================= - * ONAP PAP + * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,37 @@ import java.io.Writer; public interface Coder { /** + * Converts an object/POJO to an object of the given type. + * + * @param <T> desired type + * @param source source object + * @param clazz class of the desired object type + * @return the converted object + * @throws CoderException if an error occurs + */ + default <S, T> T convert(S source, Class<T> clazz) throws CoderException { + if (source == null) { + return null; + + } else if (clazz == source.getClass()) { + // same class - just cast it + return clazz.cast(source); + + } else if (clazz == String.class) { + // target is a string - just encode the source + return (clazz.cast(encode(source))); + + } else if (source.getClass() == String.class) { + // source is a string - just decode it + return decode(source.toString(), clazz); + + } else { + // do it the long way: encode to a string and then decode the string + return decode(encode(source), clazz); + } + } + + /** * Encodes an object into json. * * @param object object to be encoded @@ -41,6 +72,17 @@ public interface Coder { String encode(Object object) throws CoderException; /** + * Encodes an object into json, optionally making it "pretty". + * + * @param object object to be encoded + * @param pretty {@code true} if it should be encoded as "pretty" json, {@code false} + * otherwise + * @return a json string representing the object + * @throws CoderException if an error occurs + */ + String encode(Object object, boolean pretty) throws CoderException; + + /** * Encodes an object into json, writing to the given target. * * @param target target to which to write the encoded json diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java index 3036d353..daacf479 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/PropertyCoder.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,12 +55,12 @@ public class PropertyCoder { * @return a class T object */ public <T> T decode(String json, String keyProperty, Class<T> clazz) { - JsonElement jsonElement = GSON.fromJson(json, JsonElement.class); + var jsonElement = GSON.fromJson(json, JsonElement.class); return new MyDecoder(jsonElement, keyProperty).decrypt(jsonElement, clazz); } public <T> T decode(Reader reader, String keyProperty, Class<T> clazz) { - JsonElement jsonElement = GSON.fromJson(reader, JsonElement.class); + var jsonElement = GSON.fromJson(reader, JsonElement.class); return new MyDecoder(jsonElement, keyProperty).decrypt(jsonElement, clazz); } @@ -71,9 +71,9 @@ public class PropertyCoder { if (!jsonElement.isJsonObject()) { return; } - JsonObject jsonObject = jsonElement.getAsJsonObject(); + var jsonObject = jsonElement.getAsJsonObject(); // Use keyProperty from input to retrieve secretKey - String secretKey = jsonObject.get(keyProperty).getAsString(); + var secretKey = jsonObject.get(keyProperty).getAsString(); if (!StringUtils.isBlank(secretKey)) { crypto = new CryptoUtils(secretKey); } @@ -97,7 +97,7 @@ public class PropertyCoder { if (!jsonElement.getAsJsonPrimitive().isString()) { return jsonElement; } - String value = jsonElement.getAsString(); + var value = jsonElement.getAsString(); if (!value.startsWith("enc:")) { return jsonElement; } @@ -111,7 +111,7 @@ public class PropertyCoder { if (crypto == null) { return jsonArray; } - JsonArray newArray = new JsonArray(); + var newArray = new JsonArray(); for (JsonElement element: jsonArray) { newArray.add(decrypt(element)); } @@ -122,14 +122,14 @@ public class PropertyCoder { if (crypto == null) { return jsonObject; } - JsonObject newObject = new JsonObject(); + var newObject = new JsonObject(); Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet(); for (Map.Entry<String, JsonElement> entry : entrySet) { String key = entry.getKey(); - JsonElement jsonElement = decrypt(entry.getValue()); + var jsonElement = decrypt(entry.getValue()); newObject.add(key, jsonElement); } return newObject; } } -}
\ No newline at end of file +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java index e84a92f2..d6135afd 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java @@ -1,8 +1,8 @@ /* * ============LICENSE_START======================================================= - * ONAP PAP + * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,35 +38,101 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; import lombok.AccessLevel; -import lombok.Getter; +import lombok.AllArgsConstructor; import org.onap.policy.common.gson.DoubleConverter; import org.onap.policy.common.gson.GsonMessageBodyHandler; /** * JSON encoder and decoder using the "standard" mechanism, which is currently gson. */ +@AllArgsConstructor(access = AccessLevel.PROTECTED) public class StandardCoder implements Coder { /** * Gson object used to encode and decode messages. */ - @Getter(AccessLevel.PROTECTED) - private static final Gson GSON = GsonMessageBodyHandler.configBuilder( - new GsonBuilder().registerTypeAdapter(StandardCoderObject.class, new StandardTypeAdapter())) - .create(); + private static final Gson GSON_STD; + + /** + * Gson object used to encode messages in "pretty" format. + */ + private static final Gson GSON_STD_PRETTY; + + static { + GsonBuilder builder = GsonMessageBodyHandler.configBuilder( + new GsonBuilder().registerTypeAdapter(StandardCoderObject.class, new StandardTypeAdapter())); + + GSON_STD = builder.create(); + GSON_STD_PRETTY = builder.setPrettyPrinting().create(); + } + + /** + * Gson object used to encode and decode messages. + */ + protected final Gson gson; + + /** + * Gson object used to encode messages in "pretty" format. + */ + protected final Gson gsonPretty; /** * Constructs the object. */ public StandardCoder() { - super(); + this(GSON_STD, GSON_STD_PRETTY); + } + + @Override + public <S, T> T convert(S source, Class<T> clazz) throws CoderException { + if (source == null) { + return null; + + } else if (clazz == source.getClass()) { + // same class - just cast it + return clazz.cast(source); + + } else if (clazz == String.class) { + // target is a string - just encode the source + return (clazz.cast(encode(source))); + + } else if (source.getClass() == String.class) { + // source is a string - just decode it + return decode(source.toString(), clazz); + + } else { + /* + * Do it the long way: encode to a tree and then decode the tree. This entire + * method could have been left out and the default Coder.convert() used + * instead, but this should perform slightly better as it only uses a + * JsonElement as the intermediate data structure, while Coder.convert() goes + * all the way to a String as the intermediate data structure. + */ + try { + return fromJson(toJsonTree(source), clazz); + } catch (RuntimeException e) { + throw new CoderException(e); + } + } } @Override public String encode(Object object) throws CoderException { + return encode(object, false); + } + + @Override + public String encode(Object object, boolean pretty) throws CoderException { try { - return toJson(object); + if (pretty) { + return toPrettyJson(object); + + } else { + return toJson(object); + } } catch (RuntimeException e) { throw new CoderException(e); @@ -78,7 +144,7 @@ public class StandardCoder implements Coder { try { toJson(target, object); - } catch (RuntimeException | IOException e) { + } catch (RuntimeException e) { throw new CoderException(e); } } @@ -86,7 +152,7 @@ public class StandardCoder implements Coder { @Override public void encode(OutputStream target, Object object) throws CoderException { try { - Writer wtr = makeWriter(target); + var wtr = makeWriter(target); toJson(wtr, object); // flush, but don't close @@ -99,7 +165,7 @@ public class StandardCoder implements Coder { @Override public void encode(File target, Object object) throws CoderException { - try (Writer wtr = makeWriter(target)) { + try (var wtr = makeWriter(target)) { toJson(wtr, object); // no need to flush or close here @@ -141,7 +207,7 @@ public class StandardCoder implements Coder { @Override public <T> T decode(File source, Class<T> clazz) throws CoderException { - try (Reader input = makeReader(source)) { + try (var input = makeReader(source)) { return fromJson(input, clazz); } catch (RuntimeException | IOException e) { @@ -149,10 +215,20 @@ public class StandardCoder implements Coder { } } + /** + * Encodes the object as "pretty" json. + * + * @param object object to be encoded + * @return the encoded object + */ + protected String toPrettyJson(Object object) { + return gsonPretty.toJson(object); + } + @Override public StandardCoderObject toStandard(Object object) throws CoderException { try { - return new StandardCoderObject(GSON.toJsonTree(object)); + return new StandardCoderObject(gson.toJsonTree(object)); } catch (RuntimeException e) { throw new CoderException(e); @@ -162,7 +238,7 @@ public class StandardCoder implements Coder { @Override public <T> T fromStandard(StandardCoderObject sco, Class<T> clazz) throws CoderException { try { - return GSON.fromJson(sco.getData(), clazz); + return gson.fromJson(sco.getData(), clazz); } catch (RuntimeException e) { throw new CoderException(e); @@ -220,7 +296,7 @@ public class StandardCoder implements Coder { * @return a json element representing the object */ protected JsonElement toJsonTree(Object object) { - return GSON.toJsonTree(object); + return gson.toJsonTree(object); } /** @@ -230,7 +306,7 @@ public class StandardCoder implements Coder { * @return a json string representing the object */ protected String toJson(Object object) { - return GSON.toJson(object); + return gson.toJson(object); } /** @@ -238,10 +314,9 @@ public class StandardCoder implements Coder { * * @param target target to which to write the encoded json * @param object object to be encoded - * @throws IOException if an I/O error occurs */ - protected void toJson(Writer target, Object object) throws IOException { - GSON.toJson(object, object.getClass(), target); + protected void toJson(Writer target, Object object) { + gson.toJson(object, object.getClass(), target); } /** @@ -252,7 +327,7 @@ public class StandardCoder implements Coder { * @return the object represented by the given json element */ protected <T> T fromJson(JsonElement json, Class<T> clazz) { - return convertFromDouble(clazz, GSON.fromJson(json, clazz)); + return convertFromDouble(clazz, gson.fromJson(json, clazz)); } /** @@ -263,7 +338,7 @@ public class StandardCoder implements Coder { * @return the object represented by the given json string */ protected <T> T fromJson(String json, Class<T> clazz) { - return convertFromDouble(clazz, GSON.fromJson(json, clazz)); + return convertFromDouble(clazz, gson.fromJson(json, clazz)); } /** @@ -274,7 +349,7 @@ public class StandardCoder implements Coder { * @return the object represented by the given json string */ protected <T> T fromJson(Reader source, Class<T> clazz) { - return convertFromDouble(clazz, GSON.fromJson(source, clazz)); + return convertFromDouble(clazz, gson.fromJson(source, clazz)); } /** @@ -286,8 +361,8 @@ public class StandardCoder implements Coder { * @param value value to be converted * @return the converted value */ - private <T> T convertFromDouble(Class<T> clazz, T value) { - if (clazz != Object.class) { + protected <T> T convertFromDouble(Class<T> clazz, T value) { + if (clazz != Object.class && !Map.class.isAssignableFrom(clazz) && !List.class.isAssignableFrom(clazz)) { return value; } @@ -297,20 +372,14 @@ public class StandardCoder implements Coder { /** * Adapter for standard objects. */ - private static class StandardTypeAdapter extends TypeAdapter<StandardCoderObject> { + @AllArgsConstructor + protected static class StandardTypeAdapter extends TypeAdapter<StandardCoderObject> { /** * Used to read/write a JsonElement. */ private static TypeAdapter<JsonElement> elementAdapter = new Gson().getAdapter(JsonElement.class); - /** - * Constructs the object. - */ - public StandardTypeAdapter() { - super(); - } - @Override public void write(JsonWriter out, StandardCoderObject value) throws IOException { elementAdapter.write(out, value.getData()); diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderInstantAsMillis.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderInstantAsMillis.java new file mode 100644 index 00000000..27b239bb --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderInstantAsMillis.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.coder; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.time.Instant; +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.onap.policy.common.gson.InstantAsMillisTypeAdapter; + +/** + * JSON encoder and decoder using the "standard" mechanism, but encodes Instant fields as + * Long milliseconds. + */ +public class StandardCoderInstantAsMillis extends StandardCoder { + + /** + * Gson object used to encode and decode messages. + */ + private static final Gson GSON_INSTANT; + + /** + * Gson object used to encode messages in "pretty" format. + */ + private static final Gson GSON_INSTANT_PRETTY; + + static { + GsonBuilder builder = GsonMessageBodyHandler + .configBuilder(new GsonBuilder().registerTypeAdapter(StandardCoderObject.class, + new StandardTypeAdapter())) + .registerTypeAdapter(Instant.class, new InstantAsMillisTypeAdapter()); + + GSON_INSTANT = builder.create(); + GSON_INSTANT_PRETTY = builder.setPrettyPrinting().create(); + } + + /** + * Constructs the object. + */ + public StandardCoderInstantAsMillis() { + super(GSON_INSTANT, GSON_INSTANT_PRETTY); + } +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java index 60c5f4ef..55f7f9d7 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoderObject.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,18 +21,29 @@ package org.onap.policy.common.utils.coder; import com.google.gson.JsonElement; +import java.io.Serializable; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; /** * Object type used by the {@link StandardCoder}. Different serialization tools have * different "standard objects". For instance, GSON uses {@link JsonElement}. This class * wraps that object so that it can be used without exposing the object, itself. */ -public class StandardCoderObject { +@AllArgsConstructor(access = AccessLevel.PROTECTED) +public class StandardCoderObject implements Serializable { + private static final long serialVersionUID = 1L; /** * Data wrapped by this. */ - private final JsonElement data; + /* + * this should not be transient, but since it isn't serializable, we're stuck with it + * until there's time to address the issue + */ + @Getter(AccessLevel.PROTECTED) + private final transient JsonElement data; /** * Constructs the object. @@ -42,52 +53,75 @@ public class StandardCoderObject { } /** - * Constructs the object. - * - * @param data data wrapped by this object. - */ - protected StandardCoderObject(JsonElement data) { - this.data = data; - } - - /** - * Gets the data wrapped by this. - * - * @return the data wrapped by this - */ - protected JsonElement getData() { - return data; - } - - /** * Gets a field's value from this object, traversing the object hierarchy. * - * @param fields field hierarchy + * @param fields field hierarchy. These may be strings, identifying fields within the + * object, or Integers, identifying an index within an array * @return the field value or {@code null} if the field does not exist or is not a * primitive */ - public String getString(String... fields) { - - /* - * This could be relatively easily modified to allow Integer arguments, as well, - * which would be used to specify indices within an array. - */ + public String getString(Object... fields) { JsonElement jel = data; - for (String field : fields) { + for (Object field : fields) { if (jel == null) { return null; } - if (jel.isJsonObject()) { - jel = jel.getAsJsonObject().get(field); + if (field instanceof String) { + jel = getFieldFromObject(jel, field.toString()); + + } else if (field instanceof Integer) { + jel = getItemFromArray(jel, (int) field); } else { - return null; + throw new IllegalArgumentException("subscript is not a string or integer: " + field); } } return (jel != null && jel.isJsonPrimitive() ? jel.getAsString() : null); } + + /** + * Gets an item from an object. + * + * @param element object from which to extract the item + * @param field name of the field from which to extract the item + * @return the item, or {@code null} if the element is not an object or if the field + * does not exist + */ + protected JsonElement getFieldFromObject(JsonElement element, String field) { + if (!element.isJsonObject()) { + return null; + } + + return element.getAsJsonObject().get(field); + } + + /** + * Gets an item from an array. + * + * @param element array from which to extract the item + * @param index index of the item to extract + * @return the item, or {@code null} if the element is not an array or if the index is + * out of bounds + */ + protected JsonElement getItemFromArray(JsonElement element, int index) { + if (index < 0) { + throw new IllegalArgumentException("subscript is invalid: " + index); + } + + if (!element.isJsonArray()) { + return null; + } + + var array = element.getAsJsonArray(); + + if (index >= array.size()) { + return null; + } + + return array.get(index); + } } diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java new file mode 100644 index 00000000..4deeba14 --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java @@ -0,0 +1,119 @@ +/*-- + * ============LICENSE_START======================================================= + * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.utils.coder; + +import com.worldturner.medeia.api.JsonSchemaVersion; +import com.worldturner.medeia.api.SchemaSource; +import com.worldturner.medeia.api.StringSchemaSource; +import com.worldturner.medeia.api.ValidationFailedException; +import com.worldturner.medeia.api.gson.MedeiaGsonApi; +import com.worldturner.medeia.schema.validation.SchemaValidator; +import java.io.Reader; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import lombok.NonNull; +import lombok.ToString; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Extension to the StandardCoder to support streaming validation against a Draft-07 Json schema specification. + */ + +@ToString +public class StandardValCoder extends StandardCoder { + + // The medeia-validator library integrates better than + // other libraries considered with GSON, and therefore + // the StandardCoder. + + private static final Logger logger = LoggerFactory.getLogger(StandardValCoder.class); + + private final MedeiaGsonApi validatorApi = new MedeiaGsonApi(); + private final SchemaValidator validator; + + /** + * StandardCoder with validation. + */ + public StandardValCoder(@NonNull String jsonSchema, @NonNull String name) { + SchemaSource schemaSource = new StringSchemaSource(jsonSchema, JsonSchemaVersion.DRAFT07, null, name); + this.validator = validatorApi.loadSchema(schemaSource); + } + + @Override + protected String toPrettyJson(Object object) { + /* + * The validator strips off the "pretty" stuff (i.e., spaces), thus we have to validate and generate the pretty + * JSON in separate steps. + */ + gson.toJson(object, object.getClass(), validatorApi.createJsonWriter(validator, new StringWriter())); + + return super.toPrettyJson(object); + } + + @Override + protected String toJson(@NonNull Object object) { + var output = new StringWriter(); + toJson(output, object); + return output.toString(); + } + + @Override + protected void toJson(@NonNull Writer target, @NonNull Object object) { + gson.toJson(object, object.getClass(), validatorApi.createJsonWriter(validator, target)); + } + + @Override + protected <T> T fromJson(@NonNull Reader source, @NonNull Class<T> clazz) { + return convertFromDouble(clazz, gson.fromJson(validatorApi.createJsonReader(validator, source), clazz)); + } + + @Override + protected <T> T fromJson(String json, Class<T> clazz) { + var reader = new StringReader(json); + return convertFromDouble(clazz, gson.fromJson(validatorApi.createJsonReader(validator, reader), clazz)); + } + + /** + * Is the json conformant?. + */ + public boolean isConformant(@NonNull String json) { + try { + conformance(json); + } catch (CoderException e) { + logger.info("JSON is not conformant to schema", e); + return false; + } + return true; + } + + /** + * Check a json string for conformance against its schema definition. + */ + public void conformance(@NonNull String json) throws CoderException { + try { + validatorApi.parseAll(validatorApi.createJsonReader(validator, new StringReader(json))); + } catch (ValidationFailedException e) { + throw new CoderException(e); + } + } +} diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java index 36f15b96..d94ddca4 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardYamlCoder.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,7 +34,18 @@ public class StandardYamlCoder extends StandardCoder { * Constructs the object. */ public StandardYamlCoder() { - translator = new YamlJsonTranslator(getGSON()); + translator = new YamlJsonTranslator(gson) { + @Override + protected <T> T convertFromDouble(Class<T> clazz, T value) { + return StandardYamlCoder.this.convertFromDouble(clazz, value); + } + }; + } + + @Override + protected String toPrettyJson(Object object) { + // YAML is already "pretty" + return toJson(object); } @Override diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java b/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java index 906c9fdd..077246bf 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java +++ b/utils/src/main/java/org/onap/policy/common/utils/coder/YamlJsonTranslator.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ package org.onap.policy.common.utils.coder; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonNull; @@ -31,9 +32,12 @@ import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; +import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; +import lombok.AllArgsConstructor; +import org.onap.policy.common.gson.InstantTypeAdapter; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.emitter.Emitter; @@ -50,7 +54,13 @@ import org.yaml.snakeyaml.serializer.Serializer; /** * YAML-JSON translator. The methods may throw either of the runtime exceptions, * YAMLException or JsonSyntaxException. + * <p/> + * Note: if the invoker wishes Double to be converted to Integer/Long when type + * Object.class is requested, then a Gson object must be used that will perform the + * translation. In addition, the {@link #convertFromDouble(Class, Object)} method should + * be overridden with an appropriate conversion method. */ +@AllArgsConstructor public class YamlJsonTranslator { /** @@ -62,16 +72,9 @@ public class YamlJsonTranslator { * Constructs the object. */ public YamlJsonTranslator() { - this(new Gson()); - } - - /** - * Constructs the object. - * - * @param gson the Gson object to be used to serialize and de-serialize - */ - public YamlJsonTranslator(Gson gson) { - this.gson = gson; + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Instant.class, new InstantTypeAdapter()); + gson = builder.create(); } /** @@ -81,7 +84,7 @@ public class YamlJsonTranslator { * @return YAML representing the original object */ public String toYaml(Object object) { - StringWriter output = new StringWriter(); + var output = new StringWriter(); toYaml(output, object); return output.toString(); } @@ -93,8 +96,8 @@ public class YamlJsonTranslator { * @param object POJO to be translated */ public void toYaml(Writer target, Object object) { - DumperOptions dumper = new DumperOptions(); - Serializer serializer = new Serializer(new Emitter(target, dumper), new Resolver(), dumper, null); + var dumper = new DumperOptions(); + var serializer = new Serializer(new Emitter(target, dumper), new Resolver(), dumper, null); try { serializer.open(); @@ -135,7 +138,7 @@ public class YamlJsonTranslator { * @return a POJO representing the YAML read from the reader */ public <T> T fromYaml(Reader source, Class<T> clazz) { - Node node = new Yaml().compose(source); + var node = new Yaml().compose(source); return fromJson(makeJson(node), clazz); } @@ -147,7 +150,22 @@ public class YamlJsonTranslator { * @return a POJO representing the original element */ protected <T> T fromJson(JsonElement jel, Class<T> clazz) { - return gson.fromJson(jel, clazz); + return convertFromDouble(clazz, gson.fromJson(jel, clazz)); + } + + /** + * Converts a value from Double to Integer/Long, walking the value's contents if it's + * a List/Map. Only applies if the specified class refers to the Object class. + * Otherwise, it leaves the value unchanged. + * <p/> + * The default method simply returns the original value. + * + * @param clazz class of object to be decoded + * @param value value to be converted + * @return the converted value + */ + protected <T> T convertFromDouble(Class<T> clazz, T value) { + return value; } /** @@ -261,7 +279,7 @@ public class YamlJsonTranslator { protected JsonArray makeJsonArray(SequenceNode node) { List<Node> nodes = node.getValue(); - JsonArray array = new JsonArray(nodes.size()); + var array = new JsonArray(nodes.size()); nodes.forEach(subnode -> array.add(makeJson(subnode))); return array; @@ -274,10 +292,10 @@ public class YamlJsonTranslator { * @return a gson element corresponding to the node */ protected JsonObject makeJsonObject(MappingNode node) { - JsonObject obj = new JsonObject(); + var obj = new JsonObject(); for (NodeTuple tuple : node.getValue()) { - Node key = tuple.getKeyNode(); + var key = tuple.getKeyNode(); String skey = ((ScalarNode) key).getValue(); obj.add(skey, makeJson(tuple.getValueNode())); @@ -294,7 +312,7 @@ public class YamlJsonTranslator { */ protected JsonElement makeJsonPrim(ScalarNode node) { try { - Tag tag = node.getTag(); + var tag = node.getTag(); if (tag == Tag.INT) { return new JsonPrimitive(Long.valueOf(node.getValue())); |