diff options
Diffstat (limited to 'sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/TypeObjectSerializer.java')
-rw-r--r-- | sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/TypeObjectSerializer.java | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/TypeObjectSerializer.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/TypeObjectSerializer.java index b43e6c100..4fb39415e 100644 --- a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/TypeObjectSerializer.java +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/serialize/TypeObjectSerializer.java @@ -31,22 +31,43 @@ import org.opendaylight.yangtools.yang.binding.TypeObject; public class TypeObjectSerializer extends JsonSerializer<TypeObject> { + /** + * serialize typeobject values + * prefer stringValue() method over getValue() method + */ @Override public void serialize(TypeObject value, JsonGenerator gen, SerializerProvider serializers) throws IOException { - //stringValue Method[] methods = value.getClass().getDeclaredMethods(); String name; + Method getValueMethod = null; for (Method method : methods) { name = method.getName(); - if (method.getParameterCount()==0 && (name.equals("stringValue") || name.equals("getValue"))) { - try { - gen.writeString((String)method.invoke(value)); - break; - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException - | IOException e) { - throw new IOException("No String getter method supported TypeObject for "+value.getClass(),e); + if (method.getParameterCount() == 0) { + if (name.equals("getValue")) { + getValueMethod = method; + } else if (name.equals("stringValue")) { + try { + gen.writeString((String) method.invoke(value)); + break; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | IOException e) { + throw new IOException("No String getter method supported TypeObject for " + value.getClass(), + e); + } } } } + if (getValueMethod != null) { + try { + if (String.class.equals(getValueMethod.getReturnType())) { + gen.writeString((String) getValueMethod.invoke(value)); + } else { + gen.writeObject(getValueMethod.invoke(value)); + } + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | IOException e) { + throw new IOException("No String getter method supported TypeObject for " + value.getClass(), e); + } + + } } } |