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

How to save json to file? #690

Closed
nm17 opened this issue Aug 10, 2017 · 13 comments
Closed

How to save json to file? #690

nm17 opened this issue Aug 10, 2017 · 13 comments

Comments

@nm17
Copy link

nm17 commented Aug 10, 2017

I'm trying to save json to file and all I'm getting is some weird errors. Any help?
Here is all the info:

Verison json: 2.1.1

gcc version 6.3.0 20170618 (Ubuntu 6.3.0-19ubuntu1)

Example code

#include "json.hpp"
using jsonf = nlohmann::json;
jsonf jsonfile;

// ...

jsonfile["foo"] = "bar";

std::fstream file("key.json");
file << jsonfile;

Error

json.hpp:843:9: error: static assertion failed: could not find to_json() method in T's namespace
         static_assert(sizeof(BasicJsonType) == 0,
         ^~~~~~~~~~~~~

json.hpp:843:9: error: static assertion failed: could not find to_json() method in T's namespace
@nlohmann
Copy link
Owner

This code works for me:

#include "json.hpp"
#include <fstream>

using jsonf = nlohmann::json;

int main() {
    jsonf jsonfile;

    jsonfile["foo"] = "bar";

    std::ofstream file("key.json");
    file << jsonfile;
}

@nm17
Copy link
Author

nm17 commented Aug 10, 2017

Sorry, replace:
jsonfile["foo"] = "bar";
with

const unsigned char a[] = "testing";
jsonfile["foo"] = a;

@nlohmann
Copy link
Owner

This works for me with the latest version of the develop branch and GCC 6.4.0 (macOS)

#include "json.hpp"
#include <fstream>

using jsonf = nlohmann::json;

int main() {
    jsonf jsonfile;

    const unsigned char a[] = "testing";
    jsonfile["foo"] = a;

    std::ofstream file("key.json");
    file << jsonfile;
}

Note the output is

{"foo":[116,101,115,116,105,110,103,0]}

Use const char a[] = "testing"; if you want the content to be interpreted as characters rather than numbers.

@nm17
Copy link
Author

nm17 commented Aug 10, 2017

Strange... I will see what can I do.

@theodelrieu
Copy link
Contributor

Are you using the latest release? If so, it is likely that this is fixed on develop

@nm17
Copy link
Author

nm17 commented Aug 10, 2017

I guess thats fixed somehow, but now i get this error:

 error: cannot convert ‘nlohmann::basic_json<>::value_type {aka nlohmann::basic_json<>}’ to ‘const unsigned char*’ in initialization
     const unsigned char * key = jsonfilekey["key"];

@nlohmann
Copy link
Owner

String are stored as std::string inside the JSON value. You can write

std::string key = jsonfilekey["key"];

and organize the conversion to const unsigned char* from there.

@nlohmann
Copy link
Owner

Related: #683

@nm17
Copy link
Author

nm17 commented Aug 10, 2017

For unknown reasons, that corrupts the whole string.
My string contains of base64(raw bytes).

@nlohmann
Copy link
Owner

What do you mean with "corrupts". Can you provide an example?

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Aug 11, 2017
@nlohmann
Copy link
Owner

Any news on this?

@nlohmann
Copy link
Owner

@NeverMine17 Any news on this?

@nlohmann nlohmann removed the state: please discuss please discuss the issue or vote for your favorite option label Dec 4, 2017
@ytfrdfiw
Copy link

This code works for me:

#include "json.hpp"
#include <fstream>

using jsonf = nlohmann::json;

int main() {
    jsonf jsonfile;

    jsonfile["foo"] = "bar";

    std::ofstream file("key.json");
    file << jsonfile;
}

file.flush();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants