Skip to content

Commit

Permalink
Improved JSONWriters demo
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Jul 28, 2024
1 parent e494111 commit 835127c
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 310 deletions.
65 changes: 19 additions & 46 deletions samples/jsonwriterrenders/JSONSampleController.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,41 @@ interface

type

[MVCPath('/')]
[MVCPath]
TMyController = class(TMVCController)
public
[MVCPath('/')]
[MVCPath]
[MVCHTTPMethod([httpGET])]
procedure Index(ctx: TWebContext);
procedure OnBeforeAction(Context: TWebContext; const AActionName: string;
var Handled: Boolean); override;
procedure OnAfterAction(Context: TWebContext;
const AActionName: string); override;
function Index: String;
end;

implementation

uses
System.Classes, System.JSON.Writers, System.JSON.Types;

procedure TMyController.Index(ctx: TWebContext);
var
StringWriter: TStringWriter;
Writer: TJSONTextWriter;
oUser: String;
Arr: TArray<String>;
function TMyController.Index: String;
begin
StringWriter := TStringWriter.Create();
Writer := TJsonTextWriter.Create(StringWriter);
var lJSONWriter := TJsonTextWriter.Create(TStringWriter.Create(), True);
try
Writer.Formatting := TJsonFormatting.Indented;
Writer.WriteStartObject;
Writer.WritePropertyName('Users');
Writer.WriteStartArray;
Arr := ['Daniele','Peter','Scott'];
for oUser in Arr do
lJSONWriter.Formatting := TJsonFormatting.Indented;
lJSONWriter.WriteStartObject;
lJSONWriter.WritePropertyName('Users');
lJSONWriter.WriteStartArray;
var Arr := ['Daniele','Peter','Scott'];
for var oUser in Arr do
begin
Writer.WriteStartObject;
Writer.WritePropertyName('UserName');
Writer.WriteValue(oUser);
Writer.WriteEndObject;
lJSONWriter.WriteStartObject;
lJSONWriter.WritePropertyName('UserName');
lJSONWriter.WriteValue(oUser);
lJSONWriter.WriteEndObject;
end;
Writer.WriteEndArray;
Writer.WriteEndObject;
Render(StringWriter, False);
lJSONWriter.WriteEndArray;
lJSONWriter.WriteEndObject;
Result := lJSONWriter.Writer.ToString;
finally
Writer.Free;
StringWriter.Free;
lJSONWriter.Free;
end;
end;

procedure TMyController.OnAfterAction(Context: TWebContext;
const AActionName: string);
begin
{ Executed after each action }
inherited;
end;

procedure TMyController.OnBeforeAction(Context: TWebContext;
const AActionName: string; var Handled: Boolean);
begin
{ Executed before each action
if handled is true (or an exception is raised) the actual
action will not be called }
inherited;
end;

end.
1 change: 0 additions & 1 deletion samples/jsonwriterrenders/WebModuleU.dfm
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
object MyWebModule: TMyWebModule
OldCreateOrder = False
OnCreate = WebModuleCreate
OnDestroy = WebModuleDestroy
Actions = <>
Expand Down
5 changes: 0 additions & 5 deletions samples/jsonwriterrenders/WebModuleU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ procedure TMyWebModule.WebModuleCreate(Sender: TObject);
Config[TMVCConfigKey.ExposeServerSignature] := 'true';
end);
FMVC.AddController(TMyController);
FMVC.AddMiddleware(TMVCStaticFilesMiddleware.Create(
'/', { StaticFilesPath }
ExtractFilePath(GetModuleName(HInstance)) + '\www', { DocumentRoot }
'index.html' {IndexDocument - Before it was named fallbackresource}
));
end;

procedure TMyWebModule.WebModuleDestroy(Sender: TObject);
Expand Down
Loading

0 comments on commit 835127c

Please sign in to comment.