Fixed umarshalling
This commit is contained in:
parent
16d9735851
commit
6ddcd7c7fd
|
@ -186,36 +186,6 @@ public abstract class ElementMapper {
|
||||||
return mapper.writeValueAsString(array);
|
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) {
|
protected static StringBuffer getError(String unknownType) {
|
||||||
StringBuffer stringBuffer = new StringBuffer();
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
stringBuffer.append(unknownType);
|
stringBuffer.append(unknownType);
|
||||||
|
@ -280,6 +250,48 @@ public abstract class ElementMapper {
|
||||||
return jsonNode;
|
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
|
* Creates a resource of given class from its serialization in a given String
|
||||||
* @param clz the class of the resource
|
* @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)
|
public static <ISM extends Element> List<ISM> unmarshalList(Class<ISM> clz, String string)
|
||||||
throws JsonParseException, JsonMappingException, IOException {
|
throws JsonParseException, JsonMappingException, IOException {
|
||||||
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clz);
|
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clz);
|
||||||
return mapper.readValue(string, type);
|
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)
|
public static <ISM extends Element> List<ISM> unmarshalList(String string)
|
||||||
throws JsonParseException, JsonMappingException, IOException {
|
throws JsonParseException, JsonMappingException, IOException {
|
||||||
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Element.class);
|
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Element.class);
|
||||||
return mapper.readValue(string, type);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue