Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize vector of glm:vec2 #1739

Closed
zalavariandris opened this issue Sep 6, 2019 · 6 comments
Closed

Serialize vector of glm:vec2 #1739

zalavariandris opened this issue Sep 6, 2019 · 6 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@zalavariandris
Copy link

I'd like to serialize a vector of glm:vec2;

I have tried to follow the instructions in the readme and implemented from and to json methods like so:

namespace glm {
    void to_json(json& j, const glm::vec2& P) {
        j = { { "x", P.x }, { "y", P.y } };
    };

    void from_json(const json& j, glm::vec2& P) {
        P.x = j.at("x").get<double>();
        P.y = j.at("y").get<double>();
    }
}

json j;
std::vector<glm::vec2> points{ glm::vec2( 70, -140 ), glm::vec2(30, -30), glm::vec2(20, 125) };
j["points"] = points;

this little snippet wont compile and I got the following errors:

'glm::begin': ambiguous call to overloaded function
'glm::end': ambiguous call to overloaded function
 std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::create': no matching overloaded function found

the funny thing is that when I replace glm::vec2 with a simple Point struct, it compiles, and works as expected.
I'm on windows, using Visual Studio, using the single header from the master branch version3.7.0

@nlohmann
Copy link
Owner

nlohmann commented Sep 8, 2019

Strange. This code compiles with Xcode:

#include "json.hpp"
#include <iostream>
#include "/usr/local/Cellar/glm/0.9.9.5/include/glm/glm.hpp"

using json = nlohmann::json;

namespace glm {
    void to_json(json& j, const glm::vec2& P) {
        j = { { "x", P.x }, { "y", P.y } };
    };
    
    void from_json(const json& j, glm::vec2& P) {
        P.x = j.at("x").get<double>();
        P.y = j.at("y").get<double>();
    }
}

int main()
{
    json j;
    std::vector<glm::vec2> points{ glm::vec2( 70, -140 ), glm::vec2(30, -30), glm::vec2(20, 125) };
    j["points"] = points;
    
    std::cout << j << std::endl;
}

Output:

{"points":[{"x":70.0,"y":-140.0},{"x":30.0,"y":-30.0},{"x":20.0,"y":125.0}]}

Can you try this minimal example?

@stale
Copy link

stale bot commented Oct 8, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Oct 8, 2019
@stale stale bot closed this as completed Oct 15, 2019
@h4k1m0u
Copy link

h4k1m0u commented Sep 13, 2022

Any idea why to_json and from_json have to be inside the glm namespace?
I'm asking because I wanted both functions to be templates (so I can use them with another Point struct with similar x/y/z fields), but I had the following summarized error:

error: no matching function for call to ‘nlohmann::basic_json<>::get<glm::vec3>()’
template argument deduction/substitution failed

Update: Nevermind, I just saw in the README that the two function must be in the same namespace as glm::vec3 type.

@falbrechtskirchinger
Copy link
Contributor

@h4k1m0u You can also use this alternate method for third-party types which avoids placing the functions into the glm namespace.

@h4k1m0u
Copy link

h4k1m0u commented Sep 13, 2022

Thanks @falbrechtskirchinger. I'll have a look later, seems advanced but I may need it in the future.

@dunyazad
Copy link

I had the same problem, and It worked when I put the functions (to_json, from_json) inside glm namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

5 participants