Java Json to Object decode(String json, Class clazz)

Here you can find the source of decode(String json, Class clazz)

Description

json decode

License

Apache License

Parameter

Parameter Description
json a parameter
clazz a parameter

Return

clazz

Declaration

public static <T> T decode(String json, Class<T> clazz) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    /**/*w  w  w. j  ava 2s.  c o  m*/
     * json decode
     * @param json
     * @param clazz
     * @return clazz
     * @author sinmetal
     */
    public static <T> T decode(String json, Class<T> clazz) {
        try {
            return new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).readValue(json,
                    clazz);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * json decode
     * @param json
     * @param valueTypeRef 
     * @return clazz
     * @author sinmetal
     */
    public static <T> T decode(String json, TypeReference<?> valueTypeRef) {
        try {
            return new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).readValue(json,
                    valueTypeRef);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}

Related

  1. decode(String httpMessageBody, Class objectClass)
  2. decode(String input)
  3. decode(String json, Class clazz)
  4. decodeCommandAsJson(final BsonInput bsonInput)
  5. deserialize(JsonReader reader, Class type)
  6. deserialize(String json, Class clazz)
  7. deserialize(String json, Class targetType)