Deserialize xdata response with aurelius

Hi,
I get xdata response (Object List) from xdata server (which is behind another server, so xdataclient cannot be used)
I'm trying to deserialize data but I get error Property "customer_id" does not refer to a known property in type "customerbalance.entities.Tassaldo"
Original response is

{
    "value": [
        {
            "$id": 1,
            "customer_id": 1,
            "credit_balance": 123.12,
            "invoice_balance": 0,
            "last_dateof_purchase_str": "20210501",
            "last_dateof_payment_str": "        ",
            "last_dateof_invoicing_str": "        ",
            "automatic_productdate1": null,
            "automatic_productdate2": null,
            "automatic_productdate3": null,
            "version": 0
        }
    ]
}

and class declaration is

  [ Id( 'Fcustomer_id', TIdGenerator.None ) ]
  Tassaldo = class
  private
    [ Column( 'asiakas' ) ]
    Fcustomer_id: integer;
    [ Column( 'kassasaldo' ) ]
    Fcredit_balance: nullable< double >;
    [ Column( 'laskutussaldo' ) ]
    Finvoice_balance: nullable< double >;
    [ Column( 'viimostopvm', [ ], 8 ) ]
    Flast_dateof_purchase_str: nullable< string >;
    [ Column( 'ViimSuoritusPvm', [ ], 8 ) ]
    Flast_dateof_payment_str: nullable< string >;
    [ Column( 'ViimSiirtoPvm', [ ], 8 ) ]
    Flast_dateof_invoicing_str: nullable< string >;
    [ Column( 'automtuotepvm' ) ]
    Fautomatic_productdate1: nullable< tdatetime >;
    [ Column( 'automtuotepvm2' ) ]
    Fautomatic_productdate2: nullable< tdatetime >;
    [ Column( 'automtuotepvm3' ) ]
    Fautomatic_productdate3: nullable< tdatetime >;
    [ version ]
    version: integer;
  public ....

i remove the {"value": from begining and last } by code
and try to deserialize it by

        try
          list := Deserializer.Read < TList < tassaldo >> ( s );
        finally
          Deserializer.Free;
        end;

If you are not using TXDataClient, what are you using, exactly? Hard to guess from the provided information, do you have more detailed info, code you are using, call stack, project reproducing the issue...?

So you are saying (in between lines) that it shoud work :)
I just tought that I have just forgotten something ...
I get data using normal TRestClient. So the content is in string named S there.

I acidently dropped creation of Deserializer which is
Deserializer := TAureliusJsonDeserializer.Create( TMappingExplorer.Default );

Unfortunatelly (at this point) my call stack isn't better

[0212E7D3] Bcl.Json.Baseobjectconverter.TJsonBaseObjectConverter.ReadJsonProperty
[0212EB10] Bcl.Json.Baseobjectconverter.TJsonBaseObjectConverter. (possible ReadObject+716)
[0212EB72] Bcl.Json.Baseobjectconverter.TJsonBaseObjectConverter. (possible ReadObject+814)
[0212E717] Bcl.Json.Baseobjectconverter.TJsonBaseObjectConverter.ReadJson
[02109777] Bcl.Json.Listconverter.TJsonValueListConverter.ReadList
[020E2857] Bcl.Collections.AsValueList
[02109695] Bcl.Json.Listconverter.TJsonValueListConverter.ReadJson
[0213968D] Bcl.Json.Deserializer.TJsonDeserializer.Read
[0213982C] Bcl.Json.Deserializer.TJsonDeserializer.Read
[021395DB] Bcl.Json.Deserializer.TJsonDeserializer.Read
[0213958E] Bcl.Json.Deserializer.TJsonDeserializer.Read
[01089E06] clientdataupdater.TJsonDeserializer.Read<System.Generics.Collections.TList<customerbalance.entities.Tassaldo>> (Line 132, "Bcl.Json.Deserializer.pas")
[01082384] clientdataupdater.TClientUpdater.getdata (Line 374, "clientdataupdater.pas")

if this doesn't give you any clue, i'll try to reproduce isolated demo...

While writing isolated project I noticed that I should add F to json fieldnames and it works ok. (except version field which doesn't need F). Is there option to tell TAureliusJsonDeserializer to add F before searching ?
If you still need demo it's here.
entitydemo.7z

Actually you are using TAureliusJsonDeserializer, which is a kind of legacy serializer. Use the TXDataClientDeserializer:

uses {...}, XData.Json.Deserializer;

procedure TForm4.Button1Click(Sender: TObject);
var
  Deserializer: TXDataJsonClientDeserializer;
  s: string;
  list: tlist<Tassaldo>;
  startPos: integer;
begin
  Deserializer := TXDataJsonClientDeserializer.Create(TXDataAureliusModel.Default, nil);
  try

    s := Memo1.Lines.Text;
    if s.StartsWith('{') then
    begin // let's clean value
      startPos := pos('[', s);
      delete(s, 1, startPos - 1);
      s := s.TrimEnd([#13, #10]);
      setlength(s, length(s) - 1);
      Memo1.Lines.Text := s;
    end;
    list := Deserializer.Read < tlist < Tassaldo >> (s);

    showmessage(format('Lista %d asiakas %d summa %5.2f',
      [list.count, list.First.customer_id,
      list.First.credit_balance.ValueOrDefault]));
  finally
    Deserializer.Free;
  end;

end;
1 Like

Nice. Thanks.

1 Like

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