* Added beautify for JSON

This commit is contained in:
Andreas Schneider 2015-09-17 15:54:05 +02:00
parent 61f16f0278
commit 3665c0a6b5
1 changed files with 24 additions and 3 deletions

View File

@ -26,7 +26,7 @@ uses
SysUtils, Classes, strutils, IniFiles,
{$ifdef use_synapse}httpsend, ssl_openssl,{$endif}
{$ifdef use_fclweb}fphttpclient,{$endif}
JTemplate;
JTemplate, fpjson, jsonparser;
var
data: TextFile;
@ -40,6 +40,7 @@ var
configDir, templateDir: String;
templateFile, templateName: String;
sessionIni: TIniFile;
beautify: Boolean;
procedure CmdAskUser(AName: String);
var
@ -92,6 +93,8 @@ var
{$ifdef use_fclweb}
request, response: TStream;
{$endif}
jsonParser: TJSONParser;
jsonData: TJSONData;
begin
parser.Content := AURL;
parser.Replace;
@ -150,9 +153,21 @@ begin
for s in http.ResponseHeaders do
writeln(' ', s);
writeln;
response.Position := 0;
if beautify and (Pos('application/json', http.GetHeader(http.ResponseHeaders, 'Content-Type')) > 0) then
begin
jsonParser := TJSONParser.Create(response);
jsonData := jsonParser.Parse;
writeln(jsonData.FormatJSON);
jsonData.Free;
jsonParser.Free;
end else
begin
content.LoadFromStream(response);
writeln(content.Text);
end;
response.Free;
request.Free;
@ -199,6 +214,11 @@ begin
Result := True;
CmdBasicAuth(Copy(ALine, 10, Length(ALine)));
end else
if ALine = 'Beautify' then
begin
Result := True;
beautify := True;
end else
if AnsiStartsStr('Call ', ALine) then
begin
Result := True;
@ -255,6 +275,7 @@ begin
parser := TJTemplateParser.Create;
content := TStringList.Create;
beautify := False; //By default, we don't want to manipulate output.
{$ifdef use_synapse}
http := THTTPSend.Create;