Commit 6f707c20 by Eli Ben Baruch

version 1.1.2: add new apis to JsonHandler

parent 22a39522
group 'com.ipgallery.common' group 'com.ipgallery.common'
version '1.1.1' version '1.1.2'
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'maven-publish' apply plugin: 'maven-publish'
......
package common; package common;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader; import java.io.File;
import com.fasterxml.jackson.databind.SerializationFeature; import java.io.IOException;
public class JsonHandler { public class JsonHandler {
...@@ -128,6 +128,29 @@ public class JsonHandler { ...@@ -128,6 +128,29 @@ public class JsonHandler {
return null; 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){ public static JsonNode getJsonNodeFromObject(Object obj){
JsonNode node = null; JsonNode node = null;
...@@ -149,4 +172,59 @@ public class JsonHandler { ...@@ -149,4 +172,59 @@ public class JsonHandler {
} }
return node; 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