Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Pkg.Json.Mapper.pas
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
unit Pkg.Json.Mapper;

interface
uses FMX.TreeView, System.JSON, Rest.Json, RTTI, RegularExpressions, TypInfo,
uses FMX.TreeView, System.JSON, System.StrUtils, Rest.Json, RTTI, RegularExpressions, TypInfo,
SysUtils, classes, Generics.Collections, Generics.Defaults;

type
Expand Down Expand Up @@ -89,6 +89,7 @@ TPkgJsonMapper = class
FClasses: TList<TStubClass>;
FRootClass: TStubClass;
FUnitName: string;
FPreserveCapitalization: Boolean;
procedure SetUnitName(const Value: string);
protected
function GetJsonType(AJsonValue: TJsonValue): TJsonType;
Expand All @@ -110,6 +111,7 @@ TPkgJsonMapper = class
// Visualizes stub class structure in a treeview
procedure Visualize(ATreeView: TTreeView; AItemStyleLookup: string);
property DestinationUnitName: string read FUnitName write SetUnitName;
property PreserveCapitalization: Boolean read FPreserveCapitalization write FPreserveCapitalization;
end;

procedure PrettyPrintJSON(JSONValue: TJSONValue; OutputStrings: TStrings; indent: integer = 0);
Expand Down Expand Up @@ -267,7 +269,7 @@ function TPkgJsonMapper.GenerateUnit: string;
LList.Add('');
LList.Add('interface');
LList.Add('');
LList.Add('uses Generics.Collections, Rest.Json;');
LList.Add('uses Generics.Collections, Rest.Json' + ifThen(FPreserveCapitalization,', Rest.Json.Types','') +';');
LList.Add('');
LList.Add('type');

Expand Down Expand Up @@ -744,6 +746,9 @@ LLines.Add(FName + ' = class');

for LItem in FItems do
begin
if FMapper.PreserveCapitalization then
LLines.Add(format(' [JsonName(''%s'')]',[LItem.PropertyName]));

LString := format(' %s: %s;', [LItem.FieldName, LItem.GetTypeAsString]);
LLines.Add(LString);
end;
Expand Down
32 changes: 28 additions & 4 deletions uMainForm.fmx
Original file line number Diff line number Diff line change
Expand Up @@ -1707,13 +1707,13 @@ object MainForm: TMainForm
Margins.Bottom = 6.000000000000000000
PopupMenu = MemoPopupMenu
Size.Width = 657.000000000000000000
Size.Height = 448.000000000000000000
Size.Height = 414.000000000000000000
Size.PlatformDefault = False
StyleLookup = 'Memo1Style1'
TabOrder = 0
OnDblClick = Memo1DblClick
Viewport.Width = 637.000000000000000000
Viewport.Height = 428.000000000000000000
Viewport.Height = 394.000000000000000000
end
object Panel3: TPanel
Align = Bottom
Expand All @@ -1722,11 +1722,11 @@ object MainForm: TMainForm
Padding.Right = 6.000000000000000000
Padding.Bottom = 6.000000000000000000
Position.X = 6.000000000000000000
Position.Y = 473.000000000000000000
Position.Y = 439.000000000000000000
Size.Width = 657.000000000000000000
Size.Height = 53.000000000000000000
Size.PlatformDefault = False
TabOrder = 1
TabOrder = 2
object btnVisualize: TButton
Align = Right
Margins.Right = 9.000000000000000000
Expand Down Expand Up @@ -1812,6 +1812,30 @@ object MainForm: TMainForm
Size.PlatformDefault = False
Text = 'Paste JSON String here:'
end
object Panel5: TPanel
Align = Bottom
Padding.Left = 6.000000000000000000
Padding.Top = 6.000000000000000000
Padding.Right = 6.000000000000000000
Padding.Bottom = 6.000000000000000000
Margins.Top = 4.000000000000000000
Position.X = 6.000000000000000000
Position.Y = 496.000000000000000000
Size.Width = 657.000000000000000000
Size.Height = 30.000000000000000000
Size.PlatformDefault = False
TabOrder = 1
object CheckBox1: TCheckBox
Align = Left
Position.X = 6.000000000000000000
Position.Y = 6.000000000000000000
Size.Width = 307.000000000000000000
Size.Height = 18.000000000000000000
Size.PlatformDefault = False
TabOrder = 2
Text = 'Preserve capitalization of words'
end
end
end
object Splitter1: TSplitter
Align = Left
Expand Down
4 changes: 4 additions & 0 deletions uMainForm.pas
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ TMainForm = class(TConstrainedForm)
Panel4: TPanel;
MenuItem8: TMenuItem;
btnGenerateUnit: TButton;
Panel5: TPanel;
CheckBox1: TCheckBox;
procedure btnVisualizeClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
Expand Down Expand Up @@ -138,6 +140,8 @@ procedure TMainForm.DisableMenuItems;

procedure TMainForm.PreviewUnitClick(Sender: TObject);
begin
jm.PreserveCapitalization := CheckBox1.IsChecked;

if tv.Count = 0 then
btnVisualizeClick(self);

Expand Down