Fixed umarshalling

This commit is contained in:
Luca Frosini 2021-02-17 16:14:22 +01:00
parent 16d9735851
commit 6ddcd7c7fd
1 changed files with 64 additions and 46 deletions

View File

@ -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 extends Element> El unmarshal(Class<El> 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 extends Element> El unmarshal(Class<El> 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 extends Element> El unmarshal(Class<El> 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 extends Element> El unmarshal(Class<El> 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 extends Element> ISM unmarshalWithReader(Class<ISM> clz, String string)
throws JsonParseException, JsonMappingException, IOException {
return mapper.readerFor(clz).readValue(string);
}
public static <ISM extends Element> List<ISM> unmarshalList(Class<ISM> clz, String string)
throws JsonParseException, JsonMappingException, IOException {
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clz);
try {
return mapper.readValue(string, type);
} catch (JsonMappingException e) {
List<ISM> 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 <ISM extends Element> List<ISM> unmarshalList(String string)
throws JsonParseException, JsonMappingException, IOException {
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Element.class);
try {
return mapper.readValue(string, type);
} catch (JsonMappingException e) {
List<ISM> 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;
}
}
}