parsedemo.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. {
  2. This file is part of the Free Component Library
  3. JSON Parser demo
  4. Copyright (c) 2007 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program parsedemo;
  12. {$mode objfpc}{$H+}
  13. uses
  14. Classes, SysUtils, fpjson,jsonparser;
  15. Procedure DoParse(P : TJSONParser);
  16. Var
  17. J : TJSONData;
  18. begin
  19. Try
  20. J:=P.Parse;
  21. Try
  22. Writeln('Parse succesful. Dumping JSON data : ');
  23. If Assigned(J) then
  24. begin
  25. Writeln('Returned JSON structure has class : ',J.ClassName);
  26. Writeln(J.AsJSON)
  27. end
  28. else
  29. Writeln('No JSON data available');
  30. Finally
  31. FreeAndNil(J);
  32. end;
  33. except
  34. On E : Exception do
  35. Writeln('An Exception occurred when parsing : ',E.Message);
  36. end;
  37. end;
  38. Procedure ParseFile (FileName : String);
  39. Var
  40. F : TFileStream;
  41. P : TJSONParser;
  42. begin
  43. F:=TFileStream.Create(FileName,fmopenRead);
  44. try
  45. // Create parser with Stream as source.
  46. P:=TJSONParser.Create(F);
  47. try
  48. DoParse(P);
  49. finally
  50. FreeAndNil(P);
  51. end;
  52. finally
  53. F.Destroy;
  54. end;
  55. end;
  56. Procedure ParseString(S : String);
  57. Var
  58. P : TJSONParser;
  59. begin
  60. // Create parser with Stream as source.
  61. P:=TJSONParser.Create(S);
  62. try
  63. DoParse(P);
  64. finally
  65. FreeAndNil(P);
  66. end;
  67. end;
  68. Procedure DefaultParsing;
  69. Const
  70. // From JSON website
  71. SAddr ='{ "addressbook": { "name": "Mary Lebow", '+
  72. ' "address": {'+
  73. ' "street": "5 Main Street",'+LineEnding+
  74. ' "city": "San Diego, CA",'+LineEnding+
  75. ' "zip": 91912,'+LineEnding+
  76. ' },'+LineEnding+
  77. ' "phoneNumbers": [ '+LineEnding+
  78. ' "619 332-3452",'+LineEnding+
  79. ' "664 223-4667"'+LineEnding+
  80. ' ]'+LineEnding+
  81. ' }'+LineEnding+
  82. '}';
  83. begin
  84. ParseString('');
  85. ParseString('NULL');
  86. ParseString('1');
  87. ParseString('2.3');
  88. ParseString('True');
  89. ParseString('False');
  90. ParseString('"A string"');
  91. ParseString('[ Null, False, 1 , 2.3, "a" , { "b" : 1 }]');
  92. ParseString('{ "a" : 1, "b" : "Something" }');
  93. ParseString(SAddr);
  94. end;
  95. Procedure Usage;
  96. begin
  97. Writeln('Usage : parsedemo arg1 [arg2 [arg3 ...[argN]]]');
  98. Writeln(' ArgN can be the name of an existing file, or a JSON string');
  99. end;
  100. Var
  101. I : Integer;
  102. begin
  103. If (ParamCount=0) then
  104. DefaultParsing
  105. else if (ParamCount=1) and ((Paramstr(1)='-h') or (ParamStr(1)='--help')) then
  106. Usage
  107. else
  108. For I:=1 to ParamCount do
  109. If FileExists(Paramstr(i)) then
  110. ParseFile(ParamStr(I))
  111. else
  112. ParseString(Paramstr(I));
  113. end.