@@ -16,31 +16,42 @@ implementation
1616 lptypes,
1717 simba.json;
1818
19- (*
20- JSON
21- ====
22- JSON parser.
23- *)
24-
2519type
2620 PJSONItemType = ^EJSONItemType;
2721 PJSONFormatOptions = ^EJSONFormatOptions;
2822
29- procedure _LapeJSONParser_Parse (const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
30- begin
31- PLapeObjectJSON(Params^[0 ])^^.Parse(PString(Params^[1 ])^);
32- end ;
23+ (*
24+ JSON
25+ ====
26+ JSON parsing.
27+ *)
3328
34- procedure _LapeJSONParser_Load (const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
29+ (*
30+ TJSONParser.Construct
31+ ---------------------
32+ ```
33+ function TJSONArray.Construct: TJSONArray; static;
34+ function TJSONObject.Construct: TJSONObject; static;
35+ function TJSONParser.Construct: TJSONParser; static;
36+ ```
37+ Constructors for JSON. Use the `new` keyword for these.
38+
39+ ```
40+ var
41+ parser: TJSONParser;
3542begin
36- PLapeObjectJSON(Params^[ 0 ])^^.Load(PString(Params^[ 1 ])^ );
37- end ;
43+ parser := new TJSONParser( );
44+ parser.Parse('{"someNumber": 123, someString: "hello"}') ;
3845
39- procedure _LapeJSONParser_Save (const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
40- begin
41- PLapeObjectJSON(Params^[0 ])^^.Save(PString(Params^[1 ])^, PJSONFormatOptions(Params^[2 ])^, PInteger(Params^[3 ])^);
46+ WriteLn parser.Typ;
47+ WriteLn parser.Count;
48+ WriteLn parser.Item[0].Typ;
49+ WriteLn parser.Item[0].AsInt;
50+ WriteLn parser.Item['someString'].Typ;
51+ WriteLn parser.Item['someString'].AsString;
4252end;
43-
53+ ```
54+ *)
4455procedure _LapeJSONArray_Construct (const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
4556begin
4657 PLapeObjectJSON(Result)^^ := NewJSONArray();
@@ -62,6 +73,63 @@ procedure _LapeJSONItem_Destroy(const Params: PParamArray); LAPE_WRAPPER_CALLING
6273 PLapeObjectJSON(Params^[0 ])^^.Free();
6374end ;
6475
76+ (*
77+ TJSONParser.Parse
78+ -----------------
79+ ```
80+ procedure TJSONParser.Parse(Str: String);
81+ ```
82+ Parse a json string.
83+ *)
84+ procedure _LapeJSONParser_Parse (const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
85+ begin
86+ PLapeObjectJSON(Params^[0 ])^^.Parse(PString(Params^[1 ])^);
87+ end ;
88+
89+ (*
90+ TJSONParser.Load
91+ ----------------
92+ ```
93+ procedure TJSONParser.Load(FileName: String);
94+ ```
95+ Parse json from a file.
96+ *)
97+ procedure _LapeJSONParser_Load (const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
98+ begin
99+ PLapeObjectJSON(Params^[0 ])^^.Load(PString(Params^[1 ])^);
100+ end ;
101+
102+ (*
103+ TJSONParser.Save
104+ ----------------
105+ ```
106+ procedure TJSONParser.Save(FileName: String; Options: EJSONFormatOptions = []; Indent: Integer = 2);
107+ ```
108+ Formats the json parser to a string and saves to file.
109+ *)
110+ procedure _LapeJSONParser_Save (const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
111+ begin
112+ PLapeObjectJSON(Params^[0 ])^^.Save(PString(Params^[1 ])^, PJSONFormatOptions(Params^[2 ])^, PInteger(Params^[3 ])^);
113+ end ;
114+
115+ (*
116+ TJSONItem.Typ
117+ -------------
118+ ```
119+ property TJSONItem.Typ: EJSONType
120+ ```
121+ Returns json type of the item.
122+
123+ This can be any of:
124+ - `EJSONType.UNKNOWN`
125+ - `EJSONType.INT`
126+ - `EJSONType.FLOAT`
127+ - `EJSONType.STR`
128+ - `EJSONType.BOOL`
129+ - `EJSONType.NULL`
130+ - `EJSONType.ARR`
131+ - `EJSONType.OBJ`
132+ *)
65133procedure _LapeJSONItem_ItemType_Read (const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
66134begin
67135 PJSONItemType(Result)^ := PLapeObjectJSON(Params^[0 ])^^.Typ;
@@ -265,6 +333,21 @@ procedure _LapeJSONItem_Clear(const Params: PParamArray; const Result: Pointer);
265333 PLapeObjectJSON(Params^[0 ])^^.Clear();
266334end ;
267335
336+ (*
337+ TJSONItem.Format
338+ ----------------
339+ ```
340+ function TJSONItem.Format(Options: EJSONFormatOptions = []; Indent: Integer = 2): String;
341+ ```
342+ Formats the json item into a JSON string.
343+
344+ Options can be a set of:
345+ - `EJSONFormatOption.SINGLE_LINE_ARR`: Array without CR/LF : all on one line
346+ - `EJSONFormatOption.SINGLE_LINE_OBJ`: Object without CR/LF : all on one line
347+ - `EJSONFormatOption.NO_QUOTE_MEMBERS`: Do not quote object member names.
348+ - `EJSONFormatOption.USE_TABS`: Use tab characters instead of spaces.
349+ - `EJSONFormatOption.NO_WHITESPACE`: Do not use whitespace at all
350+ *)
268351procedure _LapeJSONItem_Format (const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
269352begin
270353 PString(Result)^ := PLapeObjectJSON(Params^[0 ])^^.Format(PJSONFormatOptions(Params^[1 ])^, PInteger(Params^[2 ])^);
@@ -284,15 +367,15 @@ procedure ImportJSON(Script: TSimbaScript);
284367 addGlobalType(' enum(SINGLE_LINE_ARR, SINGLE_LINE_OBJ, NO_QUOTE_MEMBERS, USE_TABS, NO_WHITESPACE)' , ' EJSONFormatOption' );
285368 addGlobalType(' enum(UNKNOWN, INT, FLOAT, STR, BOOL, NULL, ARR, OBJ)' , ' EJSONType' );
286369 addGlobalType(' set of EJSONFormatOption' , ' EJSONFormatOptions' );
287- addGlobalFunc(' procedure TJSONParser.Load(FileName: String);' , @_LapeJSONParser_Load);
288- addGlobalFunc(' procedure TJSONParser.Parse(Str: String);' , @_LapeJSONParser_Parse);
289- addGlobalFunc(' procedure TJSONParser.Save(FileName: String; Options: EJSONFormatOptions = []; Indent: Integer = 2);' , @_LapeJSONParser_Save);
290-
291370 addGlobalFunc(' function TJSONArray.Construct: TJSONArray; static;' , @_LapeJSONArray_Construct);
292371 addGlobalFunc(' function TJSONObject.Construct: TJSONObject; static;' , @_LapeJSONObject_Construct);
293372 addGlobalFunc(' function TJSONParser.Construct: TJSONParser; static;' , @_LapeJSONParser_Construct);
294373 addGlobalFunc(' procedure TJSONItem.Destroy;' , @_LapeJSONItem_Destroy);
295374
375+ addGlobalFunc(' procedure TJSONParser.Load(FileName: String);' , @_LapeJSONParser_Load);
376+ addGlobalFunc(' procedure TJSONParser.Parse(Str: String);' , @_LapeJSONParser_Parse);
377+ addGlobalFunc(' procedure TJSONParser.Save(FileName: String; Options: EJSONFormatOptions = []; Indent: Integer = 2);' , @_LapeJSONParser_Save);
378+
296379 addProperty(' TJSONItem' , ' Typ' , ' EJSONType' , @_LapeJSONItem_ItemType_Read);
297380 addProperty(' TJSONItem' , ' AsInt' , ' Int64' , @_LapeJSONItem_AsInt_Read, @_LapeJSONItem_AsInt_Write);
298381 addProperty(' TJSONItem' , ' AsString' , ' String' , @_LapeJSONItem_AsString_Read, @_LapeJSONItem_AsString_Write);
0 commit comments