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

Error Using JSON Library with arrays C++ #475

Closed
JMcCASKI opened this issue Feb 26, 2017 · 6 comments
Closed

Error Using JSON Library with arrays C++ #475

JMcCASKI opened this issue Feb 26, 2017 · 6 comments

Comments

@JMcCASKI
Copy link

Hi,

I am using your json c++ libraryon a University project.

I am trying to create a json structure with an array of strings contained in it... along with some other information.

Whenever I try to add the array I get an error:
./json.hpp:843:9: error: static_assert failed "could not find to_json() method

  in T's namespace"

    static_assert(sizeof(BasicJsonType) == 0,

I have not implemented to_json() method if that is required, though, I am unsure how to do sure with you library implementation.

My code is along the lines of:

    // Store messageboard data with JSON Lib

    json mbData;    // Json file to hold messageboard information
    // Add to mbData

   

    mbData["messageBoard"] = 
    {
         mbMessage = {message}; // Store the array of strings here
        {"NumUser", "NumMessage"},
        {numUser, numMessage}
    };

where message is a string array each of char size 50 from the string.h lib.

Possibly you may know why this is happening ?

Thanks,

Josh McCaskill

@JMcCASKI
Copy link
Author

I just realized it may be because I am not allocating mem for the strings properly?
This part of the project is less important.
I just want to get it working so I can start encryption.

string message[50]; // Messaged linked to user name through array index

@nlohmann
Copy link
Owner

You usually only need to implement a to_json() function if you are using a user-defined type. The error message you got indicates that the compiler did not find a to_json() function for your type. Looking at your code, it seems as if the compiler does not know how to convert

    {
         mbMessage = {message}; // Store the array of strings here
        {"NumUser", "NumMessage"},
        {numUser, numMessage}
    };

to json.

I understand you want to store an array of strings. This is possible by code like

json mbData;
mbData["messageBoard"] = {"foo", "bar", "baz");

I'm not sure if this helps you. Please let me know.

@gregmarr
Copy link
Contributor

You probably want to use std::vector<std::string> message; or std::array<std::string, 50> message; instead of std::string message[50]; and then it will work.

@JMcCASKI
Copy link
Author

Thanks for the responses.

I do not think I will be able to store the strings as = { aaa, bbb } since they are inputted through both a file and interface.

Unless this would work ?

GUI Input & File Input-> saved as strings
standard in... >> var;1
file >> var2

json mbData;
mbData["messageBoard"] = {var1, var2, var3);

I will know how many there are, but not what values they hold.

If not I will try
std::array<std::string, 50> message;

What do the > mean in this statement?

@nlohmann
Copy link
Owner

You can use push_back just like you would for a std::vector.

@nlohmann
Copy link
Owner

@JMcCASKI Did it work now?

@nlohmann nlohmann added state: please discuss please discuss the issue or vote for your favorite option and removed state: please discuss please discuss the issue or vote for your favorite option labels Mar 1, 2017
@nlohmann nlohmann closed this as completed Mar 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants