* Added example and tests for format

git-svn-id: trunk@16147 -
This commit is contained in:
michael 2010-10-12 20:45:49 +00:00
parent 48e8728b89
commit 1c6defe7be
4 changed files with 160 additions and 2 deletions

1
.gitattributes vendored
View File

@ -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

View 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.

View File

@ -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;

View File

@ -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