This forum is temporaily read-only. All existing discussions remain available for reference. For new questions, bug reports or support requests, please contact us at mail@lmdsupport.com.

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'.

Thanks.


Comments

  • In the following row:

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

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

      TZzyCustomGridData = class(TObject);

      [SkipAll]
      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:

    type
      TMy<T> = class
      private
        FX: T;
        FY: T;
      public
        property X: T read FX write FX;
        property Y: T read FY write FY;
      end;

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

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

      s := TXmlSerializer.Create(xml.Node);
      try
        s['My'].Value(my);
      finally
        s.Free;
      end;

      Memo1.Lines := xml.XML;
    end;

    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:

      [SkipAll]
      TZzyCustomViewRecord = class

      private
        FCode: string;
        FId: Integer;
        FName: string;
        FRecId: string;
        FRecordIndex: Integer;
      public
        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;
      end;


      [SkipAll]
      TZzyGridDroppedData<T: TZzyCustomViewRecord> = class
         public
         DataArray : array of T;
         Caption:String;
         View: String;
      end;


      TZzyGridDroppedData<T: TZzyCustomViewRecord> = class

          property Records: TZzyGridDroppedData<T> read FGridData;


      end;

    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.

    Robert.
Sign In or Register to comment.