From a1e27903f9a4f981ec15d360b659bb697e17bbac Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Thu, 31 Aug 2023 15:41:48 -0400 Subject: [PATCH] Add support for a MessagePack variant to CJSON Signed-off-by: Geoff Hutchison --- avogadro/io/cjsonformat.cpp | 13 +++++++++++-- avogadro/io/cjsonformat.h | 5 +++-- avogadro/io/cmsgpackformat.cpp | 18 +++++++++++++++++- avogadro/io/cmsgpackformat.h | 3 +++ avogadro/io/fileformatmanager.cpp | 2 ++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/avogadro/io/cjsonformat.cpp b/avogadro/io/cjsonformat.cpp index a3a5b07ca6..c5221250e6 100644 --- a/avogadro/io/cjsonformat.cpp +++ b/avogadro/io/cjsonformat.cpp @@ -80,6 +80,11 @@ bool isBooleanArray(json& j) } bool CjsonFormat::read(std::istream& file, Molecule& molecule) +{ + return deserialize(file, molecule, true); +} + +bool CjsonFormat::deserialize(std::istream& file, Molecule& molecule, bool isJson) { json jsonRoot = json::parse(file, nullptr, false); if (jsonRoot.is_discarded()) { @@ -622,6 +627,11 @@ bool CjsonFormat::read(std::istream& file, Molecule& molecule) } bool CjsonFormat::write(std::ostream& file, const Molecule& molecule) +{ + return serialize(file, molecule, true); +} + +bool CjsonFormat::serialize(std::ostream& file, const Molecule& molecule, bool isJson) { json opts; if (!options().empty()) @@ -1042,8 +1052,7 @@ bool CjsonFormat::write(std::ostream& file, const Molecule& molecule) root["layer"]["settings"][settings.first] = setting; } - // Write out the file, use a two space indent to "pretty print". - if (m_json) + if (isJson) file << std::setw(2) << root; else // write msgpack json::to_msgpack(root, file); diff --git a/avogadro/io/cjsonformat.h b/avogadro/io/cjsonformat.h index 556a78204a..e7e62a6528 100644 --- a/avogadro/io/cjsonformat.h +++ b/avogadro/io/cjsonformat.h @@ -50,8 +50,9 @@ class AVOGADROIO_EXPORT CjsonFormat : public FileFormat bool read(std::istream& in, Core::Molecule& molecule) override; bool write(std::ostream& out, const Core::Molecule& molecule) override; - // write JSON or MessagePack - bool m_json = true; + // internal - to allow JSON or MsgPack to be written + bool deserialize(std::istream& in, Core::Molecule& molecule, bool json); + bool serialize(std::ostream& out, const Core::Molecule& molecule, bool json); }; } // end Io namespace diff --git a/avogadro/io/cmsgpackformat.cpp b/avogadro/io/cmsgpackformat.cpp index c4dcb4861f..5474832794 100644 --- a/avogadro/io/cmsgpackformat.cpp +++ b/avogadro/io/cmsgpackformat.cpp @@ -5,11 +5,16 @@ #include "cmsgpackformat.h" +#include + using namespace std; namespace Avogadro::Io { -CMsgPackFormat::CMsgPackFormat() = default; +CMsgPackFormat::CMsgPackFormat(): CjsonFormat() +{ + m_json = false; +} CMsgPackFormat::~CMsgPackFormat() = default; @@ -27,4 +32,15 @@ vector CMsgPackFormat::mimeTypes() const return mime; } + bool CMsgPackFormat::read(std::istream& in, Core::Molecule& molecule) + { + return CjsonFormat::deserialize(in, molecule, false); + } + + bool CMsgPackFormat::write(std::ostream& out, const Core::Molecule& molecule) + { + std::cerr << "CMsgPackFormat::write" << std::endl; + return CjsonFormat::serialize(out, molecule, false); + } + } // namespace Avogadro::Io diff --git a/avogadro/io/cmsgpackformat.h b/avogadro/io/cmsgpackformat.h index ce92f1d672..8a8afca6fd 100644 --- a/avogadro/io/cmsgpackformat.h +++ b/avogadro/io/cmsgpackformat.h @@ -48,6 +48,9 @@ class AVOGADROIO_EXPORT CMsgPackFormat : public CjsonFormat std::vector fileExtensions() const override; std::vector mimeTypes() const override; + bool read(std::istream& in, Core::Molecule& molecule) override; + bool write(std::ostream& out, const Core::Molecule& molecule) override; + // write MessagePack bool m_json = false; }; diff --git a/avogadro/io/fileformatmanager.cpp b/avogadro/io/fileformatmanager.cpp index 1841213ee0..999a9c2c68 100644 --- a/avogadro/io/fileformatmanager.cpp +++ b/avogadro/io/fileformatmanager.cpp @@ -9,6 +9,7 @@ #include "cjsonformat.h" #include "cmlformat.h" +#include "cmsgpackformat.h" #include "dcdformat.h" #include "gromacsformat.h" #include "lammpsformat.h" @@ -284,6 +285,7 @@ FileFormatManager::FileFormatManager() { addFormat(new CmlFormat); addFormat(new CjsonFormat); + addFormat(new CMsgPackFormat); addFormat(new DcdFormat); addFormat(new GromacsFormat); addFormat(new LammpsTrajectoryFormat);