mirror of
				https://gitlab.com/freepascal.org/fpc/source.git
				synced 2025-10-31 08:31:49 +01: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/Makefile.fpc svneol=native#text/plain | ||||||
| packages/fcl-json/examples/confdemo.lpi 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/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.lpi svneol=native#text/plain | ||||||
| packages/fcl-json/examples/parsedemo.pp svneol=native#text/plain | packages/fcl-json/examples/parsedemo.pp svneol=native#text/plain | ||||||
| packages/fcl-json/examples/simpledemo.lpi 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 |   published | ||||||
|     procedure TestNull; |     procedure TestNull; | ||||||
|     Procedure TestClone; |     Procedure TestClone; | ||||||
|  |     Procedure TestFormat; | ||||||
|   end; |   end; | ||||||
|    |    | ||||||
|   { TTestBoolean } |   { TTestBoolean } | ||||||
| @ -65,6 +66,7 @@ type | |||||||
|     procedure TestTrue; |     procedure TestTrue; | ||||||
|     procedure TestFalse; |     procedure TestFalse; | ||||||
|     Procedure TestClone; |     Procedure TestClone; | ||||||
|  |     Procedure TestFormat; | ||||||
|   end; |   end; | ||||||
|    |    | ||||||
|   { TTestInteger } |   { TTestInteger } | ||||||
| @ -77,6 +79,7 @@ type | |||||||
|     procedure TestNegative; |     procedure TestNegative; | ||||||
|     procedure TestZero; |     procedure TestZero; | ||||||
|     Procedure TestClone; |     Procedure TestClone; | ||||||
|  |     Procedure TestFormat; | ||||||
|   end; |   end; | ||||||
| 
 | 
 | ||||||
|   { TTestInt64 } |   { TTestInt64 } | ||||||
| @ -89,6 +92,7 @@ type | |||||||
|     procedure TestNegative; |     procedure TestNegative; | ||||||
|     procedure TestZero; |     procedure TestZero; | ||||||
|     Procedure TestClone; |     Procedure TestClone; | ||||||
|  |     Procedure TestFormat; | ||||||
|   end; |   end; | ||||||
|    |    | ||||||
|   { TTestFloat } |   { TTestFloat } | ||||||
| @ -101,6 +105,7 @@ type | |||||||
|     procedure TestNegative; |     procedure TestNegative; | ||||||
|     procedure TestZero; |     procedure TestZero; | ||||||
|     Procedure TestClone; |     Procedure TestClone; | ||||||
|  |     Procedure TestFormat; | ||||||
|   end; |   end; | ||||||
| 
 | 
 | ||||||
|   { TTestString } |   { TTestString } | ||||||
| @ -117,6 +122,7 @@ type | |||||||
|     Procedure TestBooleanTrue; |     Procedure TestBooleanTrue; | ||||||
|     Procedure TestBooleanFalse; |     Procedure TestBooleanFalse; | ||||||
|     Procedure TestClone; |     Procedure TestClone; | ||||||
|  |     Procedure TestFormat; | ||||||
|   end; |   end; | ||||||
|    |    | ||||||
|   { TTestArray } |   { TTestArray } | ||||||
| @ -150,6 +156,7 @@ type | |||||||
|     procedure TestDelete; |     procedure TestDelete; | ||||||
|     procedure TestRemove; |     procedure TestRemove; | ||||||
|     Procedure TestClone; |     Procedure TestClone; | ||||||
|  |     Procedure TestFormat; | ||||||
|   end; |   end; | ||||||
|    |    | ||||||
|   { TTestObject } |   { TTestObject } | ||||||
| @ -186,6 +193,7 @@ type | |||||||
|     procedure TestClone; |     procedure TestClone; | ||||||
|     procedure TestExtract; |     procedure TestExtract; | ||||||
|     Procedure TestNonExistingAccessError; |     Procedure TestNonExistingAccessError; | ||||||
|  |     Procedure TestFormat; | ||||||
|   end; |   end; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -449,6 +457,20 @@ begin | |||||||
|   end; |   end; | ||||||
| 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 } | { TTestNull } | ||||||
| @ -495,6 +517,18 @@ begin | |||||||
|   end; |   end; | ||||||
| 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 } | { TTestString } | ||||||
| 
 | 
 | ||||||
| @ -663,6 +697,19 @@ begin | |||||||
|   end; |   end; | ||||||
| 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); | procedure TTestString.DoTestFloat(F : TJSOnFloat;S : String; OK : Boolean); | ||||||
| 
 | 
 | ||||||
| Var | Var | ||||||
| @ -749,6 +796,20 @@ begin | |||||||
| 
 | 
 | ||||||
| end; | 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 } | { TTestInt64 } | ||||||
| 
 | 
 | ||||||
| procedure TTestInt64.DoTest(I: Int64); | procedure TTestInt64.DoTest(I: Int64); | ||||||
| @ -813,6 +874,19 @@ begin | |||||||
| 
 | 
 | ||||||
| end; | 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 } | { TTestFloat } | ||||||
| 
 | 
 | ||||||
| procedure TTestFloat.DoTest(F: TJSONFloat); | procedure TTestFloat.DoTest(F: TJSONFloat); | ||||||
| @ -888,6 +962,21 @@ begin | |||||||
| 
 | 
 | ||||||
| end; | 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 } | { TTestArray } | ||||||
| 
 | 
 | ||||||
| procedure TTestArray.TestCreate; | procedure TTestArray.TestCreate; | ||||||
| @ -1437,6 +1526,32 @@ begin | |||||||
|   end; |   end; | ||||||
| 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 } | { TTestObject } | ||||||
| 
 | 
 | ||||||
| procedure TTestObject.TestCreate; | procedure TTestObject.TestCreate; | ||||||
| @ -1833,6 +1948,26 @@ begin | |||||||
|   AssertException(EJSON,@TestAccessError); |   AssertException(EJSON,@TestAccessError); | ||||||
| end; | 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; | procedure TTestObject.TestCreateString; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -135,7 +135,7 @@ Var | |||||||
|   J : TJSONData; |   J : TJSONData; | ||||||
| 
 | 
 | ||||||
| begin | begin | ||||||
|   P:=TJSONParser.Create('True'); |   P:=TJSONParser.Create('true'); | ||||||
|   Try |   Try | ||||||
|     J:=P.Parse; |     J:=P.Parse; | ||||||
|     If (J=Nil) then |     If (J=Nil) then | ||||||
| @ -155,7 +155,7 @@ Var | |||||||
|   J : TJSONData; |   J : TJSONData; | ||||||
| 
 | 
 | ||||||
| begin | begin | ||||||
|   P:=TJSONParser.Create('False'); |   P:=TJSONParser.Create('false'); | ||||||
|   Try |   Try | ||||||
|     J:=P.Parse; |     J:=P.Parse; | ||||||
|     If (J=Nil) then |     If (J=Nil) then | ||||||
| @ -339,6 +339,7 @@ Var | |||||||
| begin | begin | ||||||
|   ParseOK:=False; |   ParseOK:=False; | ||||||
|   P:=TJSONParser.Create(S); |   P:=TJSONParser.Create(S); | ||||||
|  |   P.Strict:=True; | ||||||
|   J:=Nil; |   J:=Nil; | ||||||
|   Try |   Try | ||||||
|     Try |     Try | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 michael
						michael