Skip to content

Commit

Permalink
Add a Chem MessagePack format as a subclass of 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 22, 2023
1 parent b5239fe commit c6d1ee3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
2 changes: 2 additions & 0 deletions avogadro/io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_library(IO)
avogadro_headers(IO
cjsonformat.h
cmlformat.h
cmsgpackformat.h
dcdformat.h
fileformat.h
fileformatmanager.h
Expand All @@ -19,6 +20,7 @@ avogadro_headers(IO
target_sources(IO PRIVATE
cjsonformat.cpp
cmlformat.cpp
cmsgpackformat.cpp
dcdformat.cpp
fileformat.cpp
fileformatmanager.cpp
Expand Down
5 changes: 4 additions & 1 deletion avogadro/io/cjsonformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,10 @@ bool CjsonFormat::write(std::ostream& file, const Molecule& molecule)
}

// Write out the file, use a two space indent to "pretty print".
file << std::setw(2) << root;
if (m_json)
file << std::setw(2) << root;
else // write msgpack
json::to_msgpack(root, file);

return true;
}
Expand Down
5 changes: 4 additions & 1 deletion avogadro/io/cjsonformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ class AVOGADROIO_EXPORT CjsonFormat : public FileFormat

std::string specificationUrl() const override
{
return "http://wiki.openchemistry.org/Chemical_JSON";
return "https://github.com/openchemistry/chemicaljson";
}

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 JSON or MessagePack
bool m_json = true;
};

} // end Io namespace
Expand Down
30 changes: 30 additions & 0 deletions avogadro/io/cmsgpackformat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#include "cmsgpackformat.h"

using namespace std;

namespace Avogadro::Io {

CMsgPackFormat::CMsgPackFormat() = default;

CMsgPackFormat::~CMsgPackFormat() = default;

vector<std::string> CMsgPackFormat::fileExtensions() const
{
vector<std::string> ext;
ext.emplace_back("cmpk");
return ext;
}

vector<std::string> CMsgPackFormat::mimeTypes() const
{
vector<std::string> mime;
mime.emplace_back("chemical/x-cmpack");
return mime;
}

} // namespace Avogadro::Io
58 changes: 58 additions & 0 deletions avogadro/io/cmsgpackformat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#ifndef AVOGADRO_IO_CMSGPACKFORMAT_H
#define AVOGADRO_IO_CMSGPACKFORMAT_H

#include "fileformat.h"
#include "cjsonformat.h"

namespace Avogadro {
namespace Core {
class GaussianSet;
}
namespace Io {

/**
* @class CMsgPackFormat cmsgpackformat.h <avogadro/io/cmsgpackformat.h>
* @brief Implementation of the Chemical MessagePack format.
*/

class AVOGADROIO_EXPORT CMsgPackFormat : public CjsonFormat
{
public:
CMsgPackFormat();
~CMsgPackFormat() override;

Operations supportedOperations() const override
{
return ReadWrite | File | Stream | String;
}

FileFormat* newInstance() const override { return new CMsgPackFormat; }
std::string identifier() const override { return "Avogadro: CMsgPack"; }
std::string name() const override { return "Chemical MessagePack"; }
std::string description() const override
{
return "CMsgPack format is a lightweight intermediate format used to exchange "
"information between Avogadro and other data parsing applications";
}

std::string specificationUrl() const override
{
return "https://github.com/openchemistry/chemicaljson";
}

std::vector<std::string> fileExtensions() const override;
std::vector<std::string> mimeTypes() const override;

// write MessagePack
bool m_json = false;
};

} // end Io namespace
} // end Avogadro namespace

#endif // AVOGADRO_IO_CMSGPACKFORMAT_H

0 comments on commit c6d1ee3

Please sign in to comment.