Refs #10804: Add possibility to marshal/unmarshall list and array of Records

Task-Url: https://support.d4science.org/issues/10804

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib@161866 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-12-22 13:53:40 +00:00
parent 49313c4c1e
commit fcb6dae159
1 changed files with 43 additions and 48 deletions

View File

@ -4,9 +4,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.gcube.documentstore.records.implementation.AbstractRecord;
@ -27,7 +25,7 @@ public class DSMapper {
protected static final ObjectMapper mapper;
private DSMapper(){}
private DSMapper() { }
/**
* @return the ObjectMapper
@ -45,61 +43,67 @@ public class DSMapper {
mapper.registerSubtypes(AbstractRecord.class);
}
/**
* Write the serialization of a given resource to a given
* Write the serialization of a given Record to a given
* {@link OutputStream} .
*
* @param object the resource
* @param record the Record
* @param stream the stream in input
* @return the OutputStream
* @throws IOException
* @throws JsonMappingException
* @throws JsonGenerationException
*/
public static <T extends OutputStream, R extends Record> T marshal(R object, T stream)
public static <T extends OutputStream, R extends Record> T marshal(R record, T stream)
throws JsonGenerationException, JsonMappingException, IOException {
mapper.writeValue(stream, object);
mapper.writeValue(stream, record);
return stream;
}
/**
* Write the serialization of a given resource to a given {@link Writer} .
* @param object the resource
* Write the serialization of a given Record to a given {@link Writer} .
* @param record the Record
* @param writer the writer in input
* @return the Writer
* @throws IOException
* @throws JsonMappingException
* @throws JsonGenerationException
*/
public static <T extends Writer, R extends Record> T marshal(R object, T writer)
public static <T extends Writer, R extends Record> T marshal(R record, T writer)
throws JsonGenerationException, JsonMappingException, IOException {
mapper.writeValue(writer, object);
mapper.writeValue(writer, record);
return writer;
}
/**
* Return the String serialization of a given resource
* @param object the resource
* @return the String serialization of a given resource
* Return the String serialization of a given Record
* @param record the resource
* @return the String serialization of a given Record
* @throws JsonProcessingException
*/
public static <R extends Record> String marshal(R object) throws JsonProcessingException {
return mapper.writeValueAsString(object);
public static <R extends Record> String marshal(R record) throws JsonProcessingException {
return mapper.writeValueAsString(record);
}
/**
* Return the String serialization of a given resource
* @param list the list of Records
* @return the String serialization of a given resource
* @throws IOException
* Return the String serialization of a given list
* @param list the list to marshal
* @return the String serialization of a given list
* @throws JsonProcessingException
*/
public static <R extends Record> String marshal(List<R> list) throws IOException {
final StringWriter sw =new StringWriter();
mapper.writeValue(sw, list);
String ret = sw.toString();
sw.close();
return ret;
public static <R extends Record> String marshal(List<R> list) throws JsonProcessingException {
JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, Record.class);
return mapper.writerFor(type).writeValueAsString(list);
}
/**
* Return the String serialization of a given array
* @param array the array to marshal
* @return the String serialization of a given array
* @throws JsonProcessingException
*/
public static <R extends Record> String marshal(R[] array) throws JsonProcessingException {
return mapper.writeValueAsString(array);
}
/**
@ -131,38 +135,30 @@ public class DSMapper {
throws JsonParseException, JsonMappingException, IOException {
return mapper.readValue(stream, clz);
}
/**
* Creates a resource of given class from its serialization in a given String
* @param clz the class of the resource
* Creates a record of given class from its serialization in a given String
* @param clz the class of the record
* @param string
* @return the resource
* @return the record
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
public static <R extends Record> R unmarshal(Class<R> clz, String string)
public static <R extends Record> R unmarshal(Class<R> clz, String string)
throws JsonParseException, JsonMappingException, IOException {
return mapper.readValue(string, clz);
}
public static <R extends Record> List<R> unmarshalList(Class<R> clz , String string)
public static <R extends Record> List<R> unmarshalList(Class<R> clz, String string)
throws JsonParseException, JsonMappingException, IOException {
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clz) ;
JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clz);
return mapper.readValue(string, type);
}
public static <R extends Record> List<R> unmarshalList(String string)
public static <R extends Record> List<R> unmarshalList(String string)
throws JsonParseException, JsonMappingException, IOException {
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Record.class) ;
JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, Record.class);
return mapper.readValue(string, type);
}
@ -173,8 +169,7 @@ public class DSMapper {
public static JsonNode asJsonNode(String jsonString) throws JsonProcessingException, IOException {
ObjectMapper mapperJson = new ObjectMapper();
return mapperJson.readTree(jsonString);
return mapperJson.readTree(jsonString);
}
}