mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-20 19:29:26 +02:00
* Added parser demo
* Removed debug statements from parser code git-svn-id: trunk@8556 -
This commit is contained in:
parent
821eb1fe18
commit
f8dfbc729f
2
.gitattributes
vendored
2
.gitattributes
vendored
@ -4264,6 +4264,8 @@ packages/fcl-image/src/pscanvas.pp svneol=native#text/plain
|
||||
packages/fcl-image/src/targacmn.pp svneol=native#text/plain
|
||||
packages/fcl-json/Makefile svneol=native#text/plain
|
||||
packages/fcl-json/Makefile.fpc svneol=native#text/plain
|
||||
packages/fcl-json/demo/parsedemo.lpi svneol=native#text/plain
|
||||
packages/fcl-json/demo/parsedemo.pp svneol=native#text/plain
|
||||
packages/fcl-json/demo/simpledemo.lpi svneol=native#text/plain
|
||||
packages/fcl-json/demo/simpledemo.pp svneol=native#text/plain
|
||||
packages/fcl-json/src/fpjson.pp svneol=native#text/plain
|
||||
|
46
packages/fcl-json/demo/parsedemo.lpi
Normal file
46
packages/fcl-json/demo/parsedemo.lpi
Normal file
@ -0,0 +1,46 @@
|
||||
<?xml version="1.0"?>
|
||||
<CONFIG>
|
||||
<ProjectOptions>
|
||||
<PathDelim Value="/"/>
|
||||
<Version Value="5"/>
|
||||
<General>
|
||||
<SessionStorage Value="InProjectDir"/>
|
||||
<MainUnit Value="0"/>
|
||||
<TargetFileExt Value=""/>
|
||||
</General>
|
||||
<VersionInfo>
|
||||
<ProjectVersion Value=""/>
|
||||
</VersionInfo>
|
||||
<PublishOptions>
|
||||
<Version Value="2"/>
|
||||
<IgnoreBinaries Value="False"/>
|
||||
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
|
||||
<ExcludeFileFilter Value="*.(bak|ppu|ppw|o|so);*~;backup"/>
|
||||
</PublishOptions>
|
||||
<RunParams>
|
||||
<local>
|
||||
<FormatVersion Value="1"/>
|
||||
<LaunchingApplication PathPlusParams="/usr/X11R6/bin/xterm -T 'Lazarus Run Output' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine)"/>
|
||||
</local>
|
||||
</RunParams>
|
||||
<Units Count="1">
|
||||
<Unit0>
|
||||
<Filename Value="parsedemo.pp"/>
|
||||
<IsPartOfProject Value="True"/>
|
||||
<UnitName Value="parsedemo"/>
|
||||
</Unit0>
|
||||
</Units>
|
||||
</ProjectOptions>
|
||||
<CompilerOptions>
|
||||
<Version Value="5"/>
|
||||
<SearchPaths>
|
||||
<OtherUnitFiles Value="../src/"/>
|
||||
</SearchPaths>
|
||||
<CodeGeneration>
|
||||
<Generate Value="Faster"/>
|
||||
</CodeGeneration>
|
||||
<Other>
|
||||
<CompilerPath Value="$(CompPath)"/>
|
||||
</Other>
|
||||
</CompilerOptions>
|
||||
</CONFIG>
|
123
packages/fcl-json/demo/parsedemo.pp
Normal file
123
packages/fcl-json/demo/parsedemo.pp
Normal file
@ -0,0 +1,123 @@
|
||||
program parsedemo;
|
||||
|
||||
{$mode objfpc}{$H+}
|
||||
|
||||
uses
|
||||
Classes, SysUtils, fpjson,jsonparser;
|
||||
|
||||
Procedure DoParse(P : TJSONParser);
|
||||
|
||||
Var
|
||||
J : TJSONData;
|
||||
|
||||
begin
|
||||
Try
|
||||
J:=P.Parse;
|
||||
Try
|
||||
Writeln('Parse succesful. Dumping JSON data : ');
|
||||
If Assigned(J) then
|
||||
begin
|
||||
Writeln('Returned JSON structure has class : ',J.ClassName);
|
||||
Writeln(J.AsJSON)
|
||||
end
|
||||
else
|
||||
Writeln('No JSON data available');
|
||||
Finally
|
||||
FreeAndNil(J);
|
||||
end;
|
||||
except
|
||||
On E : Exception do
|
||||
Writeln('An Exception occurred when parsing : ',E.Message);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
Procedure ParseFile (FileName : String);
|
||||
|
||||
Var
|
||||
F : TFileStream;
|
||||
P : TJSONParser;
|
||||
|
||||
begin
|
||||
F:=TFileStream.Create(FileName,fmopenRead);
|
||||
try
|
||||
// Create parser with Stream as source.
|
||||
P:=TJSONParser.Create(F);
|
||||
try
|
||||
DoParse(P);
|
||||
finally
|
||||
FreeAndNil(P);
|
||||
end;
|
||||
finally
|
||||
F.Destroy;
|
||||
end;
|
||||
end;
|
||||
|
||||
Procedure ParseString(S : String);
|
||||
|
||||
Var
|
||||
P : TJSONParser;
|
||||
begin
|
||||
// Create parser with Stream as source.
|
||||
P:=TJSONParser.Create(S);
|
||||
try
|
||||
DoParse(P);
|
||||
finally
|
||||
FreeAndNil(P);
|
||||
end;
|
||||
end;
|
||||
|
||||
Procedure DefaultParsing;
|
||||
|
||||
Const
|
||||
// From JSON website
|
||||
SAddr ='{ "addressbook": { "name": "Mary Lebow", '+
|
||||
' "address": {'+
|
||||
' "street": "5 Main Street",'+LineEnding+
|
||||
' "city": "San Diego, CA",'+LineEnding+
|
||||
' "zip": 91912,'+LineEnding+
|
||||
' },'+LineEnding+
|
||||
' "phoneNumbers": [ '+LineEnding+
|
||||
' "619 332-3452",'+LineEnding+
|
||||
' "664 223-4667"'+LineEnding+
|
||||
' ]'+LineEnding+
|
||||
' }'+LineEnding+
|
||||
'}';
|
||||
|
||||
|
||||
begin
|
||||
ParseString('');
|
||||
ParseString('NULL');
|
||||
ParseString('1');
|
||||
ParseString('2.3');
|
||||
ParseString('True');
|
||||
ParseString('False');
|
||||
ParseString('"A string"');
|
||||
ParseString('[ Null, False, 1 , 2.3, "a" , { "b" : 1 }]');
|
||||
ParseString('{ "a" : 1, "b" : "Something" }');
|
||||
ParseString(SAddr);
|
||||
end;
|
||||
|
||||
Procedure Usage;
|
||||
|
||||
begin
|
||||
Writeln('Usage : parsedemo arg1 [arg2 [arg3 ...[argN]]]');
|
||||
Writeln(' ArgN can be the name of an existing file, or a JSON string');
|
||||
end;
|
||||
|
||||
Var
|
||||
I : Integer;
|
||||
|
||||
begin
|
||||
If (ParamCount=0) then
|
||||
DefaultParsing
|
||||
else if (ParamCount=1) and ((Paramstr(1)='-h') or (ParamStr(1)='--help')) then
|
||||
Usage
|
||||
else
|
||||
For I:=1 to ParamCount do
|
||||
If FileExists(Paramstr(i)) then
|
||||
ParseFile(ParamStr(I))
|
||||
else
|
||||
ParseString(Paramstr(I));
|
||||
end.
|
||||
|
@ -164,7 +164,6 @@ begin
|
||||
T:=GetNextToken;
|
||||
If (T<>tkColon) then
|
||||
DoError(SErrExpectedColon);
|
||||
Writeln('Getting element');
|
||||
E:=DoParse(False,False);
|
||||
Result.Add(N,E);
|
||||
T:=GetNextToken;
|
||||
@ -221,7 +220,6 @@ begin
|
||||
Repeat
|
||||
Result:=FScanner.FetchToken;
|
||||
Until (Result<>tkWhiteSpace);
|
||||
Writeln('GetNextToken : ',CurrentTokenString);
|
||||
end;
|
||||
|
||||
Procedure TJSONParser.DoError(Msg : String);
|
||||
|
Loading…
Reference in New Issue
Block a user