How tencent/apijson works
Project type and purpose
• This is a back-end Java library (no UI) that implements a JSON‐protocol API → SQL translation engine.
• It parses incoming JSON requests (CRUD, joins, nested queries), auto-generates SQL, executes via JDBC, and returns JSON.
File structure
• Top-level “APIJSONORM” Maven module with pom.xml → core Java library.
• src/main/java/apijson contains protocol classes: JSON, JSONParser, JSONRequest, JSONResponse, Log, StringUtil, etc.
• src/main/java/apijson/orm contains the heart of the SQL engine: AbstractParser hierarchy, ParserCreator, FunctionParser, ObjectParser, SQLConfig, SQLCreator, SQLExecutor, Verifier, VerifierCreator, Exception classes, plus model and script subpackages.
• src/main/resources/application.properties for default config.
Extract key internal components
a. HTTP/Framework Launcher (provided by user’s SpringBoot or JFinal integration – not in this module): receives HTTP JSON payload.
b. JSON Layer
– JSONRequest: wraps raw JSON → internal Java objects
– JSONParser / ParserCreator: picks the right parser (FunctionParser vs. ObjectParser)
c. Parser Layer
– AbstractParser → FunctionParser / ObjectParser → builds an internal representation of operations (Entry objects, Pair, Join, Logic).
– Verifier (from VerifierCreator): enforces schema/permission rules.
d. SQL Layer
– SQLConfig: contains DB metadata, table/column mappings.
– SQLCreator: constructs SQL strings from parsed representations.
– AbstractSQLExecutor / SQLExecutor: executes SQL via JDBC, returns raw ResultSets.
e. Response Layer
– JSONCreator / JSONResponse: transforms ResultSets back into JSONList/JSONMap structures.
f. Extension Points
– ScriptExecutor (JavaScriptExecutor, JSR223ScriptExecutor) for remote functions.
– OnParseCallback hooks, custom VerifierCreator, custom SQLExecutorCreator.
Relationships and data flow
1. HTTP Request → JSONRequest
2. JSONRequest → ParserCreator → JSONParser / AbstractParser
3. AbstractParser → Verifier → (passes or errors)
4. AbstractParser → SQLConfig + SQLCreator → SQL string
5. SQL string → SQLExecutor → JDBC → Database
6. ResultSet → JSONCreator → JSONResponse
7. JSONResponse → HTTP Response
Architectural patterns & principles
• Factory/Creator (ParserCreator, VerifierCreator, SQLExecutorCreator)
• Template Method (AbstractParser, AbstractSQLExecutor)
• Strategy (multiple Parser and Verifier implementations)
• Visitor (Visitor interface for walking parse trees)
• Layered separation (Protocol, Parse, SQL, Execute, Response)