Skip to content

Commit

Permalink
Add support for a MessagePack variant to CJSON
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Aug 31, 2023
1 parent c6d1ee3 commit a1e2790
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
13 changes: 11 additions & 2 deletions avogadro/io/cjsonformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions avogadro/io/cjsonformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion avogadro/io/cmsgpackformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@

#include "cmsgpackformat.h"

#include <iostream>

using namespace std;

namespace Avogadro::Io {

CMsgPackFormat::CMsgPackFormat() = default;
CMsgPackFormat::CMsgPackFormat(): CjsonFormat()
{
m_json = false;
}

CMsgPackFormat::~CMsgPackFormat() = default;

Expand All @@ -27,4 +32,15 @@ vector<std::string> 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
3 changes: 3 additions & 0 deletions avogadro/io/cmsgpackformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class AVOGADROIO_EXPORT CMsgPackFormat : public CjsonFormat
std::vector<std::string> fileExtensions() const override;
std::vector<std::string> 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;
};
Expand Down
2 changes: 2 additions & 0 deletions avogadro/io/fileformatmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "cjsonformat.h"
#include "cmlformat.h"
#include "cmsgpackformat.h"
#include "dcdformat.h"
#include "gromacsformat.h"
#include "lammpsformat.h"
Expand Down Expand Up @@ -284,6 +285,7 @@ FileFormatManager::FileFormatManager()
{
addFormat(new CmlFormat);
addFormat(new CjsonFormat);
addFormat(new CMsgPackFormat);
addFormat(new DcdFormat);
addFormat(new GromacsFormat);
addFormat(new LammpsTrajectoryFormat);
Expand Down

0 comments on commit a1e2790

Please sign in to comment.