kbmMWを使用したJSON/XML/YAMLのオブジェクト変換

JSONやXMLといったデータ形式をDelphiクラスに自動変換し、そのクラスを通じてデータの読み書きを行う方法について説明します。kbmMWライブラリを使用することで、これを行うことができます。
たとえば、以下のようなJSONファイルから対応するDelphiクラスを生成できます。

var
   resultStr: string;
begin
   resultStr := TkbmMWJSONMarshal.GenerateDelphiClassFromUTF8File('auctions.json', 'Unit2', 'AuctionData');
このコードは、指定されたJSONファイル(`auctions.json`)を解析し、`AuctionData`という名前のクラスを含むユニットを生成します。生成されたクラスを使用すると、JSONデータを簡単に扱うことができるようになります。
次に、生成されるクラスの例を示します。

unit Unit2;

interface

uses
   Classes,
   Generics.Collections,
   kbmMWRTTI,
   kbmMWObjectMarshal,
   kbmMWDateTime,
   kbmMWNullable;

type
   TAuctionData = class;
   TRealmInfo = class;
   TFactionAuctions = class;
   TSingleAuction = class;

   [kbmMW_Root('TAuctionData', [mwrfIncludeOnlyTagged])]
   TAuctionData = class
   private
      FRealm: TRealmInfo;
      FAlliance: TFactionAuctions;
      FHorde: TFactionAuctions;
      FNeutral: TFactionAuctions;
   protected
      procedure SetRealm(const Value: TRealmInfo);
      procedure SetAlliance(const Value: TFactionAuctions);
      procedure SetHorde(const Value: TFactionAuctions);
      procedure SetNeutral(const Value: TFactionAuctions);
   public
      destructor Destroy; override;
      property Realm: TRealmInfo read FRealm write SetRealm;
      property Alliance: TFactionAuctions read FAlliance write SetAlliance;
      property Horde: TFactionAuctions read FHorde write SetHorde;
      property Neutral: TFactionAuctions read FNeutral write SetNeutral;
   end;

   [kbmMW_Root('TRealmInfo', [mwrfIncludeOnlyTagged])]
   TRealmInfo = class
   private
      FName: string;
      FSlug: string;
   public
      property Name: string read FName write FName;
      property Slug: string read FSlug write FSlug;
   end;

   [kbmMW_Child('Auctions', [mwcfFlatten])]
   TAuctionList = class(TObjectList<TSingleAuction>);
   [kbmMW_Root('TSingleAuction', [mwrfIncludeOnlyTagged])]
   TSingleAuction = class
   private
      FAucId: Int64;
      FItemId: Int64;
      FOwner: string;
      FBid: Int64;
      FBuyout: Int64;
      FQuantity: Int64;
      FTimeLeft: string;
   public
      property AucId: Int64 read FAucId write FAucId;
      property ItemId: Int64 read FItemId write FItemId;
      property Owner: string read FOwner write FOwner;
      property Bid: Int64 read FBid write FBid;
      property Buyout: Int64 read FBuyout write FBuyout;
      property Quantity: Int64 read FQuantity write FQuantity;
      property TimeLeft: string read FTimeLeft write FTimeLeft;
   end;

implementation

procedure TAuctionData.SetRealm(const Value: TRealmInfo);
begin
   if Assigned(FRealm) then
      FRealm.Free;
   FRealm := Value;
end;

// 他のsetterメソッドも同様に実装されます。

destructor TAuctionData.Destroy;
begin
   FRealm.Free;
   FAlliance.Free;
   FHorde.Free;
   FNeutral.Free;
   inherited;
end;

initialization
   kbmMWRegisterKnownClasses([TAuctionData, TRealmInfo, TFactionAuctions, TSingleAuction]);

end.
生成されたクラスを使用してJSONファイルを読み込むには、以下のコードを使用します。

procedure TForm1.Button1Click(Sender: TObject);
var
   marshaller: TkbmMWCustomRTTIMarshal;
   auctionObj: TAuctionData;
begin
   marshaller := TkbmMWJSONMarshal.Create;
   try
      TkbmMWJSONMarshal(marshaller).AnonymousRoot := True;
      auctionObj := TkbmMWJSONMarshal(marshaller).ValueFromUTF8File<TAuctionData>('auctions.json');
   finally
      auctionObj.Free;
      marshaller.Free;
   end;
end;
同様に、XMLデータを処理する場合も、次のステップを踏みます。

var
   xmlResult: string;
begin
   xmlResult := TkbmMWXMLMarshal.GenerateDelphiClassFromFile('courses.xml', 'Unit3', 'CourseData');
これにより、XML構造に基づいたDelphiクラスが生成されます。生成されたクラスを使ってXMLデータを読み書きする例を以下に示します。

procedure TForm1.Button2Click(Sender: TObject);
var
   marshaller: TkbmMWCustomRTTIMarshal;
   courseObj: TCourseData;
   xmlString: string;
begin
   marshaller := TkbmMWXMLMarshal.Create;
   try
      courseObj := TkbmMWXMLMarshal(marshaller).ValueFromFile<TCourseData>('courses.xml');
      xmlString := TkbmMWXMLMarshal(marshaller).ValueToString(courseObj);
   finally
      courseObj.Free;
      marshaller.Free;
   end;
end;

タグ: kbmMW Delphi JSON/XML Conversion

5月26日 09:51 投稿