org.dromara.hutool.json.JSONGetter Maven / Gradle / Ivy
/*
* Copyright (c) 2013-2024 Hutool Team and hutool.cn
*
* 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 org.dromara.hutool.json;
import org.dromara.hutool.core.lang.getter.TypeGetter;
import org.dromara.hutool.core.util.ObjUtil;
import java.lang.reflect.Type;
import java.util.List;
/**
* 用于JSON的Getter类,提供各种类型的Getter方法
*
* @param Key类型
* @author Looly
*/
public interface JSONGetter extends TypeGetter {
/**
* 获取JSON配置
*
* @return {@link JSONConfig}
* @since 5.3.0
*/
JSONConfig config();
/**
* key对应值是否为{@code null}或无此key
*
* @param key 键
* @return true 无此key或值为{@code null}返回{@code false},其它返回{@code true}
*/
default boolean isNull(final K key) {
return ObjUtil.isNull(this.getObj(key));
}
/**
* 获取字符串类型值,并转义不可见字符,如'\n'换行符会被转义为字符串"\n"
*
* @param key 键
* @return 字符串类型值
* @since 4.2.2
*/
default String getStrEscaped(final K key) {
return getStrEscaped(key, null);
}
/**
* 获取字符串类型值,并转义不可见字符,如'\n'换行符会被转义为字符串"\n"
*
* @param key 键
* @param defaultValue 默认值
* @return 字符串类型值
* @since 4.2.2
*/
default String getStrEscaped(final K key, final String defaultValue) {
return InternalJSONUtil.escape(getStr(key, defaultValue));
}
/**
* 获得JSONArray对象
* 如果值为其它类型对象,尝试转换为{@link JSONArray}返回,否则抛出异常
*
* @param key KEY
* @return JSONArray对象,如果值为{@code null},返回{@code null},非JSONArray类型,尝试转换,转换失败抛出异常
*/
default JSONArray getJSONArray(final K key) {
final Object object = this.getObj(key);
if (ObjUtil.isNull(object)) {
return null;
}
if (object instanceof JSON) {
return (JSONArray) object;
}
return new JSONArray(object, config());
}
/**
* 获得JSONObject对象
* 如果值为其它类型对象,尝试转换为{@link JSONObject}返回,否则抛出异常
*
* @param key KEY
* @return JSONObject对象,如果值为{@code null},返回{@code null},非JSONObject类型,尝试转换,转换失败抛出异常
*/
default JSONObject getJSONObject(final K key) {
final Object object = this.getObj(key);
if (ObjUtil.isNull(object)) {
return null;
}
if (object instanceof JSON) {
return (JSONObject) object;
}
return new JSONObject(object, config());
}
/**
* 从JSON中直接获取Bean对象
* 先获取JSONObject对象,然后转为Bean对象
*
* @param Bean类型
* @param key KEY
* @param beanType Bean类型
* @return Bean对象,如果值为null或者非JSONObject类型,返回null
* @since 3.1.1
*/
default T getBean(final K key, final Class beanType) {
final JSONObject obj = getJSONObject(key);
return (null == obj) ? null : obj.toBean(beanType);
}
/**
* 从JSON中直接获取Bean的List列表
* 先获取JSONArray对象,然后转为Bean的List
*
* @param Bean类型
* @param key KEY
* @param beanType Bean类型
* @return Bean的List,如果值为null或者非JSONObject类型,返回null
* @since 5.7.20
*/
default List getBeanList(final K key, final Class beanType) {
final JSONArray jsonArray = getJSONArray(key);
return (null == jsonArray) ? null : jsonArray.toList(beanType);
}
@Override
default T get(final K key, final Type type, final T defaultValue) {
final Object value = this.getObj(key);
if (ObjUtil.isNull(value)) {
return defaultValue;
}
return get(key, type, config().getConverter(), defaultValue);
}
}