delphimvcframework/samples/jsonwriterrenders/JSONSampleController.pas

75 lines
1.7 KiB
ObjectPascal
Raw Normal View History

2016-04-22 09:46:21 +02:00
unit JSONSampleController;
interface
uses
MVCFramework;
type
[MVCPath('/')]
TMyController = class(TMVCController)
public
[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;
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>;
begin
StringWriter := TStringWriter.Create();
Writer := TJsonTextWriter.Create(StringWriter);
try
Writer.Formatting := TJsonFormatting.Indented;
Writer.WriteStartObject;
Writer.WritePropertyName('Users');
Writer.WriteStartArray;
Arr := ['Daniele','Peter','Scott'];
for oUser in Arr do
begin
Writer.WriteStartObject;
Writer.WritePropertyName('UserName');
Writer.WriteValue(oUser);
Writer.WriteEndObject;
end;
Writer.WriteEndArray;
Writer.WriteEndObject;
Render(StringWriter, False);
finally
Writer.Free;
StringWriter.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.