Java Json to Object decode(String httpMessageBody, Class objectClass)

Here you can find the source of decode(String httpMessageBody, Class objectClass)

Description

decode

License

Open Source License

Declaration

public static <T> T decode(String httpMessageBody, Class<T> objectClass) 

Method Source Code

//package com.java2s;
/*//w  w w  . ja va 2s . co m
 * Ejisto, a powerful developer assistant
 *
 * Copyright (C) 2010-2013 Celestino Bellone
 *
 * Ejisto is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ejisto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.io.IOException;

public class Main {
    public static <T> T decode(String httpMessageBody, Class<T> objectClass) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(httpMessageBody, objectClass);
        } catch (IOException e) {
            throw new IllegalArgumentException("httpMessageBody is not a valid JSON", e);
        }
    }

    public static <T> T decode(String httpMessageBody, TypeReference<T> typeReference) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(httpMessageBody, typeReference);
        } catch (IOException e) {
            throw new IllegalArgumentException("httpMessageBody is not a valid JSON", e);
        }
    }
}

Related

  1. decode(String input)
  2. decode(String json, Class clazz)
  3. decode(String json, Class clazz)
  4. decodeCommandAsJson(final BsonInput bsonInput)