json $type-less to delphi TPersistent

I've studied and tested the example at

and it works perfectly well.

I've written a TOpportunity class following the above article, in order to parse JSON responses from REST API - herebelow the details of the service I'm using:
https://developer.capsulecrm.com/v2/operations/Opportunity#showOpportunity

The problem I'm facing is that the JSON received is "pure" and has no $type information inside.

If I try using TTMSFNCPersistence.LoadSettingsFromStream I get a ' "$type" property not found in Object descriptor ' error.

If I use the object class helper in WEBLib.TMSFNCTypes I can Create an empty well-formed-with-$type class instance... that stays empty when I'm assigning JSON coming from the REST service above ==> so no reading/parsing is done.

Any suggestion? Thanks in advance!

Hi,

There are various ways to work around this, but one of them is including WEBLib.TMSFNCTypes and use the JSON class helper property.

uses
  WEBLib.TMSFNCTypes;

type
  TMyObject = class
  private
    FX: string;
  published
    property X: string read FX write FX;
  end;

procedure TForm1.LoadJSON;
var
  o: TMyObject;
begin
  o := TMyObject.Create;
  o.JSON := '{"X": "Hello"}';
  console.log(o.X);
end;

Hi Pieter, thanks for your prompt reply.
Yes, I've already included WEBLib.TMSFNCTypes - but the problem is that the object remains empty.

procedure TfrmDevTest.WebHttpRequest1Response(Sender: TObject; AResponse: string);
var
  OP: CAPS.Opportunity.TOpportunity;
begin
  OP := TOpportunity.Create;
  OP.JSON := AResponse;

  WebMemo1.Text := AResponse;
  WebMemo2.Text := OP.ToJSON;
end;

TOpportunity derived from TPersistent as per your article.
Class written into CAPS.Opportunity.pas in order to use namespaces.

here the output dump (just canceled some data in the source JSON for privacy reason)

WebMemo1 at left, WeMemo2 at right.
(btw OP.Free missing in the code above)

I notice that the JSON is not a 1 on 1 match with the TOpportunity class structure. It really needs to match the JSON structure you see at the right side. Properties will be skipped, if they don't exist, but I see that the JSON data starts with "opportunity". If this is not found, the JSON will be skipped. Also, the property names need to match the name values in the JSON.

I suppose you could try parsing the response JSON, and get the "opportunity" JSON object from the left side JSON data, and then pass this as a JSON string, which will at least be a starting point to match the properties on the right side. Could be possible that you need to take care of case-sensitivity as well.

OK - I'm getting the "opportunity" value:

procedure TfrmDevTest.WebHttpRequest1Response(Sender: TObject; AResponse: string);
var
  OP: CAPS.Opportunity.Topportunity;
  JT: TJSONObject;
  JV: TJSONValue;
begin
  WebMemo1.Text := AResponse; // show incoming data - OK
  JT := TJSONObject.ParseJSONValue(AResponse) as TJSONObject;
  JV := JT.GetValue('opportunity');
  WebMemo3.Text := JV.ToString; // show opportunity object extracted - OK

  OP := Topportunity.Create;
  OP.JSON := JV.ToJSON;
  WebMemo2.Text := OP.ToJSON; // still empty... :(

Noted about case-sensitive... I've used https://jsontodelphi.com/ to write a draft of the class from incoming JSON, then manually edited and adjusted the code. Even changing the field and property names exactly matching the case (eg. FId ==> Fid) does not change having an empty result (so far I've not adjusted all the fields, just some). Does order of fields matter ?

To further help you I suppose that we are going to need a sample of some sort. Could you provide your TOpportunity class structure and a sample JSON response? The data can be fake of course. You can send your support question & data to support@tmssoftware.com with a link to this topic.

OK - sending a D10.4 project to support... thanks!

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.