Java Json convertJsonValue(Object jsonValue, Class desiredType)

Here you can find the source of convertJsonValue(Object jsonValue, Class desiredType)

Description

convert Json Value

License

Apache License

Declaration

public static Object convertJsonValue(Object jsonValue, Class desiredType) 

Method Source Code

//package com.java2s;
/*/*from   ww  w. ja  va  2  s.  co m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 javax.json.JsonNumber;

import javax.json.JsonString;
import javax.json.JsonValue;

public class Main {
    public static Object convertJsonValue(Object jsonValue, Class desiredType) {
        if (jsonValue instanceof JsonNumber) {
            JsonNumber number = (JsonNumber) jsonValue;

            if (desiredType == null || desiredType == Long.class || desiredType == Long.TYPE) {
                return number.longValue();
            } else if (desiredType == Integer.class || desiredType == Integer.TYPE) {
                return number.intValue();
            } else if (desiredType == Double.class || desiredType == Double.TYPE) {
                return number.doubleValue();
            } else {
                return number.longValue();
            }
        } else if (jsonValue instanceof JsonString) {
            return ((JsonString) jsonValue).getString();
        } else if (jsonValue instanceof JsonValue) {
            if (jsonValue == JsonValue.TRUE) {
                return true;
            } else if (jsonValue == JsonValue.FALSE) {
                return false;
            } else {
                return jsonValue.toString();
            }
        } else if (jsonValue instanceof Number) {
            Number jsonNumber = (Number) jsonValue;
            if (desiredType == Integer.TYPE || desiredType == Integer.class) {
                return jsonNumber.intValue();
            } else if (desiredType == Long.TYPE || desiredType == Long.class) {
                return jsonNumber.longValue();
            } else if (desiredType == Double.TYPE || desiredType == Double.class) {
                return jsonNumber.doubleValue();
            } else if (desiredType == Short.TYPE || desiredType == Short.class) {
                return jsonNumber.shortValue();
            } else {
                return jsonValue;
            }
        } else if (jsonValue instanceof Object[]) {
            Object[] array = (Object[]) jsonValue;
            for (int i = 0; i < array.length; i++) {
                array[i] = convertJsonValue(array[i], desiredType);
            }
            return array;
        } else {
            return jsonValue;
        }
    }
}

Related

  1. addAll(JsonObjectBuilder a, JsonObjectBuilder b)
  2. asJsonObject(JsonValue value)
  3. asString(JsonValue value)
  4. compare(JsonValue expected, JsonValue actual, boolean strict)
  5. escape(String text)
  6. fill_dictionary(Map dictionary, JsonObject jsonObject)
  7. fill_key_value(JsonObjectBuilder jsonObject, String key, Object value)
  8. fill_list(JsonObjectBuilder jsonObject, String key, List list)

    HOME | Copyright © www.java2s.com 2016