|
@@ -1,42 +1,45 @@
|
|
|
-# TRJ - JSON simple read and write
|
|
|
+# TRJ - JSON Simple Read and Write
|
|
|
- v0.9.1
|
|
|
- 2024-09-05 by gale
|
|
|
- https://github.com/higale/RJSON
|
|
|
|
|
|
+**Languages: [English](README.md) | [简体中文](README_zh_CN.md) | [繁體中文](README_zh_TW.md)**
|
|
|
+
|
|
|
+
|
|
|
## Properties:
|
|
|
-- Items[Path] 路径读写,对Object和Array都有效
|
|
|
+- `Items[Path]` Path-based read/write, works for both Objects and Arrays.
|
|
|
|
|
|
a['x.y[2].z'] := 5;
|
|
|
b['[3].ok'] := false;
|
|
|
|
|
|
-- Items[Index] 数组读写
|
|
|
+- `Items[Index]` Array read/write.
|
|
|
|
|
|
a[3][1] := 'hello';
|
|
|
|
|
|
-- Pairs[Index] 获取JSONObject下的键值对
|
|
|
+- `Pairs[Index]` Retrieving key-value pairs under a JSONObject.
|
|
|
|
|
|
for var i := 0 to RJ.Count do
|
|
|
begin
|
|
|
Memo1.Lines.Add(RJ.Pairs[i].Key + '=' + RJ.Pairs[i].Format(0));
|
|
|
end;
|
|
|
|
|
|
-- Count Object或Array包含条目数,其它类型返回0
|
|
|
-- Index 条目在Array中的索引值,不是数组数条目据返回-1
|
|
|
-- Key 如果是键值对数据,返回键值,否则返回空
|
|
|
-- Root 根数据接口
|
|
|
-- Path 值的路径
|
|
|
-- JSONValue 包含的TJSONValue值
|
|
|
+- `Count` Number of entries in an Object or Array, returns 0 for other types.
|
|
|
+- `Index` Index of the entry in an Array, returns -1 if not an array data.
|
|
|
+- `Key` If it's a key-value pair data, returns the key, otherwise returns an empty string.
|
|
|
+- `Root` Interface to root data.
|
|
|
+- `Path` Path of the value.
|
|
|
+- `JSONValue` Contains the `TJSONValue`.
|
|
|
|
|
|
## Methods
|
|
|
-- ToStr 转换为字符串,缺省为空
|
|
|
+- `ToStr` Converts to a string, defaults to an empty string.
|
|
|
|
|
|
var str: string;
|
|
|
|
|
|
str := RJ['title'];
|
|
|
str := RJ['title'].ToStr;
|
|
|
- str := RJ['title'].ToStr('没有标题');
|
|
|
+ str := RJ['title'].ToStr('No Title');
|
|
|
|
|
|
-- ToInt 转换为整数,缺省为0
|
|
|
+- `ToInt` Converts to an integer, defaults to 0.
|
|
|
|
|
|
var i: integer;
|
|
|
|
|
@@ -44,8 +47,7 @@
|
|
|
i := RJ['num'].ToInt;
|
|
|
i := RJ['num'].ToInt(-1);
|
|
|
|
|
|
-
|
|
|
-- ToInt64 转换为64位整数,缺省为0
|
|
|
+- `ToInt64` Converts to a 64-bit integer, defaults to 0.
|
|
|
|
|
|
var i64: Int64;
|
|
|
|
|
@@ -53,7 +55,7 @@
|
|
|
i64 := RJ['num64'].ToInt64;
|
|
|
i64 := RJ['num64'].ToInt64(-1);
|
|
|
|
|
|
-- ToFloat 转换为浮点数(使用 Extended),缺省为0.0
|
|
|
+- `ToFloat` Converts to a floating-point number (using Extended), defaults to 0.0.
|
|
|
|
|
|
var f: Extended;
|
|
|
|
|
@@ -61,42 +63,42 @@
|
|
|
f := RJ['num'].ToFloat;
|
|
|
f := RJ['num'].ToFloat(100.0);
|
|
|
|
|
|
-- ToBool 转换为 Boolean,缺省为 False
|
|
|
+- `ToBool` Converts to Boolean, defaults to False.
|
|
|
|
|
|
- var b:Boolean;
|
|
|
+ var b: Boolean;
|
|
|
|
|
|
b := RJ['bool'];
|
|
|
b := RJ['bool'].ToBool;
|
|
|
b := RJ['bool'].ToBool(True);
|
|
|
|
|
|
-- RootIs<T: TJSONValue> 根是否是某种类型(TJSONObject、TJSONArray等)
|
|
|
-- ValueIs<T: TJSONValue> 当前值是否是某种类型(TJSONObject、TJSONArray等)
|
|
|
-- CloneJSONValue 克隆一份当前值,如果当前值不存在,则生成 TJSONNull
|
|
|
-- Reset 复位到出厂状态
|
|
|
-- Format 输出格式化好的JSON字符串
|
|
|
+- `RootIs<T: TJSONValue>` Checks if the root is of a certain type (TJSONObject, TJSONArray, etc.).
|
|
|
+- `ValueIs<T: TJSONValue>` Checks if the current value is of a certain type (TJSONObject, TJSONArray, etc.).
|
|
|
+- `CloneJSONValue` Clones the current value, generates TJSONNull if the current value does not exist.
|
|
|
+- `Reset` Resets to factory settings.
|
|
|
+- `Format` Outputs a formatted JSON string.
|
|
|
|
|
|
- str1 := RJ.Format(2); // 缩进2个空格(缺省4个)
|
|
|
- str2 := RJ.Format(0); // 压缩格式,无缩进无换行
|
|
|
+ str1 := RJ.Format(2); // Indented by 2 spaces (default is 4)
|
|
|
+ str2 := RJ.Format(0); // Compressed format, no indentation, no line breaks
|
|
|
|
|
|
-- ParseJSONValue 从字符串加载数据
|
|
|
+- `ParseJSONValue` Loads data from a string.
|
|
|
|
|
|
RJ.ParseJSONValue('{"a":1}');
|
|
|
|
|
|
-- LoadFromFile 从文件加载数据
|
|
|
+- `LoadFromFile` Loads data from a file.
|
|
|
|
|
|
procedure LoadFromFile(
|
|
|
- const AFileName: string; // JSON文件名
|
|
|
- AUseBool: boolean = False; // 遇到JSON数据中的 true 或 false 时,是否创建 TJSONBool 类型的值
|
|
|
- ARaiseExc: boolean = False // 遇到无效的 JSON 数据时是否抛出异常
|
|
|
+ const AFileName: string; // JSON filename
|
|
|
+ AUseBool: boolean = False; // Whether to create TJSONBool type values when encountering true or false in JSON data
|
|
|
+ ARaiseExc: boolean = False // Whether to throw an exception when encountering invalid JSON data
|
|
|
);
|
|
|
|
|
|
-- SaveToFile 保存到文件
|
|
|
+- `SaveToFile` Saves to a file.
|
|
|
|
|
|
- procedure aveToFile(
|
|
|
- const AFileName: string; // 文件名
|
|
|
- AIndentation: Integer = 4; // 缩进空格数,0:不缩进不换行
|
|
|
- AWriteBOM: boolean = False; // 文件是否添加 BOM 标记
|
|
|
- ATrailingLineBreak: boolean = False // 是否在结尾添加一个空行
|
|
|
+ procedure SaveToFile(
|
|
|
+ const AFileName: string; // Filename
|
|
|
+ AIndentation: Integer = 4; // Number of indentation spaces, 0: no indentation, no line breaks
|
|
|
+ AWriteBOM: boolean = False; // Whether to add a BOM marker to the file
|
|
|
+ ATrailingLineBreak: boolean = False // Whether to add an empty line at the end
|
|
|
);
|
|
|
|
|
|
## Example:
|
|
@@ -122,7 +124,7 @@
|
|
|
with RJ['y'] do
|
|
|
begin
|
|
|
items['ya'] := 'y1';
|
|
|
- items['yb'] := -2;;
|
|
|
+ items['yb'] := -2;
|
|
|
end;
|
|
|
Memo1.Text := RJ.Format;
|
|
|
Memo1.Lines.Add('-----------------------------------------------------------');
|
|
@@ -150,21 +152,21 @@
|
|
|
[item.Index, item.Key, item.Format(0)]);
|
|
|
Memo1.Lines.Add(strTmp);
|
|
|
end;
|
|
|
- Memo1.Lines.Add('-----------------------RJ[''a'']--------------------------');
|
|
|
+ Memo1.Lines.Add('-----------------------RJ[a]--------------------------');
|
|
|
for var item in RJ['a'] do
|
|
|
begin
|
|
|
strTmp := Format('Index: %d Key: %s Value: %s',
|
|
|
[item.Index, item.Key, item.Format(0)]);
|
|
|
Memo1.Lines.Add(strTmp);
|
|
|
end;
|
|
|
- Memo1.Lines.Add('-----------------------RJ[''b'']--------------------------');
|
|
|
+ Memo1.Lines.Add('-----------------------RJ[b]--------------------------');
|
|
|
for var item in RJ['b'] do
|
|
|
begin
|
|
|
strTmp := Format('Index: %d Key: %s Value: %s',
|
|
|
[item.Index, item.Key, item.Format(0)]);
|
|
|
Memo1.Lines.Add(strTmp);
|
|
|
end;
|
|
|
- Memo1.Lines.Add('-----------------------RJ[''c'']--------------------------');
|
|
|
+ Memo1.Lines.Add('-----------------------RJ[c]--------------------------');
|
|
|
for var i := 0 to RJ['c'].Count - 1 do
|
|
|
begin
|
|
|
strTmp := Format('Index: %d Key: %s Value: %s',
|