C ++シリアル化ライブラリシリアルの使用(1)



C Serialization Library Cereal Use



シリアル紹介

Cerealこれは、カスタムデータ型のJSONXMLBinary compressionへのシリアル化をサポートするヘッドオンリーのオープンソースC ++ 11シリアル化ライブラリです。逆シリアル化も可能です。他のサードパーティライブラリ(RapidJson、RadidXml)にほとんど依存せず、便利で高速です。

ダウンロード

オープンソースプロジェクトなので、直接Github最新バージョンをダウンロードできます。



github

git clone root@xxxxx:USCiLab/cereal.git

使用する

構成

次のように、前の手順でダウンロードしたライブラリをプロジェクトパスに含めます。



INCLUDEPATH +=$$PWD/Cereal

データ型を定義する

独自のデータ型を定義し、シリアル化関数を記述します。以下は、私自身のコードでテストされたクラスです。

class QDicomTag { public: QDicomTag() ~QDicomTag() /*! Custom serialization function*/ template friend void serialize( Archive & ar, const QDicomTag &tag, std::uint32_t const &version) public: void cleanTag() bool loadTag(DcmDataset *pDataSet) bool loadTagGroup0008(DcmDataset *pDataSet) bool loadTagGroup0010(DcmDataset *pDataSet) bool loadTagGroup0020(DcmDataset *pDataSet) bool loadTagGroup0028(DcmDataset *pDataSet) std::string m_strImageType // (0008,0008) std::string m_strSopClassUID // (0008,0016) std::string m_strSopInstanceUID // (0008,0018) std::string m_strStudyDate // (0008,0020) std::string m_strSeriesDate // (0008,0021) }; template void serialize(Archive &ar, const QDicomTag &tag, std::uint32_t const &version) { (void)version ar(cereal::make_nvp('imageType', tag.m_strImageType) ,cereal::make_nvp('sopClassUID', tag.m_strSopClassUID) ,cereal::make_nvp('sopInstanceUID', tag.m_strSopInstanceUID) ) }

重要な部分は、シリアル化関数を作成することです。特定のシリアル化は、シリアル化機能によって実装されます。ここでは、コードをクリーンアップするために、シリアル化関数をクラスの外に配置しました。シリアル化機能は後で見ることができます。

シリアル化を開始します

ヘッダーファイルを含める必要があります

#include 'cereal/macros.hpp' #include 'cereal/specialize.hpp' #include 'cereal/version.hpp' #include 'cereal/access.hpp' #include 'cereal/cereal.hpp' #include 'cereal/archives/binary.hpp' #include 'cereal/archives/xml.hpp' #include 'cereal/archives/json.hpp' #include 'cereal/types/memory.hpp' #include 'cereal/types/string.hpp' #include #include #include { QString strFile = 'D:/dev/TestSerialization.json' std::ofstream os(strFile.toStdString ()) cereal::JSONOutputArchive archive(os) QDicomTag tag // .... The initialization of the tag structure is omitted here archive(cereal::make_nvp('dicomTag' ,tag)) }

シリアル化機能

シリアル化機能は次のカテゴリに分類できます。特定のクラスの構造は上記のとおりですDicomTag

クラス内で定義

  1. 単一のシリアル化機能を使用する
template void serialize(Archive &ar) { ar(m_strImageType, m_strSopClassUID, m_strStudyDate, m_strSeriesDate) }

必要に応じて、シリアル化されたフィールドを選択できます。シリアル化機能に記載されている項目がシリアル化されます。上記の書き込みは、シリアル化出力Value0.....フィールド値の後に自動的に追加されます。便宜上、独自のフィールド名を追加できます。

template void serialize(Archive &ar) { ar(cereal::make_nvp('imageType', tag.m_strImageType) ,cereal::make_nvp('sopClassUID', tag.m_strSopClassUID) ,cereal::make_nvp('sopInstanceUID', tag.m_strSopInstanceUID) ) }

特定の出力結果

  • XML
DERIVED 1.2.840.10008.5.1.4.1.1.7 1.2.156.14702.1.1005.128.2.201607141416245011010000001
  • JSON
{ 'dicomTag': { 'imageType': 'DERIVED', 'sopClassUID': '1.2.840.10008.5.1.4.1.1.7', 'sopInstanceUID': '1.2.156.14702.1.1005.128.2.201607141416245011010000001', } }
  1. 個別の保存機能とロード機能
template void save(Archive &ar) const { ar(cereal::make_nvp('imageType', tag.m_strImageType) ,cereal::make_nvp('sopClassUID', tag.m_strSopClassUID) ,cereal::make_nvp('sopInstanceUID', tag.m_strSopInstanceUID) ) } template void load(Archive &ar) const { ar(cereal::make_nvp('imageType', tag.m_strImageType) ,cereal::make_nvp('sopClassUID', tag.m_strSopClassUID) ,cereal::make_nvp('sopInstanceUID', tag.m_strSopInstanceUID) ) }

出力後の上記の関数の結果は1と同じです。

クラス外で定義

  1. 単一のシリアル化機能を使用する
class QDicomTag { public: QDicomTag() ~QDicomTag() /*! Custom serialization function*/ template friend void serialize( Archive & ar, const QDicomTag &tag, std::uint32_t const &version) }; template void serialize(Archive &ar, const QDicomTag &tag, std::uint32_t const &version) { (void)version ar(cereal::make_nvp('imageType', tag.m_strImageType) ,cereal::make_nvp('sopClassUID', tag.m_strSopClassUID) ,cereal::make_nvp('sopInstanceUID', tag.m_strSopInstanceUID) ) }
  1. 個別の保存機能とロード機能
class QDicomTag { public: QDicomTag() ~QDicomTag() /*! Custom serialization function*/ template friend void save(Archive &a, const QDicomTag &tag, std::uint32_t const &version) template friend void load(Archive &a, const QDicomTag &tag, std::uint32_t const &version) }; template void save(Archive &ar, const QDicomTag &tag, std::uint32_t const &version) { (void)version ar(cereal::make_nvp('imageType', tag.m_strImageType) ,cereal::make_nvp('sopClassUID', tag.m_strSopClassUID) ,cereal::make_nvp('sopInstanceUID', tag.m_strSopInstanceUID) ) } template void load(Archive &ar, const QDicomTag &tag, std::uint32_t const &version) { (void)version ar(cereal::make_nvp('imageType', tag.m_strImageType) ,cereal::make_nvp('sopClassUID', tag.m_strSopClassUID) ,cereal::make_nvp('sopInstanceUID', tag.m_strSopInstanceUID) ) }

その他

参考記事

  • 【シリアル公式文書】 1

Author: cobblestone Time: November 2, 2019 22:33:08 Version: V 0.0.1 Email: root@xxxxx WeChat public account: devstone Copyright: If there is no special statement in this blog, it is the original article of the author, welcome to reprint and share. However, I hope you indicate the source and leave the original address. This is the greatest respect for the author and also for knowledge.

この記事について質問がある場合は、以下にメッセージを残すか、私にメールしてください。

フォローしてください