Utilities for serialize and deserialize from JSON using Jackson
Functionality of this package is contained in Java package com.fasterxml.jackson.databind
, and can be used using following Maven dependency:
<dependencies>
...
<dependency>
<groupId>com.jarcasting</groupId>
<artifactId>commons-json-utils</artifactId>
<version>1.0.7</version>
</dependency>
...
</dependencies>
Depends on:
com.fasterxml.jackson.core : jackson-databind
com.fasterxml.jackson.module : jackson-module-parameter-names
com.fasterxml.jackson.datatype : jackson-datatype-jdk8
com.fasterxml.jackson.datatype : jackson-datatype-jsr310
com.fasterxml.jackson.datatype : jackson-datatype-jsr353
org.apache.commons : commons-lang3
-
class MyClass { int a; int b; } MyClass result = JsonUtils.fromJson( "{\"a\":1,\"b\":2}", MyClass.class );
-
enum MyEnum { A, B } enum MyEnum2 { A(87), B(89); int myField; MyEnum2(int myField) { this.myField=myField; } } public void jsonToEnum(){ MyEnum result = JsonUtils.fromJson( "\"A\"", MyEnum.class ); MyEnum2 result2 = JsonUtils.fromJson( "\"B\"", MyEnum2.class ); Assert.assertEquals(JsonUtils.toJson2(result), "\"A\""); Assert.assertEquals(JsonUtils.toJson2(result2), "\"B\""); }
-
static class MyGenericClass<T> { T value; } public void jsonToGenericObject() throws Exception { MyGenericClass<MySimpleClass> result = JsonUtils.fromJson( "{\"value\":{\"a\":1,\"b\":2}}", new TypeReference<MyGenericClass<MySimpleClass>>() { } ); Assert.assertEquals(JsonUtils.toJson2(result), "{\"value\":{\"a\":1,\"b\":2}}"); }
-
Hashtable<String, Integer> result = JsonUtils.fromJson("{\"a\":1,\"b\":2}", new TypeReference<Hashtable<String, Integer>>(){} );
-
// TODO
-
Set<String> result = JsonUtils.jsonKeySet("{\"a\":1,\"b\":2}");
-
String[] result = JsonUtils.jsonKeys("{\"a\":1,\"b\":2}");
@Override
public String toString() {
return JsonUtils.toJson(this);
}
Licensed under the Apache License Version 2.0