Commit 6f707c20 by Eli Ben Baruch

version 1.1.2: add new apis to JsonHandler

parent 22a39522
group 'com.ipgallery.common'
version '1.1.1'
version '1.1.2'
apply plugin: 'java'
apply plugin: 'maven-publish'
......
package common;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import java.io.File;
import java.io.IOException;
public class JsonHandler {
......@@ -127,7 +127,30 @@ public class JsonHandler {
}
return null;
}
/**
* Use to serialize jsonNode to the class referenced by TypeReference
* @param jsonNode - the json node to serialize
* @param type - typeReference of the object
* @return * Object of the requested type, or null for failure
*/
public static Object getNodeAsObject(JsonNode jsonNode, TypeReference<?> type) {
ObjectReader objectReader = SORTED_MAPPER.reader(type);
if(jsonNode != null && !jsonNode.isNull() && !jsonNode.isMissingNode() && objectReader != null) {
try {
return objectReader.readValue(jsonNode);
} catch (Exception var4) {
var4.printStackTrace();
}
}
else
return null;
return null;
}
public static JsonNode getJsonNodeFromObject(Object obj){
JsonNode node = null;
......@@ -148,5 +171,60 @@ public class JsonHandler {
}
return node;
}
}
/**
* use to serialize a json file indicated by it's location in the file system
* @param strFile - file location in file system
* @return object of type JsonNode, or null for failure
*/
public static JsonNode readJsonNodeFromFile(String strFile) {
File file=new File(strFile);
JsonNode jsonNode = null;
try {
jsonNode = SORTED_MAPPER.readTree(file);
} catch (IOException e) {
e.printStackTrace();
}
return jsonNode;
}
/**
* Use to serialize jsonNode to object of given JavaType
* @param jsonNode - the json node to serialize
* @param javaType - type of the object
* @return Object of the requested type, or null for failure
*/
public static Object getNodeAsObject(JsonNode jsonNode, JavaType javaType) {
ObjectReader objectReader = SORTED_MAPPER.reader(javaType);
if(jsonNode != null && !jsonNode.isNull() && !jsonNode.isMissingNode() && objectReader != null) {
try {
return objectReader.readValue(jsonNode);
} catch (Exception var4) {
var4.printStackTrace();
}
}
else
return null;
return null;
}
/**
* Use to serialize jsonNode to generic class object.
* for example: public class MyClass<T>. the @param outer parameter is for MyClass.class
* and the inner is for the actual class of T.
*
* @param jsonNode - the json node to serialize
* @param outer - the class of the generic class
* @param inner - the actual class of the
* @return Object of the requested type, or null for failure
*/
public static Object getNodeAsParametricTypeObject(JsonNode jsonNode, Class<?> outer, Class<?> inner) {
JavaType javaType = SORTED_MAPPER.getTypeFactory().constructParametricType(outer, inner);
return getNodeAsObject(jsonNode, javaType);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment