json2pas.pp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. { JSON 2 Pascal class converter
  2. Copyright (C) 2016 Michael Van Canneyt ([email protected])
  3. This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
  4. published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  5. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  6. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. A copy of the GNU General Public License is available on the World Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can
  8. also obtain it by writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  9. }
  10. program json2pas;
  11. {$mode objfpc}{$H+}
  12. uses
  13. Classes, SysUtils, CustApp, jsonparser, fpjsontopas;
  14. type
  15. { TJSON2PasApplication }
  16. TJSON2PasApplication = class(TCustomApplication)
  17. Private
  18. FGen : TJSONToPascal;
  19. FIN : TFileStream;
  20. FON : String;
  21. procedure ProcessOptions;
  22. protected
  23. procedure DoRun; override;
  24. public
  25. constructor Create(TheOwner: TComponent); override;
  26. destructor Destroy; override;
  27. procedure Usage(Msg: String); virtual;
  28. end;
  29. { TJSON2PasApplication }
  30. procedure TJSON2PasApplication.ProcessOptions;
  31. var
  32. UN,J: String;
  33. begin
  34. UN:='';
  35. if HasOption('d','generate-delphi') then
  36. FGen.Options:=FGen.Options+[jpoDelphiJSON];
  37. if HasOption('l','generate-load') then
  38. FGen.Options:=FGen.Options+[jpoGenerateLoad];
  39. if HasOption('s','generate-save') then
  40. FGen.Options:=FGen.Options+[jpoGenerateSave];
  41. if HasOption('c','load-ignores-case') then
  42. FGen.Options:=FGen.Options+[jpoLoadCaseInsensitive];
  43. if HasOption('e','use-setter') then
  44. FGen.Options:=FGen.Options+[jpoUseSetter];
  45. if HasOption('r','load-with-error') then
  46. FGen.Options:=FGen.Options+[jpoUnknownLoadPropsError];
  47. if HasOption('u','unit') then
  48. UN:=GetOptionValue('u','unit');
  49. J:=GetOptionValue('i','input');
  50. if (J<>'') then
  51. begin
  52. FIN:=TFileStream.Create(J,fmOpenRead or fmShareDenyWrite);
  53. FGen.JSONStream:=FIN;
  54. end
  55. else
  56. FGen.JSON:=getOptionValue('j','json');
  57. if HasOption('o','output') then
  58. FON:=GetOptionValue('o','output');
  59. if (FON='') then
  60. begin
  61. if Assigned(FIN) then
  62. FON:=ChangeFileExt(FIN.FileName,'')
  63. else if (UN<>'') then
  64. FON:=UN;
  65. if (UN='') then
  66. UN:=ChangeFileExt(ExtractFileName(FON),'');
  67. end
  68. else
  69. UN:=ChangeFileExt(ExtractFileName(FON),'');
  70. if (ExtractFileExt(FON)='') then
  71. if (jpoDelphiJSON in Fgen.Options) then
  72. FON:=FON+'.pas'
  73. else
  74. FON:=FON+'.pp';
  75. FGen.DestUnitName:=UN;
  76. If HasOption('n','classname') then
  77. FGen.PropertyMap.AddPath('',GetOptionValue('n','classname'));
  78. end;
  79. procedure TJSON2PasApplication.DoRun;
  80. var
  81. ErrorMsg: String;
  82. begin
  83. // quick check parameters
  84. ErrorMsg:=CheckOptions('hi:j:u:o:n:dlscer', ['help','input:','output:','unit:','json:','generate-delphi','generate-load','generate-save','load-ignores-case','use-setter','classname:','load-with-error']);
  85. if (ErrorMsg<>'') or HasOption('h', 'help') then
  86. Usage(ErrorMsg);
  87. ProcessOptions;
  88. FGen.Execute;
  89. FGen.Code.SaveToFile(FON);
  90. Terminate;
  91. end;
  92. constructor TJSON2PasApplication.Create(TheOwner: TComponent);
  93. begin
  94. inherited Create(TheOwner);
  95. StopOnException:=True;
  96. FGen:=TJSONToPascal.Create(Self);
  97. end;
  98. destructor TJSON2PasApplication.Destroy;
  99. begin
  100. FreeAndNil(FGen);
  101. FreeAndNil(FIN);
  102. inherited Destroy;
  103. end;
  104. procedure TJSON2PasApplication.Usage(Msg : String);
  105. begin
  106. if (Msg<>'') then
  107. Writeln('Error : ',Msg);
  108. writeln('Usage: ', ExeName, ' [options]');
  109. Writeln('Where options is one or more of:');
  110. // 'hi:j:u:o:n:dlscer', ['help','input:','output:','unit:','json:','generate-delphi','generate-load','generate-save','load-ignores-case','use-setter','classname:','load-with-error']);
  111. Writeln('-h --help Show this help');
  112. Writeln('-i --input=file Use file as JSON input. The file must contain a valid JSON object.');
  113. Writeln('-i --json=json Use json as JSON input. The value must contain a valid JSON object.');
  114. Writeln('-u --unit=name Use name as the name of the unit.');
  115. Writeln(' If no output file is specified, this sets the name');
  116. Writeln('-o --output=file Set the output filename. Sets the unit name if none is given');
  117. Writeln('-d --generate-delphi Use Delphi 10 JSON routines');
  118. Writeln('-l --generate-load Create LoadFromJSON routine');
  119. Writeln('-s --generate-save Create SaveToJSON routine');
  120. Writeln('-c --load-ignores-case The LoadFromJSON routine will ignore case');
  121. Writeln('-e --use-setter Property setters use a routine instead of writing to field');
  122. Writeln('-n --classname=name Set the name of the top-level class (default: TMyObject)');
  123. Writeln('-r --load-with-error Load will raise an exception if an unknown property is met');
  124. Halt(Ord(Msg<>''));
  125. end;
  126. var
  127. Application: TJSON2PasApplication;
  128. begin
  129. Application:=TJSON2PasApplication.Create(nil);
  130. Application.Title:='JSON to Pascal code generator';
  131. Application.Run;
  132. Application.Free;
  133. end.