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