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

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

Description

decode

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/*/*from w w  w .  j a v a 2 s.co m*/
 * Copyright 2014 Alexey Plotnik
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.type.SimpleType;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    private static ObjectMapper mapper = new ObjectMapper();

    public static <T> T decode(String json, Class<T> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (IOException e) {
            throw new RuntimeException("Can't serialize object", e);
        }
    }

    public static <T> T decode(byte[] json, Class<T> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (IOException e) {
            throw new RuntimeException("Can't serialize object", e);
        }
    }

    public static <T> T decode(InputStream jsonStream, Class<T> clazz) {
        try {
            return mapper.readValue(jsonStream, clazz);
        } catch (IOException e) {
            throw new RuntimeException("Can't serialize object", e);
        }
    }

    public static <T> T decode(InputStream jsonStream, SimpleType typeInfo) {
        try {
            return mapper.readValue(jsonStream, typeInfo);
        } catch (IOException e) {
            throw new RuntimeException("Can't serialize object", e);
        }
    }

    public static <T> T decode(String json, JavaType type) {
        try {
            return mapper.readValue(json, type);
        } catch (IOException e) {
            throw new RuntimeException("Can't serialize object", 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)