1
0
higale 1 жил өмнө
parent
commit
e65ba8289f
3 өөрчлөгдсөн 392 нэмэгдсэн , 40 устгасан
  1. 42 40
      README.md
  2. 175 0
      README_zh_CN.md
  3. 175 0
      README_zh_TW.md

+ 42 - 40
README.md

@@ -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',

+ 175 - 0
README_zh_CN.md

@@ -0,0 +1,175 @@
+# TRJ - JSON 简单读写
+- v0.9.1
+- 2024-09-05 by gale
+- https://github.com/higale/RJSON
+
+**语言版本: [English](README.md) | [简体中文](README_zh_CN.md) | [繁體中文](README_zh_TW.md)**
+
+## 属性:
+- `Items[Path]` 基于路径的读写,适用于对象和数组。
+ 
+        a['x.y[2].z'] := 5;
+        b['[3].ok'] := false;
+
+- `Items[Index]` 数组读写。
+
+        a[3][1] := 'hello';
+
+- `Pairs[Index]` 获取 JSONObject 下的键值对。
+
+        for var i := 0 to RJ.Count do
+        begin
+            Memo1.Lines.Add(RJ.Pairs[i].Key + '=' + RJ.Pairs[i].Format(0));
+        end;
+
+- `Count` 对象或数组中的条目数量,其他类型返回 0。
+- `Index` 数组中的条目索引,如果不是数组数据则返回 -1。
+- `Key` 如果是键值对数据,则返回键,否则返回空字符串。
+- `Root` 根数据接口。
+- `Path` 值的路径。
+- `JSONValue` 包含的 `TJSONValue`。
+
+## 方法
+- `ToStr` 转换为字符串,默认为空字符串。
+
+        var str: string;
+
+        str := RJ['title'];
+        str := RJ['title'].ToStr;
+        str := RJ['title'].ToStr('没有标题');
+
+- `ToInt` 转换为整数,默认为 0。
+
+        var i: integer;
+
+        i := RJ['num'];
+        i := RJ['num'].ToInt;
+        i := RJ['num'].ToInt(-1);
+
+- `ToInt64` 转换为 64 位整数,默认为 0。
+
+        var i64: Int64;
+
+        i64 := RJ['num64'];
+        i64 := RJ['num64'].ToInt64;
+        i64 := RJ['num64'].ToInt64(-1);
+
+- `ToFloat` 转换为浮点数(使用 Extended),默认为 0.0。
+
+        var f: Extended;
+
+        f := RJ['num'];
+        f := RJ['num'].ToFloat;
+        f := RJ['num'].ToFloat(100.0);
+
+- `ToBool` 转换为布尔值,默认为 False。
+
+        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 字符串。
+
+        str1 := RJ.Format(2); // 缩进 2 个空格(默认为 4)
+        str2 := RJ.Format(0); // 压缩格式,无缩进无换行
+
+- `ParseJSONValue` 从字符串加载数据。
+
+        RJ.ParseJSONValue('{"a":1}');
+
+- `LoadFromFile` 从文件加载数据。
+
+        procedure LoadFromFile(
+            const AFileName: string;   // JSON 文件名
+            AUseBool: boolean = False; // 遇到 JSON 数据中的 true 或 false 时,是否创建 TJSONBool 类型的值
+            ARaiseExc: boolean = False // 遇到无效的 JSON 数据时是否抛出异常
+        );
+
+- `SaveToFile` 保存到文件。
+
+        procedure SaveToFile(
+            const AFileName: string;            // 文件名
+            AIndentation: Integer = 4;          // 缩进空格数,0: 不缩进不换行
+            AWriteBOM: boolean = False;         // 文件是否添加 BOM 标记
+            ATrailingLineBreak: boolean = False // 是否在结尾添加一个空行
+        );
+
+## 示例:
+    procedure TFormMain.btnTestClick(Sender: TObject);
+    var
+      RJ, RJ1: TRJ;
+      fTemp: Extended;
+    begin
+      RJ['title'] := 'hello world! 你好,世界!';
+      RJ['a.num'] := 1;
+      RJ['a.hah'] := false;
+      RJ['b[2]'] := 505;
+      RJ['b[0]'] := 'first';
+      RJ['good'] := True;
+      RJ1 := RJ['c'];
+      RJ1['c1'] := 1.1;
+      RJ1['c2[2]'] := 2.33;
+      with RJ['x'] do
+      begin
+        items[1] := 100;
+        items[2] := '202';
+      end;
+      with RJ['y'] do
+      begin
+        items['ya'] := 'y1';
+        items['yb'] := -2;
+      end;
+      Memo1.Text := RJ.Format;
+      Memo1.Lines.Add('-----------------------------------------------------------');
+      fTemp := RJ['c.c2[3]'];
+      Memo1.Lines.Add('fTemp:' + fTemp.ToString);
+      fTemp := RJ['c.c3[3]'].ToFloat(-100);
+      Memo1.Lines.Add('fTemp:' + fTemp.ToString);
+      Memo1.Lines.Add(RJ['a.num'].ToStr('a.num not exist'));
+      Memo1.Lines.Add(RJ['none'].ToFloat(-1).ToString);
+      Memo1.Lines.Add(RJ['none.a3'].ToStr('none.a3 not exist'));
+      RJ.SaveToFile('test.json', 0);
+    end;
+
+    procedure TFormMain.btnOpenClick(Sender: TObject);
+    var
+      RJ: TRJ;
+      strTmp: string;
+    begin
+      RJ.LoadFromFile('test.json');
+      Memo1.Text := RJ.Format(8);
+      Memo1.Lines.Add('-----------------------ROOT--------------------------');
+      for var item in RJ 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[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]--------------------------');
+      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]--------------------------');
+      for var i := 0 to RJ['c'].Count - 1 do
+      begin
+        strTmp := Format('Index: %d  Key: %s  Value: %s',
+          [RJ['c'].Pairs[i].Index, RJ['c'].Pairs[i].Key, RJ['c'].Pairs[i].Format(0)]);
+        Memo1.Lines.Add(strTmp);
+      end;
+    end;

+ 175 - 0
README_zh_TW.md

@@ -0,0 +1,175 @@
+# TRJ - JSON 簡單讀取與寫入
+- v0.9.1
+- 2024-09-05 by gale
+- https://github.com/higale/RJSON
+
+**語言版本: [English](README.md) | [简体中文](README_zh_CN.md) | [繁體中文](README_zh_TW.md)**
+
+## 屬性:
+- `Items[Path]` 基於路徑的讀寫,適用於物件和陣列。
+
+        a['x.y[2].z'] := 5;
+        b['[3].ok'] := false;
+
+- `Items[Index]` 陣列讀寫。
+
+        a[3][1] := 'hello';
+
+- `Pairs[Index]` 獲取 JSONObject 下的鍵值對。
+
+        for var i := 0 to RJ.Count do
+        begin
+            Memo1.Lines.Add(RJ.Pairs[i].Key + '=' + RJ.Pairs[i].Format(0));
+        end;
+
+- `Count` 物件或陣列中的條目數量,其他類型返回 0。
+- `Index` 陣列中的條目索引,如果不是陣列數據則返回 -1。
+- `Key` 如果是鍵值對數據,則返回鍵,否則返回空字符串。
+- `Root` 根數據接口。
+- `Path` 值的路徑。
+- `JSONValue` 包含的 `TJSONValue`。
+
+## 方法
+- `ToStr` 轉換為字符串,默認為空字符串。
+
+        var str: string;
+
+        str := RJ['title'];
+        str := RJ['title'].ToStr;
+        str := RJ['title'].ToStr('沒有標題');
+
+- `ToInt` 轉換為整數,默認為 0。
+
+        var i: integer;
+
+        i := RJ['num'];
+        i := RJ['num'].ToInt;
+        i := RJ['num'].ToInt(-1);
+
+- `ToInt64` 轉換為 64 位整數,默認為 0。
+
+        var i64: Int64;
+
+        i64 := RJ['num64'];
+        i64 := RJ['num64'].ToInt64;
+        i64 := RJ['num64'].ToInt64(-1);
+
+- `ToFloat` 轉換為浮點數(使用 Extended),默認為 0.0。
+
+        var f: Extended;
+
+        f := RJ['num'];
+        f := RJ['num'].ToFloat;
+        f := RJ['num'].ToFloat(100.0);
+
+- `ToBool` 轉換為布林值,默認為 False。
+
+        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 字符串。
+
+        str1 := RJ.Format(2); // 縮進 2 個空格(默認為 4)
+        str2 := RJ.Format(0); // 壓縮格式,無縮進無換行
+
+- `ParseJSONValue` 從字符串加載數據。
+
+        RJ.ParseJSONValue('{"a":1}');
+
+- `LoadFromFile` 從文件加載數據。
+
+        procedure LoadFromFile(
+            const AFileName: string;   // JSON 文件名
+            AUseBool: boolean = False; // 遇到 JSON 數據中的 true 或 false 時,是否創建 TJSONBool 類型的值
+            ARaiseExc: boolean = False // 遇到無效的 JSON 數據時是否拋出異常
+        );
+
+- `SaveToFile` 保存到文件。
+
+        procedure SaveToFile(
+            const AFileName: string;            // 文件名
+            AIndentation: Integer = 4;          // 縮進空格數,0: 不縮進不換行
+            AWriteBOM: boolean = False;         // 文件是否添加 BOM 標記
+            ATrailingLineBreak: boolean = False // 是否在結尾添加一個空行
+        );
+
+## 範例:
+    procedure TFormMain.btnTestClick(Sender: TObject);
+    var
+      RJ, RJ1: TRJ;
+      fTemp: Extended;
+    begin
+      RJ['title'] := 'hello world! 你好,世界!';
+      RJ['a.num'] := 1;
+      RJ['a.hah'] := false;
+      RJ['b[2]'] := 505;
+      RJ['b[0]'] := 'first';
+      RJ['good'] := True;
+      RJ1 := RJ['c'];
+      RJ1['c1'] := 1.1;
+      RJ1['c2[2]'] := 2.33;
+      with RJ['x'] do
+      begin
+        items[1] := 100;
+        items[2] := '202';
+      end;
+      with RJ['y'] do
+      begin
+        items['ya'] := 'y1';
+        items['yb'] := -2;
+      end;
+      Memo1.Text := RJ.Format;
+      Memo1.Lines.Add('-----------------------------------------------------------');
+      fTemp := RJ['c.c2[3]'];
+      Memo1.Lines.Add('fTemp:' + fTemp.ToString);
+      fTemp := RJ['c.c3[3]'].ToFloat(-100);
+      Memo1.Lines.Add('fTemp:' + fTemp.ToString);
+      Memo1.Lines.Add(RJ['a.num'].ToStr('a.num not exist'));
+      Memo1.Lines.Add(RJ['none'].ToFloat(-1).ToString);
+      Memo1.Lines.Add(RJ['none.a3'].ToStr('none.a3 not exist'));
+      RJ.SaveToFile('test.json', 0);
+    end;
+
+    procedure TFormMain.btnOpenClick(Sender: TObject);
+    var
+      RJ: TRJ;
+      strTmp: string;
+    begin
+      RJ.LoadFromFile('test.json');
+      Memo1.Text := RJ.Format(8);
+      Memo1.Lines.Add('-----------------------ROOT--------------------------');
+      for var item in RJ 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[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]--------------------------');
+      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]--------------------------');
+      for var i := 0 to RJ['c'].Count - 1 do
+      begin
+        strTmp := Format('Index: %d  Key: %s  Value: %s',
+          [RJ['c'].Pairs[i].Index, RJ['c'].Pairs[i].Key, RJ['c'].Pairs[i].Format(0)]);
+        Memo1.Lines.Add(strTmp);
+      end;
+    end;