-
Notifications
You must be signed in to change notification settings - Fork 27
/
Pkg.Json.JSONName.pas
146 lines (118 loc) · 2.97 KB
/
Pkg.Json.JSONName.pas
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
unit Pkg.Json.JSONName;
interface
uses
Pkg.Json.Settings;
{$M+}
type
TJSONName = class
strict private
FJsonName: string;
FDelphiName: string;
FNeedsAttribute: Boolean;
FName: string;
FPureClassName: string;
FSettings: TSettings;
protected
procedure SetName(const Value: string); virtual;
published
property JSONName: string read FJsonName;
property DelphiName: string read FDelphiName;
property NeedsAttribute: Boolean read FNeedsAttribute;
property PureClassName: string read FPureClassName write FPureClassName;
property Name: string read FName write SetName;
public
constructor Create(aItemName: string); reintroduce;
function NameAttribute: string;
class function CapitalizeFirst(Value: string): string;
end;
implementation
uses
System.Classes, System.Sysutils, System.Character;
{ TJSONName }
class function TJSONName.CapitalizeFirst(Value: string): string;
var
List: TStringList;
s: string;
i: Integer;
begin
if Value.Substring(1, 4) = 'name' then
Value := Value[1] + 'Name' + Value.Substring(4);
if Value.EndsWith('Test', True) then
begin
i := Value.Length - 4;
Value := Value.Substring(0, i) + 'Test';
end;
if Value.EndsWith('Id', True) then
begin
i := Value.Length - 2;
Value := Value.Substring(0, i) + 'Id';
end;
List := TStringList.Create;
try
ExtractStrings(['_'], [], PChar(Value), List);
if List.Count = 0 then
Exit('');
for i := 0 to List.Count - 1 do
begin
s := List[i];
if s.StartsWith('&') then
s[2] := s.ToUpper[2]
else
s[1] := s.ToUpper[1];
List[i] := s;
end;
if not TSettings.Instance.UsePascalCase then
begin
List.Delimiter := '_';
Exit(List.DelimitedText);
end;
with TStringBuilder.Create do
try
for s in List do
Append(s);
Result := ToString(True);
finally
free;
end;
finally
List.free;
end;
end;
constructor TJSONName.Create(aItemName: string);
var
s: string;
ch: Char;
begin
inherited Create;
FSettings := TSettings.Instance;
if aItemName.IsEmpty then
raise Exception.Create('aItemName can not be empty');
FNeedsAttribute := False;
FJsonName := aItemName;
for ch in FJsonName do
if ch.IsLetterOrDigit then
s := s + ch
else
s := s + '_';
if s.StartsWith('_') then
s := s.Substring(1);
FDelphiName := CapitalizeFirst(s);
if FDelphiName = '' then
FDelphiName := 'Property';
if not FDelphiName[1].IsLetter then
FDelphiName := '_' + FDelphiName;
if TSettings.Instance.AddJsonPropertyAttributes then
FNeedsAttribute := True
else
FNeedsAttribute := not SameText(FDelphiName, FJsonName);
end;
function TJSONName.NameAttribute: string;
begin
Exit('JSONName(' + AnsiQuotedStr(FJsonName, #39) + ')');
end;
procedure TJSONName.SetName(const Value: string);
begin
FPureClassName := Value;
FName := 'T' + FPureClassName + TSettings.GetPostFix;
end;
end.