Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Deserializing Generic Classes

Does anyone know if the following should work??

  jsonVal  := TJSONObject.ParseJsonValue(AList.Text);
  jsonDeSer := TJsonDeserializer.Create(@jsonVal);
  FGridData := jsonDeSer.Value<TZzyGridDroppedData<T>>;  // Problem line

The above fails to deserialize the class of <T> with an error raised 'invalid state'.



  • 7 Comments sorted by Votes Date Added
  • In the following row:

    FGridData := jsonDeSer.Value<TZzyGridDroppedData<T>>

    what is "T"?
  • Hi Eugene, In this case <T> is declared as:

      TZzyCustomGridData = class(TObject);

      TZzyGridDroppedData<T: TZzyCustomGridData> = class

    Thanks, Robert.

    • Add to Phrasebook
      • No word lists for English -> German...
      • Create a new word list...
    • Copy
  • I've checked on the following example:

      TMy<T> = class
        FX: T;
        FY: T;
        property X: T read FX write FX;
        property Y: T read FY write FY;

    procedure TForm2.Button1Click(Sender: TObject);
      my:  TMy<Integer>;
      xml: IXMLDocument;
      s:   TXmlSerializer;
      my := TMy<Integer>.Create;

      xml         := TXMLDocument.Create(nil);
      xml.Options := xml.Options + [doNodeAutoIndent];
      xml.Active  := True;

      s := TXmlSerializer.Create(xml.Node);

      Memo1.Lines := xml.XML;

    And everything worked fine. So, I cannot reproduce the issue. Can you, please, isolate problematic code into small demo project and sent it to our support email?..
  • Hi Eugene,
    You example is quite different to mine and I find it difficult to make a comparison. I use the json serializer. Here is a snippet of my code:

      TZzyCustomViewRecord = class

        FCode: string;
        FId: Integer;
        FName: string;
        FRecId: string;
        FRecordIndex: Integer;
        property Code: string read FCode write FCode;
        property Id: Integer read FId write FId;
        property Name: string read FName write FName;
        property RecId: string read FRecId write FRecId;
        property RecordIndex: Integer read FRecordIndex write FRecordIndex;

      TZzyGridDroppedData<T: TZzyCustomViewRecord> = class
         DataArray : array of T;
         View: String;

      TZzyGridDroppedData<T: TZzyCustomViewRecord> = class

          property Records: TZzyGridDroppedData<T> read FGridData;


    I think you will find that if you try this you may get the error. I say may because I have had to remove all generics from my classes regarding the other issue I reported. At this point I have lost a lot of time so I need to proceed with what I have rather than going backwards. However, that being said, it would be really good to have an answer to this for future use. If I get the chance I wou

    • Add to Phrasebook
      • No word lists for English -> German...
      • Create a new word list...
    • Copy
  • Unfortunately, I can't say anything without isolated compilable demo, as was requested before.
  • Hi Eugene,
    If it;s okay with you I would like to keep this ticket open. I will send you a demo of my issue within the next 2 weeks. 

    Thanks for your help.

Sign In or Register to comment.