diff options
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java')
-rw-r--r-- | utils/src/main/java/org/onap/policy/common/utils/coder/StandardCoder.java | 135 |
1 files changed, 102 insertions, 33 deletions
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()); |