Skip to content

Utilities for serialize and deserialize from JSON using Jackson

License

Notifications You must be signed in to change notification settings

JarCasting/commons-json-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Commons Json Utils

Utilities for serialize and deserialize from JSON using Jackson

javadoc Maven Central

Maven

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

Examples

JSON to POJO

  • json to Custom Object

        class MyClass {
            int a;
            int b;
        }
    
    
        MyClass result = 
            JsonUtils.fromJson(
                   "{\"a\":1,\"b\":2}", 
                   MyClass.class
            );
  • json to Enum

        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\"");
        }
  • json to Generic Object

        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}}");
    
        }
  • json to HashTable

        Hashtable<String, Integer> result =
                JsonUtils.fromJson("{\"a\":1,\"b\":2}",
                        new TypeReference<Hashtable<String, Integer>>(){}
                );
  • convert json to Key Value Pairs

        // TODO
  • json keyset

        Set<String> result = 
            JsonUtils.jsonKeySet("{\"a\":1,\"b\":2}");
  • json keys

        String[] result = 
            JsonUtils.jsonKeys("{\"a\":1,\"b\":2}");

Override Object.toString()

    @Override
    public String toString() {
        return JsonUtils.toJson(this);
    }

License

Licensed under the Apache License Version 2.0


JarCasting.ru - поиск по центральному Maven-репозиторию

JarCasting.com - Unofficial Maven Central Search Engine