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 DemoFunctionParser

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

Overview

Concrete tool for defining and executing server-side remote functions callable from APIJSON requests, provided by the APIJSON framework.

Description

The DemoFunctionParser class extends APIJSONFunctionParser<Long> and provides the business logic functions that clients can invoke via the key() syntax in JSON requests. Functions are registered in the database Method table and resolved by the framework at request time.

Key functions provided:

  • getCurrentUser(curObj): Returns the authenticated Visitor<Long> from the session.
  • getCurrentUserId(curObj): Returns the current user's ID as a Long.
  • getCurrentUserIdAsList(curObj): Returns user ID wrapped in a list.
  • getCurrentContactIdList(curObj): Returns the current user's contact ID list.
  • verifyIdList(curObj, idList): Validates that an argument is an array of numeric IDs.
  • verifyURLList(curObj, urlList): Validates that an argument is an array of valid URLs.
  • deleteCommentOfMoment(curObj, momentId): Deletes all comments for a given moment via an internal APIJSON DELETE request.
  • deleteChildComment(curObj, toId): Recursively deletes child comments.
  • verifyAccess(curObj): Verifies the current user matches the OWNER role.
  • verifyGroupUrlLike(curObj, urlLike): Validates a group URL pattern.
  • getMethodDefinition/getMethodArguments: Returns method signature definitions for API documentation.

The class also has a (currently commented out) static block for registering script executors via SCRIPT_EXECUTOR_MAP.put().

Usage

  • From client JSON: Use "key()":"functionName(args)" in the request body to invoke a function.
  • From Java code: Instantiate DemoFunctionParser and call methods directly (for internal use).
  • Registering new functions: Add a public method with @NotNull JSONObject curObj as the first parameter, then insert a record in the Method database table.

Code Reference

Source Location

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

Signature

public class DemoFunctionParser extends APIJSONFunctionParser<Long> {

    public DemoFunctionParser();
    public DemoFunctionParser(RequestMethod method, String tag, int version,
                               JSONObject request, HttpSession session);

    // User context functions
    public Visitor<Long> getCurrentUser(@NotNull JSONObject curObj);
    public Long getCurrentUserId(@NotNull JSONObject curObj);
    public List<Long> getCurrentUserIdAsList(@NotNull JSONObject curObj);
    public List<Long> getCurrentContactIdList(@NotNull JSONObject curObj);

    // Validation functions
    public void verifyIdList(@NotNull JSONObject curObj, @NotNull String idList) throws Exception;
    public void verifyURLList(@NotNull JSONObject curObj, @NotNull String urlList) throws Exception;
    public Object verifyAccess(@NotNull JSONObject curObj) throws Exception;
    public void verifyGroupUrlLike(@NotNull JSONObject curObj, String urlLike) throws Exception;

    // Business logic functions
    public int deleteCommentOfMoment(@NotNull JSONObject curObj, @NotNull String momentId) throws Exception;
    public int deleteChildComment(@NotNull JSONObject curObj, @NotNull String toId) throws Exception;

    // Method introspection
    public String getMethodDefinition(@NotNull JSONObject curObj) throws IllegalArgumentException;
    public String getMethodArguments(@NotNull JSONObject curObj) throws Exception;
    public String getMethodRequest(@NotNull JSONObject curObj);

    // Utility
    public JSONArray getIdList(@NotNull JSONObject curObj);
    public static String removeComment(String json);
}

Import

import apijson.demo.DemoFunctionParser;

I/O Contract

Inputs

Name Type Required Description
curObj JSONObject Yes Current JSON object in the request being processed
idList String Yes (verifyIdList) Argument key name whose value is the ID list to validate
urlList String Yes (verifyURLList) Argument key name whose value is the URL list to validate
momentId String Yes (deleteCommentOfMoment) Argument key for moment ID

Outputs

Name Type Description
getCurrentUser Visitor<Long> Current authenticated user
getCurrentUserId Long Current user's ID
verifyIdList void Throws IllegalArgumentException on invalid input
deleteCommentOfMoment int Number of comments deleted
getMethodDefinition String Method signature string

Usage Examples

Invoking from JSON Request

{
    "User": {
        "id@": "getCurrentUserId()",
        "Comment[]": {
            "Comment": {
                "userId@": "User/id"
            }
        }
    }
}

Invoking Validation Function

{
    "Moment": {
        "id": 123,
        "praiseUserIdList-()": "verifyIdList(praiseUserIdList)"
    }
}

Adding a New Remote Function

public class DemoFunctionParser extends APIJSONFunctionParser<Long> {
    // New function accessible via "key()":"calculateTotal(price, quantity)"
    public Double calculateTotal(@NotNull JSONObject curObj, String price, String qty) {
        Double p = getArgDecimal(price);
        Double q = getArgDecimal(qty);
        return p * q;
    }
}
// Then insert into Method table:
// INSERT INTO Method(package, method, detail, userId)
// VALUES('apijson.demo', 'calculateTotal', 'Calculate total price', 82001);

Related Pages

Implements Principle

Requires Environment

Page Connections

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