Skip to content

Commit 55eca20

Browse files
committed
Support stringifying simple getter methods for classes
1 parent cdcc279 commit 55eca20

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

Source/dwsJSONScript.pas

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ implementation
6262
// ------------------------------------------------------------------
6363
// ------------------------------------------------------------------
6464

65-
uses dwsCompilerUtils, dwsConstExprs, dwsArrayElementContext;
65+
uses
66+
dwsCompilerUtils, dwsConstExprs, dwsArrayElementContext,
67+
dwsInfo, dwsInfoClasses;
6668

6769
// ------------------
6870
// ------------------ JSONScript ------------------
@@ -319,7 +321,14 @@ class procedure JSONScript.StringifyComposite(exec : TdwsExecution;
319321
fieldSym : TFieldSymbol;
320322
propSym : TPropertySymbol;
321323
locData : IDataContext;
324+
progInfo : TProgramInfo;
325+
info : IInfo;
326+
scriptObj : IScriptObj;
322327
begin
328+
if exec is TdwsProgramExecution then
329+
progInfo := TdwsProgramExecution(exec).ProgramInfo
330+
else progInfo := nil;
331+
323332
writer.BeginObject;
324333
while compSym <> nil do begin
325334
for i:=0 to compSym.Members.Count-1 do begin
@@ -340,9 +349,18 @@ class procedure JSONScript.StringifyComposite(exec : TdwsExecution;
340349
fieldSym:=TFieldSymbol(sym);
341350
dataPtr.CreateOffset(fieldSym.Offset, locData);
342351
StringifySymbol(exec, writer, fieldSym.Typ, locData);
343-
end else begin
344-
// SetLength(bufData, sym.Typ.Size);
345-
Assert(False, 'published method getters not supported yet');
352+
end else if sym is TFuncSymbol then begin
353+
Assert(progInfo <> nil);
354+
if compSym is TRecordSymbol then begin
355+
Assert(False, 'JSON utility does not yet support published method getters for records');
356+
end else begin
357+
scriptObj := (dataPtr.GetSelf as TScriptObjInstance) as IScriptObj;
358+
info := TInfoFunc.Create(progInfo, sym, progInfo.Execution.DataContext_Nil,
359+
nil, scriptObj, TClassSymbol(compSym));
360+
end;
361+
StringifySymbol(exec, writer, sym.Typ, info.Call.GetDataPtr);
362+
info := nil;
363+
scriptObj := nil;
346364
end;
347365
end;
348366
compSym := compSym.Parent;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
type
2+
TSubTest = class
3+
private
4+
FSub : TSubTest;
5+
FName : String;
6+
function GetSub : TSubTest; begin Result := FSub; end;
7+
8+
published
9+
property Name : String read ('<' + FName + '>') write FName;
10+
property Sub : TSubtest read GetSub write FSub;
11+
end;
12+
13+
type
14+
TTest = class
15+
protected
16+
function GetStr : String; begin Result := 'hello'; end;
17+
18+
published
19+
property Str : String read GetStr;
20+
property Int : Integer read (123);
21+
property Bool : Boolean read (1 <> 0);
22+
property Num : Float read (3.14);
23+
property Arr : array of String read (StrSplit('abc,de', ','));
24+
property Sub : TSubTest;
25+
end;
26+
27+
var i := new TTest;
28+
i.Sub := new TSubTest;
29+
i.Sub.Name := 'hello';
30+
i.Sub.Sub := new TSubTest;
31+
i.Sub.Sub.Name := 'world';
32+
33+
34+
PrintLn(JSON.PrettyStringify(i));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"Arr" : [
3+
"abc",
4+
"de"
5+
],
6+
"Bool" : true,
7+
"Int" : 123,
8+
"Num" : 3.14,
9+
"Str" : "hello",
10+
"Sub" : {
11+
"Name" : "<hello>",
12+
"Sub" : {
13+
"Name" : "<world>",
14+
"Sub" : null
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)