Antlr Json解析过程——Json解析器的诞生(3) - xcr1234/json GitHub Wiki

版权所有,转载请注明来源!

当每次进入一个ObjectContext,也就是一个{...}的时候,就意味着有一个新的JsonObject要被产生了。因此new一个JsonObject,并将它放到property中;之后便是对这个JsonObject内容的处理。 ObjectContext是由若干个PairContext组成的(pair是key:value的形式)。每一个pair就是JsonObject中的一个键值对。在pair中,我们可以拿到它的STRING() [代表键]、ValueContext(代表值)。 而ValueContext值可能是多种情况,包括number、boolean、string、JsonObject、JsonArray等,因此,需要再借助一个property来处理这些值。

JsonObjectListener的源码如下:

public class JsonObjectListener extends JSONBaseListener{

    //ParseTreeProperty其实就是HashMap

    protected ParseTreeProperty<Object> property = new ParseTreeProperty<Object>();
    protected ParseTreeProperty<JsonObject> jsonObjectParseTreeProperty = new ParseTreeProperty<JsonObject>();
    protected ParseTreeProperty<JsonArray> jsonArrayParseTreeProperty = new ParseTreeProperty<JsonArray>();
    private JsonObject jsonObject;    //最终解析出的结果
    private DeserializeFeature feature; 

    public JsonObjectListener(DeserializeFeature feature) {
        this.feature = feature;
    }

    @Override
    public void enterJsonArray(JSONParser.JsonArrayContext ctx) {
        throw new JsonParseException("need JsonObject but meet JsonArray!"); 

        //现在是JsonObject模式({}),如果是JsonArray([]),则抛出异常。
    }

    public JsonObject getJsonObject() {
        return jsonObject;
    }

    @Override
    public void exitJsonObject(JSONParser.JsonObjectContext ctx) {
        this.jsonObject = jsonObjectParseTreeProperty.get(ctx.object());
    }

    @Override
    public void enterObject(JSONParser.ObjectContext ctx) {
        jsonObjectParseTreeProperty.put(ctx,new JsonObject()); //设置当前正在解析的JsonObject
    }

    @Override
    public void enterArray(JSONParser.ArrayContext ctx) {
        jsonArrayParseTreeProperty.put(ctx,new JsonArray());  //设置当前正在解析的JsonArray
    }

    @Override
    public void exitPair(JSONParser.PairContext ctx) {
        JsonObject jsonObject = jsonObjectParseTreeProperty.get(ctx.getParent()); //取出当前正在解析的JsonObject

        String name = StringUtil.parseJson(ctx.STRING().getText(),feature);
        JSONParser.ValueContext valueContext = ctx.value();
        Object value = property.get(valueContext);    //取出由exitNumber、exitBoolean等方法获得的值

        if(value!=null||feature.readNullValue()){
            jsonObject.put(name,value);
        }
    }

    //将这些基本值放到property中,在pair节点中取出

    @Override
    public void exitString(JSONParser.StringContext ctx) {
        property.put(ctx, StringUtil.parseJson(ctx.STRING().getText(),feature));
    }

    @Override
    public void exitNUMBER(JSONParser.NUMBERContext ctx) {
        String numberString = ctx.NUMBER().getText();
        property.put(ctx, feature.numberCaster().toNumber(numberString));
    }

    @Override
    public void exitArray(JSONParser.ArrayContext ctx) {
        JsonArray jsonArray = jsonArrayParseTreeProperty.get(ctx);
        List<JSONParser.ValueContext> valueContexts = ctx.value();
        for(JSONParser.ValueContext valueContext:valueContexts){
            jsonArray.add(property.get(valueContext));
        }
    }

    @Override
    public void exitObjectValue(JSONParser.ObjectValueContext ctx) {
        JsonObject jsonObject = jsonObjectParseTreeProperty.get(ctx.object());
        property.put(ctx,jsonObject);
    }

    @Override
    public void exitArrayValue(JSONParser.ArrayValueContext ctx) {
        JsonArray jsonArray = jsonArrayParseTreeProperty.get(ctx.array());
        property.put(ctx,jsonArray);
    }

    @Override
    public void exitBOOLEANTRUE(JSONParser.BOOLEANTRUEContext ctx) {
        property.put(ctx,true);
    }

    @Override
    public void exitBOOLEANFALSE(JSONParser.BOOLEANFALSEContext ctx) {
        property.put(ctx,false);
    }

    @Override
    public void exitNULL(JSONParser.NULLContext ctx) {
        property.put(ctx,null);
    }
}
⚠️ **GitHub.com Fallback** ⚠️