From 6ddcd7c7fddf7aa8b394bb5e46ed4f1b2a7b5f5e Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 17 Feb 2021 16:14:22 +0100 Subject: [PATCH] Fixed umarshalling --- .../utils/ElementMapper.java | 110 ++++++++++-------- 1 file changed, 64 insertions(+), 46 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/utils/ElementMapper.java b/src/main/java/org/gcube/informationsystem/utils/ElementMapper.java index ef8c1f6..108d698 100644 --- a/src/main/java/org/gcube/informationsystem/utils/ElementMapper.java +++ b/src/main/java/org/gcube/informationsystem/utils/ElementMapper.java @@ -186,36 +186,6 @@ public abstract class ElementMapper { return mapper.writeValueAsString(array); } - /** - * Creates a resource of given class from its serialization in a given - * {@link Reader}. - * @param clz the class of the resource - * @param reader the reader - * @return the resource - * @throws JsonParseException - * @throws JsonMappingException - * @throws IOException - */ - public static El unmarshal(Class clz, Reader reader) - throws JsonParseException, JsonMappingException, IOException { - return mapper.readValue(reader, clz); - } - - /** - * Creates a resource of given class from its serialization in a given - * {@link InputStream}. - * @param clz the class of the resource - * @param stream the stream - * @return the resource - * @throws IOException - * @throws JsonMappingException - * @throws JsonParseException - */ - public static El unmarshal(Class clz, InputStream stream) - throws JsonParseException, JsonMappingException, IOException { - return mapper.readValue(stream, clz); - } - protected static StringBuffer getError(String unknownType) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(unknownType); @@ -280,6 +250,48 @@ public abstract class ElementMapper { return jsonNode; } + /** + * Creates a resource of given class from its serialization in a given + * {@link Reader}. + * @param clz the class of the resource + * @param reader the reader + * @return the resource + * @throws JsonParseException + * @throws JsonMappingException + * @throws IOException + */ + public static El unmarshal(Class clz, Reader reader) + throws JsonParseException, JsonMappingException, IOException { + try { + return mapper.readValue(reader, clz); + } catch (JsonMappingException e) { + JsonNode jsonNode = mapper.readTree(reader); + jsonNode = analizeFullJson(jsonNode); + return ElementMapper.unmarshal(clz, mapper.writeValueAsString(jsonNode)); + } + } + + /** + * Creates a resource of given class from its serialization in a given + * {@link InputStream}. + * @param clz the class of the resource + * @param stream the stream + * @return the resource + * @throws IOException + * @throws JsonMappingException + * @throws JsonParseException + */ + public static El unmarshal(Class clz, InputStream stream) + throws JsonParseException, JsonMappingException, IOException { + try { + return mapper.readValue(stream, clz); + } catch (JsonMappingException e) { + JsonNode jsonNode = mapper.readTree(stream); + jsonNode = analizeFullJson(jsonNode); + return ElementMapper.unmarshal(clz, mapper.writeValueAsString(jsonNode)); + } + } + /** * Creates a resource of given class from its serialization in a given String * @param clz the class of the resource @@ -300,30 +312,36 @@ public abstract class ElementMapper { } } - /** - * Creates a resource of given class from its serialization in a given String - * @param clz the class of the resource - * @param string - * @return the resource - * @throws JsonParseException - * @throws JsonMappingException - * @throws IOException - */ - public static ISM unmarshalWithReader(Class clz, String string) - throws JsonParseException, JsonMappingException, IOException { - return mapper.readerFor(clz).readValue(string); - } - public static List unmarshalList(Class clz, String string) throws JsonParseException, JsonMappingException, IOException { JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clz); - return mapper.readValue(string, type); + try { + return mapper.readValue(string, type); + } catch (JsonMappingException e) { + List ret = new ArrayList<>(); + ArrayNode arrayNode = (ArrayNode) mapper.readTree(string); + for(JsonNode jsonNode : arrayNode) { + jsonNode = analizeFullJson(jsonNode); + ret.add(ElementMapper.unmarshal(clz, mapper.writeValueAsString(jsonNode))); + } + return ret; + } } public static List unmarshalList(String string) throws JsonParseException, JsonMappingException, IOException { JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Element.class); - return mapper.readValue(string, type); + try { + return mapper.readValue(string, type); + } catch (JsonMappingException e) { + List ret = new ArrayList<>(); + ArrayNode arrayNode = (ArrayNode) mapper.readTree(string); + for(JsonNode jsonNode : arrayNode) { + jsonNode = analizeFullJson(jsonNode); + ret.add((ISM) ElementMapper.unmarshal(Element.class, mapper.writeValueAsString(jsonNode))); + } + return ret; + } } }