Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json Output and Arrays #14

Open
philhutch50 opened this issue Jun 30, 2020 · 4 comments
Open

Json Output and Arrays #14

philhutch50 opened this issue Jun 30, 2020 · 4 comments

Comments

@philhutch50
Copy link

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;
[GenericListReflect]
FLinks: TObjectList;
FList_Id: string;
FLocation: TLocationDTO;
FMember_Rating: Integer;
FMerge_Fields: TMerge_FieldsDTO;
FSource: string;
FStats: TStatsDTO;
FStatus: string;
[JSONName('tags')]
FTagsArray: TArray;
[GenericListReflect]
FTags: TObjectList;
FTags_Count: Integer;
FTimestamp_Opt: string;
FTimestamp_Signup: string;
FUnique_Email_Id: string;
FVip: Boolean;
FWeb_Id: Integer;
function GetTags: TObjectList;
function GetLinks: TObjectList;
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 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 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;
[GenericListReflect]
FMembers: TObjectList;
FTotal_Items: Integer;
function GetMembers: TObjectList;
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 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;
begin
if not Assigned(FTags) then
begin
FTags := TObjectList.Create;
FTags.AddRange(FTagsArray);
end;
Result := FTags;
end;

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

{ TRootDTO }

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

function TRootDTO.GetMembers: TObjectList;
begin
if not Assigned(FMembers) then
begin
FMembers := TObjectList.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

@marlonnardi
Copy link

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

@philhutch50
Copy link
Author

@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?

@JensBorrisholt
Copy link

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

@philhutch50
Copy link
Author

philhutch50 commented Jul 13, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants