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

converting std::chrono::system_clock::time_point to json. #2159

Closed
2 tasks
victor-psiori opened this issue Jun 2, 2020 · 3 comments
Closed
2 tasks

converting std::chrono::system_clock::time_point to json. #2159

victor-psiori opened this issue Jun 2, 2020 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@victor-psiori
Copy link

What do you want to achieve?

I was trying to convert time variable of my custom class which is std::chrono::system_clock::time_point type, to a json.

I am able to convert other class variable which are strings. The class outline looks like this:
SensorData(std::chrono::system_clock::time_point t, const std::string& topic, const std::string& frame_id)
After going through the README, I believe converting third party type is the relevant section for me. I have scratched the surface a bit, but yet to go deeper.
I have two questions:
i. Could you confirm whether this is the right section to look for regarding the mentioned use case.
ii. Are there examples available here which does a similar task?

Please let me know if something is not clear here.
Thanks.

What have you tried?

I have tried creating object as mentioned in README following a key-value pair pattern.

Can you provide a small code example?

`json j;
j["topic"] = topic;
j["frame_id"] = frame_id;
j["t"] = t; //here i get error: " no viable overloaded '=' "`

Which compiler and operating system are you using?

  • Compiler: Apple LLVM version 10.0.1 (clang-1001.0.46.4)
  • Operating system: mac os
    Target: x86_64-apple-darwin18.7.0
    Thread model: posix

Which version of the library did you use?

  • [ x] latest release version 3.7.3
  • other release - please state the version: ___
  • the develop branch
@nlohmann
Copy link
Owner

nlohmann commented Jun 3, 2020

This is the section you need: https://github.com/nlohmann/json#how-do-i-convert-third-party-types

Example:

#include <iostream>
#include <chrono>
#include "json.hpp"

using json = nlohmann::json;

namespace nlohmann {
    template<typename Clock, typename Duration>
    struct adl_serializer<std::chrono::time_point<Clock, Duration>>
    {
        static void to_json(json& j, const std::chrono::time_point<Clock, Duration>& tp)
        {
            j["since_epoch"] = std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch()).count();
            j["unit"] = "microseconds";
        }
    };
}

int main()
{
    auto time_point_now = std::chrono::high_resolution_clock::now();
    json j = time_point_now;
    std::cout << j << std::endl;
}

Output:

{"since_epoch":1732154346,"unit":"microseconds"}

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 3, 2020
@victor-psiori
Copy link
Author

oh yes, this works. Thanks a lot for the response.

@paulocoutinhox
Copy link

Hi,

Do you have the from_json method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants