Search in sources :

Example 1 with JsonMapExpression

use of org.beetl.core.statement.JsonMapExpression in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseJson.

protected Expression parseJson(JsonContext ctx) {
    if (ctx.LEFT_SQBR() != null) {
        // array
        JsonArrayExpression json = null;
        List<ExpressionContext> listCtx = ctx.expression();
        if (listCtx.size() == 0) {
            json = new JsonArrayExpression(Collections.EMPTY_LIST, this.getBTToken(ctx.LEFT_SQBR().getSymbol()));
        } else {
            List<Expression> list = new ArrayList<Expression>(listCtx.size());
            for (ExpressionContext expCtx : listCtx) {
                list.add(this.parseExpress(expCtx));
            }
            json = new JsonArrayExpression(list, this.getBTToken(ctx.LEFT_SQBR().getSymbol()));
        }
        return json;
    } else {
        // map
        JsonMapExpression json = null;
        List<JsonKeyValueContext> listCtx = ctx.jsonKeyValue();
        if (listCtx.size() == 0) {
            json = new JsonMapExpression(Collections.EMPTY_MAP, this.getBTToken(ctx.LEFT_BRACE().getSymbol()));
        } else {
            Map<String, Expression> map = new LinkedHashMap<String, Expression>(listCtx.size());
            for (JsonKeyValueContext kvCtx : listCtx) {
                String key = null;
                if (kvCtx.StringLiteral() != null) {
                    key = this.getStringValue(kvCtx.StringLiteral().getText());
                } else {
                    key = kvCtx.Identifier().getSymbol().getText();
                }
                Expression exp = this.parseExpress(kvCtx.expression());
                map.put(key, exp);
            }
            json = new JsonMapExpression(map, this.getBTToken(ctx.LEFT_BRACE().getSymbol()));
        }
        return json;
    }
}
Also used : StatementExpressionContext(org.beetl.core.parser.BeetlParser.StatementExpressionContext) ExpressionContext(org.beetl.core.parser.BeetlParser.ExpressionContext) ParExpressionContext(org.beetl.core.parser.BeetlParser.ParExpressionContext) ContentBodyExpression(org.beetl.core.statement.ContentBodyExpression) ArthExpression(org.beetl.core.statement.ArthExpression) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) CompareExpression(org.beetl.core.statement.CompareExpression) FunctionExpression(org.beetl.core.statement.FunctionExpression) IncDecExpression(org.beetl.core.statement.IncDecExpression) Expression(org.beetl.core.statement.Expression) AndExpression(org.beetl.core.statement.AndExpression) StatementExpression(org.beetl.core.statement.StatementExpression) NativeCallExpression(org.beetl.core.statement.NativeCallExpression) NegExpression(org.beetl.core.statement.NegExpression) FormatExpression(org.beetl.core.statement.FormatExpression) TernaryExpression(org.beetl.core.statement.TernaryExpression) OrExpression(org.beetl.core.statement.OrExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) NotBooleanExpression(org.beetl.core.statement.NotBooleanExpression) JsonKeyValueContext(org.beetl.core.parser.BeetlParser.JsonKeyValueContext) ArrayList(java.util.ArrayList) JsonMapExpression(org.beetl.core.statement.JsonMapExpression) JsonArrayExpression(org.beetl.core.statement.JsonArrayExpression) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 ExpressionContext (org.beetl.core.parser.BeetlParser.ExpressionContext)1 JsonKeyValueContext (org.beetl.core.parser.BeetlParser.JsonKeyValueContext)1 ParExpressionContext (org.beetl.core.parser.BeetlParser.ParExpressionContext)1 StatementExpressionContext (org.beetl.core.parser.BeetlParser.StatementExpressionContext)1 AndExpression (org.beetl.core.statement.AndExpression)1 ArthExpression (org.beetl.core.statement.ArthExpression)1 CompareExpression (org.beetl.core.statement.CompareExpression)1 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)1 Expression (org.beetl.core.statement.Expression)1 FormatExpression (org.beetl.core.statement.FormatExpression)1 FunctionExpression (org.beetl.core.statement.FunctionExpression)1 IncDecExpression (org.beetl.core.statement.IncDecExpression)1 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)1 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)1 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)1 NegExpression (org.beetl.core.statement.NegExpression)1 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)1 OrExpression (org.beetl.core.statement.OrExpression)1