Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:APIJSON APIJSON Demo DemoParser

From Leeroopedia
Knowledge Sources
Domains Parsing, API_Design, Security
Last Updated 2026-10-14 00:00 GMT

Overview

Concrete tool for parsing APIJSON requests with configurable query limits, session-based authentication, and database-account sharing provided by the APIJSON framework.

Description

The DemoParser class extends APIJSONParser<Long> and serves as the request parsing component in the APIJSON pipeline. It receives the JSON request from the controller, enforces query/update/object/SQL count limits, creates per-table object parsers, and optionally shares the application user's database credentials with the database connection (for multi-tenant scenarios).

Key features:

  • Query limits: Max 200 rows per query (1000 for internal authenticated calls), max 10 updates, max 500 SQL statements.
  • Internal call override: The KEY_MAP allows internal calls with a session key to bypass the standard 200-row limit.
  • DB account sharing: When AS_DB_ACCOUNT is set in the session, the parser uses the logged-in user's phone and password as database credentials, enabling per-user database authentication.
  • Object parser factory: Creates DemoObjectParser instances for each table in the request.

Usage

  • Standard request parsing: Called by DemoController via super.get/post/put/delete(), which internally calls parseResponse().
  • Internal API calls: Use KEY_MAP.put(key, session) to register a session for elevated query limits, then include the key in the request JSON.
  • Multi-tenant DB auth: Set session.setAttribute(DemoController.AS_DB_ACCOUNT, true) to use user credentials for DB connections.

Code Reference

Source Location

  • Repository: APIJSON-Demo
  • File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoParser.java

Signature

public class DemoParser extends APIJSONParser<Long> {

    public static final Map<String, HttpSession> KEY_MAP;

    public DemoParser();
    public DemoParser(RequestMethod method);
    public DemoParser(RequestMethod method, boolean needVerify);

    // Query limits
    @Override
    public int getMaxQueryCount();    // 200 (default), 1000 (internal)
    @Override
    public int getMaxUpdateCount();   // 10
    @Override
    public int getMaxObjectCount();   // 10
    @Override
    public int getMaxSQLCount();      // 500

    // Request parsing
    @Override
    public JSONObject parseResponse(JSONObject request);
    @Override
    public APIJSONObjectParser<Long> createObjectParser(
        JSONObject request, String parentPath,
        SQLConfig<Long, JSONObject, JSONArray> arrayConfig,
        boolean isSubquery, boolean isTable, boolean isArrayMainTable
    ) throws Exception;

    // DB account sharing (multi-tenant)
    @Override
    public APIJSONParser<Long> setSession(HttpSession session);
    @Override
    public JSONObject executeSQL(SQLConfig<Long, JSONObject, JSONArray> config, boolean isSubquery) throws Exception;
}

Import

import apijson.demo.DemoParser;

I/O Contract

Inputs

Name Type Required Description
request JSONObject Yes Parsed JSON request from controller
method RequestMethod No HTTP method (GET/POST/PUT/DELETE), set via constructor
needVerify boolean No Whether to verify access (default true)
session HttpSession No User session for auth and DB account sharing

Outputs

Name Type Description
parseResponse return JSONObject Response JSON with query results or errors
createObjectParser return APIJSONObjectParser<Long> Per-table parser for processing nested objects

Usage Examples

Standard Parse

// Parse a GET request
DemoParser parser = new DemoParser(RequestMethod.GET);
JSONObject request = JSON.parseObject("{\"User\":{\"id\":82001}}");
JSONObject response = parser.parseResponse(request);
// response contains the User record

Internal Call with Elevated Limits

// Register a session for internal use
HttpSession session = ...;
String key = UUID.randomUUID().toString();
DemoParser.KEY_MAP.put(key, session);

// Include key in request to bypass 200-row limit
JSONObject request = JSON.parseObject("{\"key\":\"" + key + "\",\"User\":{}}");
JSONObject response = new DemoParser().parseResponse(request);
// Can now return up to 1000 rows

// Clean up
DemoParser.KEY_MAP.remove(key);

Disable Verification for Internal Calls

// Skip access verification (e.g., for system-internal queries)
DemoParser parser = new DemoParser(RequestMethod.DELETE).setNeedVerify(false);
JSONObject response = parser.parseResponse(request);

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment