Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery.common.java
/
utils
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6f707c20
authored
Jul 12, 2016
by
Eli Ben Baruch
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
version 1.1.2: add new apis to JsonHandler
parent
22a39522
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
8 deletions
build.gradle
src/main/java/common/JsonHandler.java
build.gradle
View file @
6f707c20
group
'com.ipgallery.common'
version
'1.1.
1
'
version
'1.1.
2
'
apply
plugin:
'java'
apply
plugin:
'maven-publish'
...
...
src/main/java/common/JsonHandler.java
View file @
6f707c20
package
common
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.
databind.DeserializationFeatur
e
;
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.TypeReferenc
e
;
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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment