mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-16 23:21:57 +02:00
* Added example and tests for format
git-svn-id: trunk@16147 -
This commit is contained in:
parent
48e8728b89
commit
1c6defe7be
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -2011,6 +2011,7 @@ packages/fcl-json/Makefile svneol=native#text/plain
|
||||
packages/fcl-json/Makefile.fpc svneol=native#text/plain
|
||||
packages/fcl-json/examples/confdemo.lpi svneol=native#text/plain
|
||||
packages/fcl-json/examples/confdemo.pp svneol=native#text/plain
|
||||
packages/fcl-json/examples/demoformat.pp svneol=native#text/plain
|
||||
packages/fcl-json/examples/parsedemo.lpi svneol=native#text/plain
|
||||
packages/fcl-json/examples/parsedemo.pp svneol=native#text/plain
|
||||
packages/fcl-json/examples/simpledemo.lpi svneol=native#text/plain
|
||||
|
21
packages/fcl-json/examples/demoformat.pp
Normal file
21
packages/fcl-json/examples/demoformat.pp
Normal file
@ -0,0 +1,21 @@
|
||||
{$mode objfpc}
|
||||
{$h+}
|
||||
program demoformat;
|
||||
|
||||
uses fpjson;
|
||||
|
||||
var
|
||||
O : TJSONObject;
|
||||
A : TJSONArray;
|
||||
begin
|
||||
O:=TJSONObject.Create(['a',1,'b','two','three',TJSONObject.Create(['x',10,'y',20])]);
|
||||
Writeln (O.FormatJSon);
|
||||
Writeln (O.FormatJSon([foDonotQuoteMembers,foUseTabChar],1));
|
||||
Writeln (O.FormatJSon([foSingleLineObject,foUseTabChar],1));
|
||||
Writeln (O.asJSON);
|
||||
A:=TJSONArray.Create([1,2,'a',TJSONObject.Create(['x',10,'y',20])]);
|
||||
Writeln (A.FormatJSon());
|
||||
Writeln (A.FormatJSON([foSinglelineArray],2));
|
||||
Writeln (A.FormatJSON([foSinglelineArray,foSingleLineObject],2));
|
||||
Writeln (A.asJSON);
|
||||
end.
|
@ -56,6 +56,7 @@ type
|
||||
published
|
||||
procedure TestNull;
|
||||
Procedure TestClone;
|
||||
Procedure TestFormat;
|
||||
end;
|
||||
|
||||
{ TTestBoolean }
|
||||
@ -65,6 +66,7 @@ type
|
||||
procedure TestTrue;
|
||||
procedure TestFalse;
|
||||
Procedure TestClone;
|
||||
Procedure TestFormat;
|
||||
end;
|
||||
|
||||
{ TTestInteger }
|
||||
@ -77,6 +79,7 @@ type
|
||||
procedure TestNegative;
|
||||
procedure TestZero;
|
||||
Procedure TestClone;
|
||||
Procedure TestFormat;
|
||||
end;
|
||||
|
||||
{ TTestInt64 }
|
||||
@ -89,6 +92,7 @@ type
|
||||
procedure TestNegative;
|
||||
procedure TestZero;
|
||||
Procedure TestClone;
|
||||
Procedure TestFormat;
|
||||
end;
|
||||
|
||||
{ TTestFloat }
|
||||
@ -101,6 +105,7 @@ type
|
||||
procedure TestNegative;
|
||||
procedure TestZero;
|
||||
Procedure TestClone;
|
||||
Procedure TestFormat;
|
||||
end;
|
||||
|
||||
{ TTestString }
|
||||
@ -117,6 +122,7 @@ type
|
||||
Procedure TestBooleanTrue;
|
||||
Procedure TestBooleanFalse;
|
||||
Procedure TestClone;
|
||||
Procedure TestFormat;
|
||||
end;
|
||||
|
||||
{ TTestArray }
|
||||
@ -150,6 +156,7 @@ type
|
||||
procedure TestDelete;
|
||||
procedure TestRemove;
|
||||
Procedure TestClone;
|
||||
Procedure TestFormat;
|
||||
end;
|
||||
|
||||
{ TTestObject }
|
||||
@ -186,6 +193,7 @@ type
|
||||
procedure TestClone;
|
||||
procedure TestExtract;
|
||||
Procedure TestNonExistingAccessError;
|
||||
Procedure TestFormat;
|
||||
end;
|
||||
|
||||
|
||||
@ -449,6 +457,20 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TTestBoolean.TestFormat;
|
||||
|
||||
Var
|
||||
B : TJSONBoolean;
|
||||
|
||||
begin
|
||||
B:=TJSONBoolean.Create(true);
|
||||
try
|
||||
AssertEquals('FormatJSON same as asJSON',B.asJSON,B.FormatJSON);
|
||||
finally
|
||||
B.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
|
||||
{ TTestNull }
|
||||
@ -495,6 +517,18 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TTestNull.TestFormat;
|
||||
Var
|
||||
J : TJSONNull;
|
||||
begin
|
||||
J:=TJSONNull.Create;
|
||||
try
|
||||
AssertEquals('FormatJSON same as asJSON',J.asJSON,J.FormatJSON);
|
||||
finally
|
||||
J.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
{ TTestString }
|
||||
|
||||
@ -663,6 +697,19 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TTestString.TestFormat;
|
||||
Var
|
||||
S : TJSONString;
|
||||
|
||||
begin
|
||||
S:=TJSONString.Create('aloha');
|
||||
try
|
||||
AssertEquals('FormatJSON equals JSON',S.AsJSON,S.FormatJSOn);
|
||||
finally
|
||||
FreeAndNil(S);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TTestString.DoTestFloat(F : TJSOnFloat;S : String; OK : Boolean);
|
||||
|
||||
Var
|
||||
@ -749,6 +796,20 @@ begin
|
||||
|
||||
end;
|
||||
|
||||
procedure TTestInteger.TestFormat;
|
||||
|
||||
Var
|
||||
I : TJSONIntegerNumber;
|
||||
|
||||
begin
|
||||
I:=TJSONIntegerNumber.Create(99);
|
||||
try
|
||||
AssertEquals('FormatJSON equal to JSON',I.AsJSON,I.FormatJSON);
|
||||
finally
|
||||
FreeAndNil(I);
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TTestInt64 }
|
||||
|
||||
procedure TTestInt64.DoTest(I: Int64);
|
||||
@ -813,6 +874,19 @@ begin
|
||||
|
||||
end;
|
||||
|
||||
procedure TTestInt64.TestFormat;
|
||||
Var
|
||||
I : TJSONInt64Number;
|
||||
|
||||
begin
|
||||
I:=TJSONInt64Number.Create(99);
|
||||
try
|
||||
AssertEquals('FormatJSON equal to JSON',I.AsJSON,I.FormatJSON);
|
||||
finally
|
||||
FreeAndNil(I);
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TTestFloat }
|
||||
|
||||
procedure TTestFloat.DoTest(F: TJSONFloat);
|
||||
@ -888,6 +962,21 @@ begin
|
||||
|
||||
end;
|
||||
|
||||
procedure TTestFloat.TestFormat;
|
||||
|
||||
Var
|
||||
F : TJSONFloatNumber;
|
||||
|
||||
|
||||
begin
|
||||
F:=TJSONFloatNumber.Create(1.23);
|
||||
try
|
||||
AssertEquals('FormatJSON equals asJSON',F.AsJSON,F.FormatJSON);
|
||||
finally
|
||||
FreeAndNil(F);
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TTestArray }
|
||||
|
||||
procedure TTestArray.TestCreate;
|
||||
@ -1437,6 +1526,32 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TTestArray.TestFormat;
|
||||
Var
|
||||
J : TJSONArray;
|
||||
I : TJSONData;
|
||||
|
||||
begin
|
||||
J:=TJSonArray.Create;
|
||||
try
|
||||
J.Add(0);
|
||||
J.Add(1);
|
||||
J.Add(2);
|
||||
TestItemCount(J,3);
|
||||
TestJSONType(J[0],jtNumber);
|
||||
TestJSONType(J[1],jtNumber);
|
||||
TestJSONType(J[2],jtNumber);
|
||||
TestJSON(J,'[0, 1, 2]');
|
||||
AssertEquals('FormatJSON, single line',J.AsJSON,J.FormatJSON([foSingleLineArray],1));
|
||||
AssertEquals('FormatJSON, single line','['+sLinebreak+' 0,'+sLinebreak+' 1,'+sLinebreak+' 2'+sLinebreak+']',J.FormatJSON());
|
||||
AssertEquals('FormatJSON, single line','['+sLinebreak+#9'0,'+sLinebreak+#9'1,'+sLinebreak+#9'2'+sLinebreak+']',J.FormatJSON([foUseTabChar],1));
|
||||
J.Add(TJSONObject.Create(['x',1,'y',2]));
|
||||
AssertEquals('FormatJSON, single line','['+sLinebreak+#9'0,'+sLinebreak+#9'1,'+sLinebreak+#9'2,'+sLinebreak+#9'{'+sLineBreak+#9#9'"x" : 1,'+sLineBreak+#9#9'"y" : 2'+sLinebreak+#9'}'+sLineBreak+']',J.FormatJSON([foUseTabChar],1));
|
||||
finally
|
||||
J.Free
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TTestObject }
|
||||
|
||||
procedure TTestObject.TestCreate;
|
||||
@ -1833,6 +1948,26 @@ begin
|
||||
AssertException(EJSON,@TestAccessError);
|
||||
end;
|
||||
|
||||
procedure TTestObject.TestFormat;
|
||||
|
||||
Var
|
||||
O : TJSONObject;
|
||||
|
||||
begin
|
||||
O:=TJSONObject.Create(['x',1,'y',2]);
|
||||
try
|
||||
TestJSON(O,'{ "x" : 1, "y" : 2 }');
|
||||
AssertEquals('Format equals JSON',O.AsJSON,O.FormatJSON([foSingleLineObject]));
|
||||
AssertEquals('Format 1','{'+sLineBreak+' "x" : 1,'+sLineBreak+' "y" : 2'+sLineBreak+'}',O.FormatJSON([]));
|
||||
AssertEquals('Format 1','{'+sLineBreak+' x : 1,'+sLineBreak+' y : 2'+sLineBreak+'}',O.FormatJSON([foDoNotQuoteMembers]));
|
||||
AssertEquals('Format 1','{'+sLineBreak+#9'x : 1,'+sLineBreak+#9'y : 2'+sLineBreak+'}',O.FormatJSON([foUseTabChar,foDoNotQuoteMembers],1));
|
||||
O.Add('s',TJSONObject.Create(['w',10,'h',20]));
|
||||
AssertEquals('Format 1','{'+sLineBreak+#9'x : 1,'+sLineBreak+#9'y : 2,'+sLineBreak+#9's : {'+sLineBreak+#9#9'w : 10,'+sLineBreak+#9#9'h : 20'+sLineBreak+#9'}'+sLineBreak+'}',O.FormatJSON([foUseTabChar,foDoNotQuoteMembers],1));
|
||||
finally
|
||||
O.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
procedure TTestObject.TestCreateString;
|
||||
|
||||
|
@ -135,7 +135,7 @@ Var
|
||||
J : TJSONData;
|
||||
|
||||
begin
|
||||
P:=TJSONParser.Create('True');
|
||||
P:=TJSONParser.Create('true');
|
||||
Try
|
||||
J:=P.Parse;
|
||||
If (J=Nil) then
|
||||
@ -155,7 +155,7 @@ Var
|
||||
J : TJSONData;
|
||||
|
||||
begin
|
||||
P:=TJSONParser.Create('False');
|
||||
P:=TJSONParser.Create('false');
|
||||
Try
|
||||
J:=P.Parse;
|
||||
If (J=Nil) then
|
||||
@ -339,6 +339,7 @@ Var
|
||||
begin
|
||||
ParseOK:=False;
|
||||
P:=TJSONParser.Create(S);
|
||||
P.Strict:=True;
|
||||
J:=Nil;
|
||||
Try
|
||||
Try
|
||||
|
Loading…
Reference in New Issue
Block a user