문법
json.format(json: string, encodeOptions?: EncodeOptions, decodeOptions?: DecodeOptions): string인수
| 이름 | 타입 | 설명 |
|---|---|---|
json | string | 구문 분석, 형식화, 축소 또는 검증을 위한 JSON 문자열입니다. |
encodeOptions? | EncodeOptions | 예쁜 출력이나 키 순서와 같은 선택적 인코딩 설정. |
decodeOptions? | DecodeOptions | JSON을 읽는 동안 사용되는 선택적 구문 분석 설정입니다. |
반환값
| 이름 | 타입 | 설명 |
|---|---|---|
formatted | string | 형식화된 JSON 문자열입니다. |
설명
JSON을 구문 분석하고 다시 인코딩하여 형식을 표준화합니다.
3개 파라미터로 호출합니다: json, encodeOptions, decodeOptions. 인수 표는 필수 값과 동작을 조정하는 값을 설명합니다.
formatted (string)을 반환합니다. 반환 표를 사용해 성공 값, nil 결과, 복구 가능한 오류를 구분하세요.
예제
기존 JSON을 읽을 수 있는 표현으로 정규화합니다.
local compact = '{"b":2,"a":1}'
local formatted = json.format(compact, {
pretty = true,
sortKeys = true,
})
print(formatted)타입
EncodeOptions
JSON 직렬화 및 형식화를 제어합니다.
pretty? boolean 형식이 지정된 여러 줄 JSON 출력을 활성화합니다.sortKeys? boolean 인코딩하기 전에 객체 키를 정렬합니다.emptyTableAsArray? boolean 빈 테이블을 개체 대신 배열로 인코딩합니다.errorOnUnsupported? boolean 지원되지 않는 값을 대체하는 대신 오류가 발생합니다.encodeInvalidNumbersAsNull? boolean NaN 및 무한 숫자를 JSON null로 인코딩합니다.maxDepth? integer 1부터 4096까지 고정된 최대 중첩 깊이를 설정합니다.indent? string 32자로 제한되는 들여쓰기 문자열을 설정합니다.DecodeOptions
JSON 구문 분석 동작을 제어합니다.
useNull? boolean nil 대신 라이브러리의 null 센티널을 사용하여 JSON null을 나타냅니다.maxDepth? integer 1부터 4096까지 고정된 최대 중첩 깊이를 설정합니다.