Implementation:APIJSON APIJSON JSONResponse format
| Knowledge Sources | |
|---|---|
| Domains | ORM, Response_Formatting, Key_Conversion |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
Concrete key formatting utility that recursively converts response JSON keys from APIJSON's internal naming convention (with @, :, -, _ markers) to clean variable names suitable for client consumption, provided by the APIJSON framework.
Description
The format' method on JSONResponse recursively walks the response tree and transforms keys:
- formatArrayKey: key[] → keyList; Table:alias[] → aliasList
- formatObjectKey: Table:alias → alias; removes @ prefixes
- 'formatOtherKey: Applies hyphen, underscore, and dollar formatting to scalar value keys
The formatting is controlled by static flags:
- IS_FORMAT_UNDERLINE - Convert underscores to camelCase
- IS_FORMAT_HYPHEN - Convert hyphens to camelCase
- IS_FORMAT_DOLLAR - Convert dollar signs
The method handles nested maps (objects) and lists (arrays) recursively, applying the appropriate format function to each key based on whether its value is a list, map, or scalar.
Usage
Called by AbstractParser.parseResponse() when globalFormat == true (set by the @format directive in the request). Can also be called directly to format any JSON map.
Code Reference
Source Location
- Repository: APIJSON
- File: src/main/java/apijson/JSONResponse.java
Signature
public static <M extends Map<String, Object>, L extends List<Object>> M format(final M object)
public static <M extends Map<String, Object>, L extends List<Object>> L format(final L array)
// Key formatting helpers:
public static String formatArrayKey(String key) // key[] -> keyList
public static String formatObjectKey(String key) // Table:alias -> alias
public static String formatOtherKey(String fullName) // scalar key formatting
Import
import apijson.JSONResponse;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| object | M (Map<String,Object>) | Yes | Response map to format |
| IS_FORMAT_UNDERLINE | boolean | Yes | Convert underscores (static flag) |
| IS_FORMAT_HYPHEN | boolean | Yes | Convert hyphens (static flag) |
| IS_FORMAT_DOLLAR | boolean | Yes | Convert dollar signs (static flag) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | M | New map with formatted keys |
Usage Examples
Key Formatting
// Before format:
// { "User": { "user_id": 82001, "user-name": "Tom", "[]": [...] } }
// After format:
// { "User": { "userId": 82001, "userName": "Tom", "List": [...] } }
// (with IS_FORMAT_UNDERLINE and IS_FORMAT_HYPHEN enabled)
Enabling Format
// In request JSON: { "@format": true, "User": { "id": 1 } }
// parseResponse sets globalFormat = true
// After success, calls JSONResponse.format(response)
// Or directly:
JSONObject formatted = JSONResponse.format(response);