From 1c6defe7beac4f759e60c039d0fb43c70f7fe0a5 Mon Sep 17 00:00:00 2001 From: michael Date: Tue, 12 Oct 2010 20:45:49 +0000 Subject: [PATCH] * Added example and tests for format git-svn-id: trunk@16147 - --- .gitattributes | 1 + packages/fcl-json/examples/demoformat.pp | 21 ++++ packages/fcl-json/tests/testjsondata.pp | 135 ++++++++++++++++++++++ packages/fcl-json/tests/testjsonparser.pp | 5 +- 4 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 packages/fcl-json/examples/demoformat.pp diff --git a/.gitattributes b/.gitattributes index 0f137842b4..3bae22d54c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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 diff --git a/packages/fcl-json/examples/demoformat.pp b/packages/fcl-json/examples/demoformat.pp new file mode 100644 index 0000000000..bdda49cbf7 --- /dev/null +++ b/packages/fcl-json/examples/demoformat.pp @@ -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. \ No newline at end of file diff --git a/packages/fcl-json/tests/testjsondata.pp b/packages/fcl-json/tests/testjsondata.pp index 2a0a886f03..32b89c0877 100644 --- a/packages/fcl-json/tests/testjsondata.pp +++ b/packages/fcl-json/tests/testjsondata.pp @@ -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; diff --git a/packages/fcl-json/tests/testjsonparser.pp b/packages/fcl-json/tests/testjsonparser.pp index 3542e212d9..af43eb30e5 100644 --- a/packages/fcl-json/tests/testjsonparser.pp +++ b/packages/fcl-json/tests/testjsonparser.pp @@ -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