#1 2019-01-11 17:45:37

ertank
Member
Registered: 2016-03-16
Posts: 163

TJSONRecordTextDefinition.Create error text definition is not accurate

Hello,

I started to define text representations for all json record types in my project because service started to add new fields that are for service internal use (not needed for my application), but cause json deserialize to fail.

I failed to define a test representation for my following TSearchCardResult record below:

  TSubReturn = packed record
    code: Integer;
    desc: string;
  end;

  TReturn = packed record
    code: Integer;
    desc: string;
    subReturn: TSubReturn;
  end;

  TInstruction = packed record
    InstructionId: Int64;
    Amount: string;
    InsertionDate: string;
    Description: string;
  end;
  TInstructions = TArray<TInstruction>;

  TSearchCardResult = packed record
    ret: TReturn;
    cardStatus: Integer;
    message: string;
    instructions: TInstructions;
  end;

I tried to use following test representation:

const
  __TSubReturn = 'code Integer desc string';
  __TReturn = 'code Integer desc string subReturn{code Integer desc string}';
  __TInstruction = 'InstructionId Int64 Amount string InsertionDate string Description string';
  __TSearchCardResult = 'ret TReturn cardStatus Integer message string instructions TInstruction';
  // I also tried below that raise same exception for me
  //__TSearchCardResult = 'ret{code Integer desc string subReturn{code Integer desc string}} cardStatus Integer message string instructions {InstructionId Int64 Amount string InsertionDate string Description string}';

And tried to register it as following in OnCreate() event of the form they are used:

  TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TSubReturn),         __TSubReturn).Options        := [soReadIgnoreUnknownFields];
  TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TReturn),            __TReturn).Options           := [soReadIgnoreUnknownFields];
  TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TInstruction),       __TInstruction).Options      := [soReadIgnoreUnknownFields];
  TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TSearchCardResult),  __TSearchCardResult).Options := [soReadIgnoreUnknownFields];

When I run the application I get subject exception displayed. Says RTTI size is 28 bytes but text definition is 44 bytes.

I do not know where I made a mistake in the text representation.

Any help is appreciated.

Thanks & regards,
Ertan

Offline

#2 2019-01-11 18:06:42

ertank
Member
Registered: 2016-03-16
Posts: 163

Re: TJSONRecordTextDefinition.Create error text definition is not accurate

It turned out that I did not read whole section and missed information on how to define "array of record" part.

Defining text representation as following seems to work OK:

__TSearchCardResult = 'ret{code Integer desc string subReturn{code Integer desc string}} cardStatus Integer message string instructions[InstructionId Int64 Amount string InsertionDate string Description string]';

Sorry for the noise.

Offline

Board footer

Powered by FluxBB