JSON string to TList<>

Hi,

Please disregard/close if this is not relevant through this support forum.

But, I have a service on my xData server that returns TList< TMyClass >. The resulting json string looks like this:

{
  "value": [
    {
      "$id": 1,
      "Prop1": "Value1_01",
      "Prop2": "Value1_02"
    },
    {
      "$id": 2,
      "Prop1": "Value2_01",
      "Prop2": "Value2_02"
    }
  ]
}

Now, using/including BCL.Json (or similar), is there a way to Deserialize that json string directly back to a variable of TList<TMyClass)?

Not directly because such JSON is not a list, but an object with a property of type "value".
So you could do something like this (you have to handle memory management, this is just for simplification)

TMyResponse = class
  Value: TList<TMyClass>;
end;

Then yes, you can use Bcl.Json and deserialize it:

  MyClass := TJson.Deserialize<TMyClass>(Json);

But if this is a XData service, why do you need to deserialize it manually instead of using TXDataClient class?

Ok, thank you for the clarification and suggestion.

But if this is a XData service, why do you need to deserialize it manually instead of using TXDataClient class?

Hmm, not sure to be honest. MyClass in this example is actually a composite of some properties from 3 related Aurelius entities. The service might be used by none Delphi client in addition to my Delphi clients, so I thought this was the way to go. But I'm absolutely open for input or suggestion if there is other/better way to handle it?

Well, if it's going to be used by no Delphi client, then you can't use the Delphi deserialization anyway.

But if you have a service operation that returns some arbitrary TMyClass:

type
  IMyService = interface
    function SomeMethod: TMyClass;

Then from TXDataClient you can simply call the method and retrieve the TMyClass directly this way, the client will deserialize it for you:

MyClass := XDataClient.Service<IMyService>.SomeMethod;