Delphi-JsonToDelphiClass icon indicating copy to clipboard operation
Delphi-JsonToDelphiClass copied to clipboard

Json Output and Arrays

Open philhutch50 opened this issue 4 years ago • 4 comments

Nice work :-)

I am not sure if its your original or the branch done by JensBorrisholt that is causing me issues however.

I create the master and put the content in, but if I then try to turn the content back to JSON it crashes with exceptions. The issues appears to be where certain object arrays dont have any content?

The unit it created is below.. `` unit mchimp;

interface

uses Pkg.Json.DTO, System.Generics.Collections, REST.Json.Types;

{$M+}

type TLinksDTO_001 = class private FHref: string; FMethod: string; FRel: string; FSchema: string; FTargetSchema: string; published property Href: string read FHref write FHref; property Method: string read FMethod write FMethod; property Rel: string read FRel write FRel; property Schema: string read FSchema write FSchema; property TargetSchema: string read FTargetSchema write FTargetSchema; end;

TLinksDTO = class private FHref: string; FMethod: string; FRel: string; FTargetSchema: string; published property Href: string read FHref write FHref; property Method: string read FMethod write FMethod; property Rel: string read FRel write FRel; property TargetSchema: string read FTargetSchema write FTargetSchema; end;

TTagsDTO = class private FId: Integer; FName: string; published property Id: Integer read FId write FId; property Name: string read FName write FName; end;

TLocationDTO = class private FCountry_Code: string; FDstoff: Integer; FGmtoff: Integer; FLatitude: Integer; FLongitude: Integer; FTimezone: string; published property Country_Code: string read FCountry_Code write FCountry_Code; property Dstoff: Integer read FDstoff write FDstoff; property Gmtoff: Integer read FGmtoff write FGmtoff; property Latitude: Integer read FLatitude write FLatitude; property Longitude: Integer read FLongitude write FLongitude; property Timezone: string read FTimezone write FTimezone; end;

TStatsDTO = class private FAvg_Click_Rate: Integer; FAvg_Open_Rate: Integer; published property Avg_Click_Rate: Integer read FAvg_Click_Rate write FAvg_Click_Rate; property Avg_Open_Rate: Integer read FAvg_Open_Rate write FAvg_Open_Rate; end;

TMerge_FieldsDTO = class private FADDRESS: string; FBIRTHDAY: string; FFNAME: string; FLNAME: string; FMMERGE6: string; FPHONE: string; published property ADDRESS: string read FADDRESS write FADDRESS; property BIRTHDAY: string read FBIRTHDAY write FBIRTHDAY; property FNAME: string read FFNAME write FFNAME; property LNAME: string read FLNAME write FLNAME; property MMERGE6: string read FMMERGE6 write FMMERGE6; property PHONE: string read FPHONE write FPHONE; end;

TMembersDTO = class private FEmail_Address: string; FEmail_Client: string; FEmail_Type: string; FId: string; FIp_Opt: string; FIp_Signup: string; FLanguage: string; FLast_Changed: string; [JSONName('_links')] FLinksArray: TArray<TLinksDTO>; [GenericListReflect] FLinks: TObjectList<TLinksDTO>; FList_Id: string; FLocation: TLocationDTO; FMember_Rating: Integer; FMerge_Fields: TMerge_FieldsDTO; FSource: string; FStats: TStatsDTO; FStatus: string; [JSONName('tags')] FTagsArray: TArray<TTagsDTO>; [GenericListReflect] FTags: TObjectList<TTagsDTO>; FTags_Count: Integer; FTimestamp_Opt: string; FTimestamp_Signup: string; FUnique_Email_Id: string; FVip: Boolean; FWeb_Id: Integer; function GetTags: TObjectList<TTagsDTO>; function GetLinks: TObjectList<TLinksDTO>; published property Email_Address: string read FEmail_Address write FEmail_Address; property Email_Client: string read FEmail_Client write FEmail_Client; property Email_Type: string read FEmail_Type write FEmail_Type; property Id: string read FId write FId; property Ip_Opt: string read FIp_Opt write FIp_Opt; property Ip_Signup: string read FIp_Signup write FIp_Signup; property Language: string read FLanguage write FLanguage; property Last_Changed: string read FLast_Changed write FLast_Changed; property Links: TObjectList<TLinksDTO> read GetLinks; property List_Id: string read FList_Id write FList_Id; property Location: TLocationDTO read FLocation write FLocation; property Member_Rating: Integer read FMember_Rating write FMember_Rating; property Merge_Fields: TMerge_FieldsDTO read FMerge_Fields write FMerge_Fields; property Source: string read FSource write FSource; property Stats: TStatsDTO read FStats write FStats; property Status: string read FStatus write FStatus; property Tags: TObjectList<TTagsDTO> read GetTags; property Tags_Count: Integer read FTags_Count write FTags_Count; property Timestamp_Opt: string read FTimestamp_Opt write FTimestamp_Opt; property Timestamp_Signup: string read FTimestamp_Signup write FTimestamp_Signup; property Unique_Email_Id: string read FUnique_Email_Id write FUnique_Email_Id; property Vip: Boolean read FVip write FVip; property Web_Id: Integer read FWeb_Id write FWeb_Id; public constructor Create; destructor Destroy; override; end;

TRootDTO = class(TJsonDTO) private [JSONName('_links')] FLinksArray: TArray<TLinksDTO_001>; (* TLinksDTO_001 ) [GenericListReflect] FLinks: TObjectList<TLinksDTO_001>; ( TLinksDTO_001 ) FList_Id: string; [JSONName('members')] FMembersArray: TArray<TMembersDTO>; [GenericListReflect] FMembers: TObjectList<TMembersDTO>; FTotal_Items: Integer; function GetMembers: TObjectList<TMembersDTO>; function GetLinks: TObjectList<TLinksDTO_001>; ( TLinksDTO_001 ) published property Links: TObjectList<TLinksDTO_001> read GetLinks; ( TLinksDTO_001 *) property List_Id: string read FList_Id write FList_Id; property Members: TObjectList<TMembersDTO> read GetMembers; property Total_Items: Integer read FTotal_Items write FTotal_Items; destructor Destroy; override; end;

implementation

{ TMembersDTO }

constructor TMembersDTO.Create; begin inherited; FMerge_Fields := TMerge_FieldsDTO.Create; FStats := TStatsDTO.Create; FLocation := TLocationDTO.Create; end;

destructor TMembersDTO.Destroy; begin FMerge_Fields.Free; FStats.Free; FLocation.Free; GetTags.Free; GetLinks.Free; inherited; end;

function TMembersDTO.GetTags: TObjectList<TTagsDTO>; begin if not Assigned(FTags) then begin FTags := TObjectList<TTagsDTO>.Create; FTags.AddRange(FTagsArray); end; Result := FTags; end;

function TMembersDTO.GetLinks: TObjectList<TLinksDTO>; begin if not Assigned(FLinks) then begin FLinks := TObjectList<TLinksDTO>.Create; FLinks.AddRange(FLinksArray); end; Result := FLinks; end;

{ TRootDTO }

destructor TRootDTO.Destroy; begin GetMembers.Free; GetLinks.Free; inherited; end;

function TRootDTO.GetMembers: TObjectList<TMembersDTO>; begin if not Assigned(FMembers) then begin FMembers := TObjectList<TMembersDTO>.Create; FMembers.AddRange(FMembersArray); end; Result := FMembers; end;

function TRootDTO.GetLinks: TObjectList<TLinksDTO_001>; (* TLinksDTO_001 ) begin if not Assigned(FLinks) then begin FLinks := TObjectList<TLinksDTO_001>.Create; ( TLinksDTO_001 *) FLinks.AddRange(FLinksArray); end; Result := FLinks; end;

end. `` Test Code

`` var response_string:string; MC: TRootDTO;

tg:extended; tgn:integer; diag:system.Text; tag:TTagsDTO; linkbase:TLinksDTO_001; linkmc:TLinksDTO;

begin restrequest1.Params[0].value:='0'; restrequest1.Params[1].Value:='1'; restrequest1.Params[2].Destroy; RestRequest1.Execute; response_string:=RestResponse1.Content; assignfile(diag,'c:\owlbarn\mchimpupdate.json'); rewrite(diag); writeln(diag,response_string); writeln(diag,'{ EMPTY PAD }');

MC:=TRootDTO.Create; linkbase:=TlinksDTO_001.Create; linkbase.href:=' '; linkbase.Method:=' '; linkbase.Rel:=' '; linkbase.Schema:=' '; linkbase.TargetSchema:=' '; *) MC.AsJson:=response_string; MC.Members[0].Links.create(false); linkmc:=TLinksDTO.Create; linkmc.Href:=''; linkmc.Method:='';

tag:=TTagsDTO.Create; tag.Id:=123456; tag.Name:='12121212112'; mc.Members[0].Tags.create(false); mc.Links.Create(false); mc.links.Add(linkbase); mc.Members[0].Tags.add(tag); mc.Members[0].Links.add(linkmc);

response_string:=MC.asjson; writeln(diag,response_string); closefile(diag); tag.Destroy; MC.destroy; linkbase.Destroy; linkmc.Destroy; showmessage('done');

end; `` So as long as create the Link classes and put dummy content in it all works, but otherwise it fails.

I am try to add the tags object array which works but creates a duplicate entry in the JSON

"tags": [], "tags": [ { "id": 123456, "name": "12121212112" } ],

If you cannot help please say so and just close the issue.

I dont seem to be able to raise any issue with Jens.

Thanks in advance Phil

philhutch50 avatar Jun 30 '20 11:06 philhutch50

try the same case in https://jsontodelphi.com

marlonnardi avatar Jul 08 '20 15:07 marlonnardi

@marlonnardi This generates the same code as above so the same possible issue exists that it will generate double tags as above.

I can now add fields but have found that when it takes this back to json any array that has no data causes it to crash even if when extracted I can see data there?

philhutch50 avatar Jul 08 '20 15:07 philhutch50

@philhutch50 send med the original JSON to my mail address [email protected] and I'll look at it

JensBorrisholt avatar Jul 12 '20 13:07 JensBorrisholt

have sent it many thanks!

On Sun, Jul 12, 2020 at 2:38 PM Jens Borrisholt [email protected] wrote:

@philhutch50 https://github.com/philhutch50 send med the original JSON to my mail address [email protected] and I'll look at it

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/PKGeorgiev/Delphi-JsonToDelphiClass/issues/14#issuecomment-657223236, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKN7ZMDULRK2LYVJAVYVTW3R3G4HDANCNFSM4OMDUC7A .

philhutch50 avatar Jul 13 '20 10:07 philhutch50