Skip to content

How to serialize/deserialize an Eigen matrix? #541

Answered by danielaparker
recsater asked this question in Q&A
Discussion options

You must be logged in to vote

Apologies, just got to this.

I would suggest defining separate json_type_traits structs for the dynamic and fixed row/column cases. Something like this

namespace jsoncons {

template <typename Json, typename Scalar, std::size_t RowsAtCompileTime, std::size_t ColsAtCompileTime>
struct json_type_traits<Json, Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime>>
{
    using allocator_type = typename Json::allocator_type;    
    using matrix_type = Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime>;
    
    static bool is(const Json& val) noexcept
    {
        if (!val.is_array() || val.size() != RowsAtCompileTime)
            return false;

        for (std::size_t i = 0; …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@recsater
Comment options

Answer selected by recsater
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants