一个通用的Json解析框架接口设计(一)-接口设计

本文介绍了一种统一不同JSON框架(如Fastjson、Jackson、Gson等)调用方式的方法,通过定义顶层JSON接口和其下的JsonObject及JsonArray接口,实现了调用方式的统一,使得在更换JSON框架时无需大量修改代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们项目中可能会用到许多的json框架,Fastjson、jackson、Gson、orgjson、json-lib,其实现方式各不相同,接口Api就不一样,我们想换一个json框架改动的地方就会很多。参考SLF4J的思想,我统一了json框架的调用方式。

我们项目中可能会用到许多的json框架,Fastjson、jackson、Gson、orgjson、json-lib,其实现方式各不相同,接口Api就不一样,我们想换一个json框架改动的地方就会很多。参考SLF4J的思想,我统一了json框架的调用方式。定义了顶层的json接口,其下定义了代表JsonObject和JsonArray的 top.jfunc.json.JsonObject 和 top.jfunc.json.JsonArray 接口,不同的json框架去适配此接口。其实现类为 top.jfunc.json.impl.JSONObject 和 top.jfunc.json.impl.JSONArray 。 注意所有的框架实现,这两个类名不变 。于是调用方完全不用修改代码。

首先定义了顶层的json接口,可以代表jsonobject、jsonarray。

package cn.zytx.common.json;

/**
 * 代表一个JsonObject/JsonArray
 * @author xiongshiyan at 2018/6/10
 */
public interface Json<T extends Json> {
    /**
     * Json对象转换为字符串
     */
    @Override
    String toString();

    @Override
    boolean equals(Object other);

    @Override
    int hashCode();

    /**
     * 解析Json字符串
     * @param jsonString Json字符串
     * @return JsonObject或者JsonArray
     */
    T parse(String jsonString);

    /**
     * 是否严格,像Json没有键而去获取是抛异常还是返回Null
     * @return true if isStrict
     */
    default boolean isStrict(){return true;}

    /**
     * 设置是否严格
     * @param isStrict true if isStrict
     */
    T setStrict(boolean isStrict);

    /**
     * 具体的实现类
     */
    Object unwrap();
}

其下定义了代表jsonobject和jsonarray的cn.zytx.common.json.JsonObject和cn.zytx.common.json.JsonArray接口,不同的json框架去实现此接口。其实现类为cn.zytx.common.json.impl.JSONObjectcn.zytx.common.json.impl.JSONArray注意所有的框架实现,这两个类名不变。于是调用方完全不用修改代码。

package cn.zytx.common.json;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Map;
import java.util.Set;

/**
 * 代表一个Json Object
 * @author xiongshiyan at 2018/6/10
 */
public interface JsonObject extends Json<JsonObject>, Serializable{
    /get-related method

    Object get(String key);
    Object get(String key, Object defaultObject);

    JsonObject getJsonObject(String key);
    JsonArray getJsonArray(String key);

    String getString(String key);
    String getString(String key, String defaultValue);

    Boolean getBoolean(String key);
    Boolean getBoolean(String key, Boolean defaultValue);

    Integer getInteger(String key);
    Integer getInteger(String key, Integer defaultValue);

    Long getLong(String key);
    Long getLong(String key, Long defaultValue);

    Float getFloat(String key);
    Float getFloat(String key, Float defaultValue);

    Double getDouble(String key);
    Double getDouble(String key, Double defaultValue);

    BigInteger getBigInteger(String key);
    BigInteger getBigInteger(String key, BigInteger defaultValue);

    BigDecimal getBigDecimal(String key);
    BigDecimal getBigDecimal(String key, BigDecimal defaultValue);

    <T> T get(String key, Class<T> clazz);

    ///map-related method
    Set<String> keySet();
    int size();
    boolean isEmpty();
    boolean containsKey(String key);
    boolean containsValue(Object value);
    void clear();
    Object remove(String key);
    JsonObject fromMap(Map<String, Object> map);

    /put/change-related method

    JsonObject put(String key, Object value);

    JsonObject putAll(Map<? extends String, ? extends Object> m);
}
package cn.zytx.common.json;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

/**
 * 代表一个Json Array
 * @author xiongshiyan at 2018/6/10
 */
public interface JsonArray extends Json<JsonArray>{
    int size();

    Object get(int index);

    String getString(int index);
    Boolean getBoolean(int index);
    Integer getInteger(int index);
    Long getLong(int index);
    Double getDouble(int index);
    Float getFloat(int index);
    BigInteger getBigInteger(int index);
    BigDecimal getBigDecimal(int index);

    JsonObject getJsonObject(int index);
    JsonArray getJsonArray(int index);

    JsonArray remove(int index);
    JsonArray clear();

    JsonArray put(Object o);
    JsonArray put(int index, Object o);
    JsonArray putAll(Collection<?> os);

    JsonArray fromList(List<Object> list);
}

其中JsonObject还实现了Serializable接口用于序列化JavaBean为String和反序列化。

package cn.zytx.common.json;

/**
 * 序列化,反序列化能力
 * @author xiongshiyan at 2018/6/10
 */
public interface Serializable {
    /**
     * 序列化,把一个JavaBean序列化为String
     */
    String serialize(Object javaBean);

    /**
     * 反序列化,把一个字符串反序列化为一个JavaBean
     */
    <T> T deserialize(String jsonString, Class<T> clazz);
}

一个BaseJson用于处理一些公共的代码

package cn.zytx.common.json.impl;

import cn.zytx.common.json.Json;
import cn.zytx.common.json.JsonException;

/**
 * 提供一些公共的校验方法和特性,子类负责实现
 * @author xiongshiyan at 2018/6/11
 */
public abstract class BaseJson<T extends Json> {
    private boolean isStrict = true;
    private boolean isTolerant = true;

    public boolean isStrict(){return isStrict;}

    public T setStrict(boolean isStrict){this.isStrict = isStrict; return (T)this;}

    public boolean isTolerant(){return isTolerant;}

    public T setTolerant(boolean isTolerant){
        this.isTolerant = isTolerant;
        return (T)this;
    }

    protected void assertKey(String key){
        if(null == key){
            throw new IllegalArgumentException("key 不能为空");
        }
    }
    protected  <R> R checkNullValue(String key , R o) {
        if(null == o){
            if(isStrict()){
                throw new JsonException("不存在key->" + key);
            }else {
                return null;
            }
        }
        return o;
    }
    protected  <R> R checkNullValue(int index , R o) {
        if(null == o){
            if(isStrict()){
                throw new JsonException("不存在->[ " + index + " ]");
            }else {
                return null;
            }
        }
        return o;
    }

    protected void assertIndex(int index , int size){
        //越界
        if(index < 0 || index >= size){
            //严格模式
            if(isStrict()){
                throw new JsonException(new ArrayIndexOutOfBoundsException("index must between 0 and " + size));
            }
        }
    }
}
### 如何下载 ProVerif 工具最新版本 为了获取 ProVerif 的最新版本,可以访问其官方站点提供的链接。以下是具体方法: #### 下载地址 ProVerif 工具的官方网站提供了最新的下载资源,可以通过以下链接访问并下载所需文件: - 官方网站链接:[ProVerif Download](https://prosecco.gforge.inria.fr/personal/bblanche/proverif/) [^1] 在此页面上,您可以找到适用于不同操作系统的二进制文件以及源代码包。 #### 运行环境需求 在下载之前,请确认您的计算机满足 ProVerif 的运行需求。根据已知信息,ProVerif 需要以下基本条件才能正常工作: - 支持的操作系统包括 Windows、Linux 和 macOS。 - 对于某些功能扩展(如 ProVerif Editor),还需要额外依赖项,例如 Python (>= 2.6)、PyGTK 和 PyGTKSourceView2 [^3]。 #### 安装指南 完成下载后,按照以下步骤安装 ProVerif: 1. 将下载的压缩包解压至目标目录。 2. 使用命令提示符导航到该目录。例如,在 Windows 上可通过 `Win+R` 输入 `cmd` 后执行如下命令切换路径: ```bash cd D:\proverifbin2.04\proverif2.04 ``` 3. 测试安装是否成功。通过运行测试脚本验证程序状态: ```bash Proverif test.pv ``` 如果切配置无误,则会显示预期输出结果 [^2]。 对于希望获得更友好交互体验的用户来说,还可以考虑安装带有图形界面支持的 **ProVerif Editor** 插件。它同样可以从指定位置单独获取: - ProVerif Editor 下载页:[ProVerifEditor Project Page](https://sourceforge.net/projects/proverifeditor/) . 最后提醒点,尽管上述资料提到的是较早版本的信息,但总体流程变化不大;建议始终优先查阅官网说明文档来了解最权威指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值