All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zikai.apijson.core.JSON Maven / Gradle / Ivy

The newest version!
/*Copyright (C) 2020 THL A29 Limited, a Tencent company.  All rights reserved.

This source code is licensed under the Apache License Version 2.0.*/

package zikai.apijson.core;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

import java.util.List;

/**阿里FastJSON封装类 防止解析时异常
 * @author Lemon
 */
public class JSON {
	private static final String TAG = "JSON";

	/**判断json格式是否正确
	 * @param s
	 * @return
	 */
	public static boolean isJsonCorrect(String s) {
		//太长		Log.i(TAG, "isJsonCorrect  <<<<     " + s + "     >>>>>>>");
		if (s == null
				//				|| s.equals("[]")
				//				|| s.equals("{}")
				|| s.equals("")
				|| s.equals("[null]")
				|| s.equals("{null}")
				|| s.equals("null")) {
			return false;
		}
		return true;
	}

	/**获取有效的json
	 * @param s
	 * @return
	 */
	public static String getCorrectJson(String s) {
		return getCorrectJson(s, false);
	}
	/**获取有效的json
	 * @param s
	 * @param isArray
	 * @return
	 */
	public static String getCorrectJson(String s, boolean isArray) {
		s = StringUtil.getTrimedString(s);
		//		if (isArray) {
		//			while (s.startsWith("\"")) {
		//				s = s.substring(1);
		//			}
		//			while (s.endsWith("\"")) {
		//				s = s.substring(0, s.length() - 1);
		//			}
		//		}
		return s;//isJsonCorrect(s) ? s : null;
	}

	/**
	 * @param obj
	 * @return
	 */
	public static Object parse(Object obj) {
		try {
			return com.alibaba.fastjson2.JSON.parse(
					obj instanceof String ? (String) obj : toJSONString(obj)
			);
		} catch (Exception e) {
			Log.i(TAG, "parse  catch \n" + e.getMessage());
		}
		return null;
	}

	/**obj转JSONObject
	 * @param obj
	 * @return
	 */
	public static JSONObject parseObject(Object obj) {
		if (obj instanceof JSONObject) {
			return (JSONObject) obj;
		}
		return parseObject(toJSONString(obj));
	}
	/**json转JSONObject
	 * @param json
	 * @return
	 */
	public static JSONObject parseObject(String json) {
		return parseObject(json, JSONObject.class);
	}
	/**json转实体类
	 * @param json
	 * @param clazz
	 * @return
	 */
	public static  T parseObject(String json, Class clazz) {
		if (clazz == null) {
			Log.e(TAG, "parseObject  clazz == null >> return null;");
		} else {
			try {
				return com.alibaba.fastjson2.JSON.parseObject(getCorrectJson(json), clazz);
			} catch (Exception e) {
				Log.i(TAG, "parseObject  catch \n" + e.getMessage());
			}
		}
		return null;
	}

	/**
	 * list转JSONArray
	 */
	public static JSONArray parseArray(List list) {
		return new JSONArray(list);
	}
	/**
	 * obj转JSONArray
	 */
	public static JSONArray parseArray(Object obj) {
		if (obj instanceof JSONArray) {
			return (JSONArray) obj;
		}
		return parseArray(toJSONString(obj));
	}

	/**
	 * json转JSONArray
	 */
	public static JSONArray parseArray(String json) {
		try {
			return com.alibaba.fastjson2.JSON.parseArray(getCorrectJson(json, true));
		} catch (Exception e) {
			Log.i(TAG, "parseArray  catch \n" + e.getMessage());
		}
		return null;
	}

	/**
	 * JSONArray转实体类列表
	 */
	public static  List parseArray(JSONArray array, Class clazz) {
		return parseArray(toJSONString(array), clazz);
	}

	/**
	 * json转实体类列表
	 */
	public static  List parseArray(String json, Class clazz) {
		if (clazz == null) {
			Log.e(TAG, "parseArray  clazz == null >> return null;");
		} else {
			try {
				return com.alibaba.fastjson2.JSON.parseArray(getCorrectJson(json, true), clazz);
			} catch (Exception e) {
				Log.i(TAG, "parseArray  catch \n" + e.getMessage());
			}
		}
		return null;
	}


	public static String toJSONString(Object obj) {
		if (obj instanceof String) {
			return (String) obj;
		}
		try {
			return com.alibaba.fastjson2.JSON.toJSONString(obj);
		} catch (Exception e) {
			Log.e(TAG, "toJSONString  catch \n" + e.getMessage());
		}
		return null;
	}


	public static String toJSONString(Object obj, JSONWriter.Feature... features) {
		if (obj instanceof String) {
			return (String) obj;
		}
		try {
			return com.alibaba.fastjson2.JSON.toJSONString(obj, features);
		} catch (Exception e) {
			Log.e(TAG, "parseArray  catch \n" + e.getMessage());
		}
		return null;
	}



	public static String format(String json) {
		return format(parse(json));
	}


	public static String format(Object object) {
		return toJSONString(object, JSONWriter.Feature.PrettyFormat);
	}

	public static boolean isJSONObject(Object obj) {
		if (obj instanceof JSONObject) {
			return true;
		}
		if (obj instanceof String) {
			try {
				JSONObject json = parseObject((String) obj);
				return json != null && json.isEmpty() == false;
			} catch (Exception e) {
				Log.e(TAG, "isJSONObject  catch \n" + e.getMessage());
			}
		}

		return false;
	}


	public static boolean isJSONArray(Object obj) {
		if (obj instanceof JSONArray) {
			return true;
		}
		if (obj instanceof String) {
			try {
				JSONArray json = parseArray((String) obj);
				return json != null && !json.isEmpty();
			} catch (Exception e) {
				Log.e(TAG, "isJSONArray  catch \n" + e.getMessage());
			}
		}

		return false;
	}

	/**
	 * 判断是否为 Boolean,Number,String 中的一种
	 */
	public static boolean isBooleanOrNumberOrString(Object obj) {
		return obj instanceof Boolean || obj instanceof Number || obj instanceof String;
	}

}