summaryrefslogtreecommitdiff
path: root/utils/json2pas/json2pas.pp
blob: 3c26825ddcc056af112ab5b5e03b354d28485331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
{ JSON 2 Pascal class converter

  Copyright (C) 2016 Michael Van Canneyt (michael@freepascal.org)

  This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
  published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

  This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  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
  also obtain it by writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}

program json2pas;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, CustApp, jsonparser, fpjsontopas;

type

  { TJSON2PasApplication }

  TJSON2PasApplication = class(TCustomApplication)
  Private
    FGen : TJSONToPascal;
    FIN : TFileStream;
    FON : String;
    procedure ProcessOptions;
  protected
    procedure DoRun; override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure Usage(Msg: String); virtual;
  end;

{ TJSON2PasApplication }

procedure TJSON2PasApplication.ProcessOptions;

var
  UN,J: String;

begin
  UN:='';
  if HasOption('d','generate-delphi') then
    FGen.Options:=FGen.Options+[jpoDelphiJSON];
  if HasOption('l','generate-load') then
    FGen.Options:=FGen.Options+[jpoGenerateLoad];
  if HasOption('s','generate-save') then
    FGen.Options:=FGen.Options+[jpoGenerateSave];
  if HasOption('c','load-ignores-case') then
    FGen.Options:=FGen.Options+[jpoLoadCaseInsensitive];
  if HasOption('e','use-setter') then
    FGen.Options:=FGen.Options+[jpoUseSetter];
  if HasOption('r','load-with-error') then
    FGen.Options:=FGen.Options+[jpoUnknownLoadPropsError];
  if HasOption('u','unit') then
    UN:=GetOptionValue('u','unit');
  J:=GetOptionValue('i','input');
  if (J<>'') then
    begin
    FIN:=TFileStream.Create(J,fmOpenRead or fmShareDenyWrite);
    FGen.JSONStream:=FIN;
    end
  else
    FGen.JSON:=getOptionValue('j','json');
  if HasOption('o','output') then
    FON:=GetOptionValue('o','output');
  if (FON='') then
    begin
    if Assigned(FIN) then
      FON:=ChangeFileExt(FIN.FileName,'')
    else if (UN<>'') then
      FON:=UN;
    if (UN='') then
      UN:=ChangeFileExt(ExtractFileName(FON),'');
    end
  else
    UN:=ChangeFileExt(ExtractFileName(FON),'');
  if (ExtractFileExt(FON)='') then
    if (jpoDelphiJSON in Fgen.Options) then
      FON:=FON+'.pas'
    else
      FON:=FON+'.pp';
  FGen.DestUnitName:=UN;
  If HasOption('n','classname') then
    FGen.PropertyMap.AddPath('',GetOptionValue('n','classname'));
end;

procedure TJSON2PasApplication.DoRun;
var
  ErrorMsg: String;
begin
  // quick check parameters
  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']);
  if (ErrorMsg<>'') or HasOption('h', 'help') then
    Usage(ErrorMsg);
  ProcessOptions;
  FGen.Execute;
  FGen.Code.SaveToFile(FON);
  Terminate;
end;

constructor TJSON2PasApplication.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  StopOnException:=True;
  FGen:=TJSONToPascal.Create(Self);
end;

destructor TJSON2PasApplication.Destroy;
begin
  FreeAndNil(FGen);
  FreeAndNil(FIN);
  inherited Destroy;
end;

procedure TJSON2PasApplication.Usage(Msg : String);
begin
  if (Msg<>'') then
    Writeln('Error : ',Msg);
  writeln('Usage: ', ExeName, ' [options]');
  Writeln('Where options is one or more of:');
// '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']);
  Writeln('-h --help              Show this help');
  Writeln('-i --input=file        Use file as JSON input. The file must contain a valid JSON object.');
  Writeln('-i --json=json         Use json as JSON input. The value must contain a valid JSON object.');
  Writeln('-u --unit=name         Use name as the name of the unit.');
  Writeln('                       If no output file is specified, this sets the name');
  Writeln('-o --output=file       Set the output filename. Sets the unit name if none is given');
  Writeln('-d --generate-delphi   Use Delphi 10 JSON routines');
  Writeln('-l --generate-load     Create LoadFromJSON routine');
  Writeln('-s --generate-save     Create SaveToJSON routine');
  Writeln('-c --load-ignores-case The LoadFromJSON routine will ignore case');
  Writeln('-e --use-setter        Property setters use a routine instead of writing to field');
  Writeln('-n --classname=name    Set the name of the top-level class (default: TMyObject)');
  Writeln('-r --load-with-error   Load will raise an exception if an unknown property is met');
  Halt(Ord(Msg<>''));
end;

var
  Application: TJSON2PasApplication;
begin
  Application:=TJSON2PasApplication.Create(nil);
  Application.Title:='JSON to Pascal code generator';
  Application.Run;
  Application.Free;
end.