com.landawn.abacus.util.JSONUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (c) 2015, Haiyang Li.
*
* 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.
*/
package com.landawn.abacus.util;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.landawn.abacus.parser.ParserUtil;
import com.landawn.abacus.parser.ParserUtil.BeanInfo;
import com.landawn.abacus.parser.ParserUtil.PropInfo;
import com.landawn.abacus.type.Type;
/**
*
* @author Haiyang Li
* @since 0.8
*/
public final class JSONUtil {
private JSONUtil() {
// singleton.
}
/**
*
* @param map
* @return
*/
public static JSONObject wrap(final Map map) {
return new JSONObject(map);
}
/**
* wrap(bean) -> wrap(Maps.deepBean2Map(bean, true))
*
* @param bean
* @return
* @see Maps#deepBean2Map(Object)
*/
@SuppressWarnings("unchecked")
public static JSONObject wrap(final Object bean) {
return new JSONObject(bean instanceof Map ? (Map) bean : Maps.deepBean2Map(bean, true));
}
/**
*
* @param array
* @return
* @throws JSONException the JSON exception
*/
public static JSONArray wrap(final boolean[] array) throws JSONException {
return new JSONArray(array);
}
/**
*
* @param array
* @return
* @throws JSONException the JSON exception
*/
public static JSONArray wrap(final char[] array) throws JSONException {
return new JSONArray(array);
}
/**
*
* @param array
* @return
* @throws JSONException the JSON exception
*/
public static JSONArray wrap(final byte[] array) throws JSONException {
return new JSONArray(array);
}
/**
*
* @param array
* @return
* @throws JSONException the JSON exception
*/
public static JSONArray wrap(final short[] array) throws JSONException {
return new JSONArray(array);
}
/**
*
* @param array
* @return
* @throws JSONException the JSON exception
*/
public static JSONArray wrap(final int[] array) throws JSONException {
return new JSONArray(array);
}
/**
*
* @param array
* @return
* @throws JSONException the JSON exception
*/
public static JSONArray wrap(final long[] array) throws JSONException {
return new JSONArray(array);
}
/**
*
* @param array
* @return
* @throws JSONException the JSON exception
*/
public static JSONArray wrap(final float[] array) throws JSONException {
return new JSONArray(array);
}
/**
*
* @param array
* @return
* @throws JSONException the JSON exception
*/
public static JSONArray wrap(final double[] array) throws JSONException {
return new JSONArray(array);
}
/**
*
* @param array
* @return
* @throws JSONException the JSON exception
*/
public static JSONArray wrap(final Object[] array) throws JSONException {
return new JSONArray(array);
}
/**
*
* @param coll
* @return
*/
public static JSONArray wrap(final Collection coll) {
return new JSONArray(coll);
}
/**
*
* @param jsonObject
* @return
* @throws JSONException the JSON exception
*/
public static Map unwrap(final JSONObject jsonObject) throws JSONException {
return unwrap(Map.class, jsonObject);
}
/**
*
* @param
* @param cls
* @param jsonObject
* @return
* @throws JSONException the JSON exception
*/
public static T unwrap(final Class cls, final JSONObject jsonObject) throws JSONException {
return unwrap(N. typeOf(cls), jsonObject);
}
/**
*
* @param
* @param type
* @param jsonObject
* @return
* @throws JSONException the JSON exception
*/
@SuppressWarnings("unchecked")
public static T unwrap(Type type, final JSONObject jsonObject) throws JSONException {
type = type.isObjectType() ? N. typeOf("Map") : type;
final Class cls = type.clazz();
if (type.clazz().isAssignableFrom(JSONObject.class)) {
return (T) jsonObject;
} else if (type.isMap()) {
@SuppressWarnings("rawtypes")
final Map map = N.newMap((Class