Java Json to Object decode(String input)

Here you can find the source of decode(String input)

Description

decode

License

Apache License

Declaration

public static JsonNode decode(String input) 

Method Source Code

//package com.java2s;
/**/*  w  w w.  j  a  v  a2s . c om*/
 * Copyright (c) 2014 SQUARESPACE, Inc.
 *
 * 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 java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.node.MissingNode;

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

    public static JsonNode decode(String input) {
        return decode(input, false);
    }

    /**
     * Attempt to decode the input as JSON. Returns the {@link JsonNode} if
     * the decode is successful.
     *
     * If the decode fails and the {@code failQuietly} flag is true, returns a
     * {@link MissingNode}. Otherwise an {@link IllegalArgumentException} will
     * be thrown.
     */
    public static JsonNode decode(String input, boolean failQuietly) {
        try {
            return MAPPER.readTree(input);
        } catch (IOException e) {
            if (failQuietly) {
                return MissingNode.getInstance();
            }
            throw new IllegalArgumentException("Unable to decode JSON", e);
        }
    }
}

Related

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