Skip to content

Commit fd4b261

Browse files
Jens BorrisholtJens Borrisholt
Jens Borrisholt
authored and
Jens Borrisholt
committed
Merge branch 'Development'
2 parents 8841a4f + b22fc8f commit fd4b261

File tree

6 files changed

+51
-31
lines changed

6 files changed

+51
-31
lines changed

Demo Data/NullProperty.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"createdAt": null,
4+
"updatedAt": "2013-05-28T15:47:57.962Z",
5+
"username": "jacquelyn88"
6+
}
7+
]

Generator GUI/JsonToDelphiClass.delphilsp.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

Lib/Pkg.Json.JsonValueHelper.pas

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ class function TJsonValueHelper.GetJsonType(aJsonValue: TJsonValue): TJsonType;
2626
b: Boolean;
2727
d: Double;
2828
E: Extended;
29+
DT: TdateTime;
2930
begin
3031
if aJsonValue = nil then
3132
exit(jtObject);
3233

34+
if aJsonValue is TJSONNull then
35+
exit(jtUnknown);
36+
3337
if aJsonValue is TJSONObject then
3438
exit(jtObject);
3539

@@ -56,12 +60,8 @@ class function TJsonValueHelper.GetJsonType(aJsonValue: TJsonValue): TJsonType;
5660
begin
5761
Value := aJsonValue.AsType<string>;
5862

59-
try
60-
ISO8601ToDate(Value);
63+
if TryISO8601ToDate(Value, DT) then
6164
exit(jtDateTime);
62-
except
63-
64-
end;
6565

6666
if TryStrToFloat(Value, E) then
6767
Result := jtString

Lib/Pkg.Json.Mapper.pas

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,13 @@ procedure TPkgJsonMapper.ProcessJsonObject(aJsonValue: TJsonValue; aParentClass:
6666
if aJsonValue = nil then
6767
exit;
6868

69-
if not(aJsonValue is TJSONObject) then
70-
begin
71-
JsonType := GetJsonType(aJsonValue);
72-
73-
var Name := (aJsonValue as TJsonString).Value;
74-
75-
if Name = '' then
76-
Name := 'Element';
77-
78-
TStubField.Create(aParentClass, Name, JsonType);
79-
exit;
80-
end;
81-
8269
JSONObject := aJsonValue as TJSONObject;
8370
for JsonPair in JSONObject do
8471
begin
8572
JSONValue := JsonPair.JSONValue;
8673
JsonType := GetJsonType(JSONValue);
8774

8875
case JsonType of
89-
jtUnknown:
90-
{ do nothing };
9176
jtObject:
9277
begin
9378
StubClass := TStubClass.Construct(aParentClass, JsonPair.JsonString.Value, Self.FStubClasses);

Lib/Pkg.Json.StubField.pas

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ TStubField = class(TJsonName)
2222
FName: string;
2323
strict protected
2424
procedure SetName(const Value: string); override;
25-
class function GetTypeAsString(AType: TJsonType): string; overload;
25+
class function GetTypeAsString(aJsonType: TJsonType): string; overload;
2626
function GetTypeAsString: string; overload; virtual;
2727
public
2828
function IsArrayField: Boolean;
@@ -378,9 +378,6 @@ Lines.Add(Name + ' = class' + IfThen(BaseClass = '', '', '(' + BaseClass + '
378378

379379
for StubField in FItems do
380380
begin
381-
if (StubField.FieldType = jtUnknown) or ((StubField is TStubContainerField) and ((StubField as TStubContainerField).ContainedType = jtUnknown)) then
382-
raise EJsonMapper.CreateFmt('The property [%s] has unknown type!', [StubField.PropertyName]);
383-
384381
if StubField.IsArrayField then
385382
begin
386383
StubArrayField := StubField as TStubArrayField;
@@ -443,11 +440,11 @@ constructor TStubField.Create(aParentClass: TStubClass; aItemName: string; aFiel
443440
FParentClass.Items.Add(Self);
444441
end;
445442

446-
class function TStubField.GetTypeAsString(AType: TJsonType): string;
443+
class function TStubField.GetTypeAsString(aJsonType: TJsonType): string;
447444
begin
448-
case AType of
445+
case aJsonType of
449446
jtUnknown:
450-
Result := 'Unknown';
447+
Result := 'string';
451448
jtString:
452449
Result := 'string';
453450
jtTrue, jtFalse:

README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
Delphi-JsonToDelphiClass
22
========================
33

4+
## Fixes & Features: 16th June 2024 ##
5+
### Features ###
6+
* JSON null property are now mapped to a string.
7+
8+
Eg this JSON
9+
```json
10+
{
11+
{
12+
"createdAt": null,
13+
"updatedAt": "2013-05-28T15:47:57.962Z",
14+
"username": "jacquelyn88"
15+
}
16+
}
17+
```
18+
19+
Generates the following DTO:
20+
```pascal
21+
TItems = class
22+
private
23+
FCreatedAt: string;
24+
[SuppressZero]
25+
FUpdatedAt: TDateTime;
26+
FUsername: string;
27+
published
28+
property CreatedAt: string read FCreatedAt write FCreatedAt;
29+
property UpdatedAt: TDateTime read FUpdatedAt write FUpdatedAt;
30+
property Username: string read FUsername write FUsername;
31+
end;
32+
```
33+
34+
435
## Fixes & Features: 04th February 2024 ##
536
### Features ###
637
* Added a Demo, using and authenticated endpoint
@@ -26,7 +57,7 @@ Thanks to [DummyJSON](https://dummyjson.com/) for providing this service.
2657
* Added Clone function on TJsonDTO class
2758

2859
### Bugs: ###
29-
* Unittest TestDateTime didnt pass under Delphi 12
60+
* Unittest TestDateTime didn't pass under Delphi 12
3061
* Added missing Reserved words
3162

3263
## Fixes & Features: 13th January 2024 ##
@@ -163,7 +194,7 @@ E.g this JSON generated faulty code:
163194
* Allways use JsonName property annotation (Setting)
164195
* Support for objects with diffrents properties in an Array
165196

166-
Eg this JSON
197+
Eg this JSON
167198
```json
168199
{
169200
"ArrayTest":[
@@ -172,7 +203,7 @@ Eg this JSON
172203
},
173204
{
174205
"S2":"True"
175-
}
206+
}
176207
]
177208
}
178209
```

0 commit comments

Comments
 (0)