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:

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!


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


  TMyObject = class
    FX: string;
    property X: string read FX write FX;

procedure TForm1.LoadJSON;
  o: TMyObject;
  o := TMyObject.Create;
  o.JSON := '{"X": "Hello"}';

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);
  OP: CAPS.Opportunity.TOpportunity;
  OP := TOpportunity.Create;
  OP.JSON := AResponse;

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

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);
  OP: CAPS.Opportunity.Topportunity;
  JT: TJSONObject;
  JV: TJSONValue;
  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;
  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.