TkbmMWXMLMarshal doesn’t process object based on class(TObjectList)

Home Forums kbmMW TkbmMWXMLMarshal doesn’t process object based on class(TObjectList)

Tagged: 

Viewing 3 reply threads
  • Author
    Posts
    • #55113
      VadimMest
      Participant

      Hello

      TkbmMWXMLMarshal doesn’t process all properties in object based on class(TObjectList<TItem>)

      I need to create xml file with additional property Total for  node with “sequence” Item

      <?xml version=”1.0″ encoding=”utf-8″ ?>
      <Items>
      <Item>
      <Quantity>10</Quantity>
      <Price>10</Price>
      </Item>
      <Item>
      <Quantity>20</Quantity>
      <Price>20</Price>
      </Item>

      <Total>
      <TotalWithVat>500</TotalWithVat>
      <Vat>100</<Vat>
      </Total>

      </Items>

      But TkbmMWXMLMarshal  skips property Total.

      How do I need to describe class TItems for full xml?

      All code:

      unit Unit1;

      interface

      uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, kbmMWDateTime, kbmMWXML,
      kbmMWXMLMarshal,
      kbmMWObjectMarshal, kbmMWRTTI, kbmMWNullable, Generics.Collections;

      type

      TTotal = class
      private
      FTotalWithVat: currency;
      FVat: currency;
      public
      property TotalWithVat: currency read FTotalWithVat write FTotalWithVat;
      property Vat: currency read FVat write FVat;
      end;

      TItem = class
      private
      FQuantity: currency;
      FPrice: currency;
      public
      property Quantity: currency read FQuantity write FQuantity;
      property Price: currency read FPrice write FPrice;
      end;

      [kbmMW_Root(‘Items’, [])]
      [kbmMW_Child(‘Item’)]

      TItems = class(TObjectList<TItem>)
      private
      FTotal: TTotal;
      public
      constructor Create; virtual;
      destructor Destroy; override;

      // [kbmMW_Element]
      property Total: TTotal read FTotal write FTotal;
      end;

      TForm1 = class(TForm)
      procedure FormCreate(Sender: TObject);
      private
      { Private declarations }
      public
      { Public declarations }
      end;

      var
      Form1: TForm1;

      implementation

      {$R *.dfm}
      { TTableItems }

      constructor TItems.Create;
      begin
      Inherited Create;
      FTotal := TTotal.Create;
      end;

      destructor TItems.Destroy;
      begin
      FreeAndNil(FTotal);
      inherited;
      end;

      procedure TForm1.FormCreate(Sender: TObject);
      var
      Items: TItems;
      Item: TItem;
      xml: TkbmMWDOMXML;
      mx: TkbmMWXMLMarshal;
      begin
      kbmMWRegisterKnownClasses([TTableItems, TItem]);

      Items := TItems.Create;
      Item := TItem.Create;
      Item.Quantity := 10;
      Item.Price := 10;
      Items.Add(Item);
      Item := TItem.Create;
      Item.Quantity := 20;
      Item.Price := 20;
      Items.Add(Item);

      Items.Total.TotalWithVat:= 500;
      Items.Total.Vat := 100;

      mx := TkbmMWXMLMarshal.Create;
      mx.Typed := False;
      ShowMessage(mx.AsString(Items));
      mx.Free;

      Items.Free;
      end;

      end.

       

    • #55129
      kimbomadsen
      Keymaster

      Hi,

      kbmMW do not support that in the current source, however the fix is easy.

      Open kbmMWObjectMarshal.pas

      Locate the line

      if mrt=mwrmrtObject then

      at line (approx.) 1078 and 1349 and change it to:

      if true then

      In the kbmMW source the code covered by that condition will be moved after the next code block, to ensure the local properties/fields are listed _after_ the sequenced elements. Doing the above fix only will result in the Total line appearing before the sequenced elements.

      Please report back your result so I can include the change in the next release.

    • #55153
      VadimMest
      Participant

      Dear, Kim

      I’ve changed it. I replaced with mrt=mwrmrtObject and move this block under  “if mrt=mwrmrtCollection” block in two functions (Marshal and UnMarshal) and It did work.

      Thanks!

       

       

      • This reply was modified 5 years, 2 months ago by VadimMest.
    • #55167
      VadimMest
      Participant

      Dear Kim

      This changing  breaks down function Unmarshal for some JSON

       

      • #55173
        kimbomadsen
        Keymaster

        Hi,

        Can you please paste a sample that breaks with the change?

        best regards

        Kim/C4D

Viewing 3 reply threads
  • You must be logged in to reply to this topic.