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

Problem when writing to file. #904

Closed
liorko87 opened this issue Jan 7, 2018 · 4 comments
Closed

Problem when writing to file. #904

liorko87 opened this issue Jan 7, 2018 · 4 comments

Comments

@liorko87
Copy link

liorko87 commented Jan 7, 2018

Bug Report

  • What is the issue you have?

I'm trying to write json file to file. When I'm printing the json to the screen, I can see it created normally.
JSON example file:

[{
	"iteration_0": 13,
	"iteration_1": 11,
	"name": "ComputeCircuit"
}, {
	"iteration_0": 0,
	"iteration_1": 1,
	"name": "InputSharing"
}, {
	"iteration_0": 14,
	"iteration_1": 13,
	"name": "Online"
}, {
	"iteration_0": 5,
	"iteration_1": 2,
	"name": "GenerateTriples"
}, {
	"iteration_0": 6,
	"iteration_1": 2,
	"name": "Offline"
}]
  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?
    I attempt to create the json file using this code sample:
void Measurement::analyze(string type)
{
    string filePath = getcwdStr();
    string fileName = filePath + "/" + m_protocolName + "_" + type + "_partyId=" + to_string(m_partyId)
                      +"_numOfParties=" + to_string(m_numOfParties) + ".json";

    //party is the root of the json objects
    json party = json::array();

    for (int taskNameIdx = 0; taskNameIdx < m_names.size(); taskNameIdx++)
    {
        //Write for each task name all the iteration
        json task = json::object();
        task["name"] = m_names[taskNameIdx];

        for (int iterationIdx = 0; iterationIdx < m_numberOfIterations; iterationIdx++)
        {
            task["iteration_" + to_string(iterationIdx)] = (*m_cpuEndTimes)[taskNameIdx][iterationIdx];
        }
        party.insert(party.begin(), task);
    }

    //send json object to create file
    createJsonFile(party, fileName);
}


void Measurement::createJsonFile(json j, string fileName)
{
    try
    {
        ifstream myfile;
        myfile.open(fileName, ifstream::out);
        cout << j << endl;
        if (myfile.is_open())
        {
            myfile >> j;
        }
    }

    catch (exception& e)
    {
        cout << "Exception thrown : " << e.what() << endl;
    }
}
  • What is the expected behavior?
    Create json file

  • And what is the actual behavior instead?
    I got this error: [json.exception.parse_error.101] parse error at 1: syntax error - unexpected end of input; expected '[', '{', or a literal

  • Which compiler and operating system are you using? Is it a supported compiler?
    I'm using GCC 5.4, Ubuntu 16.04.3 LTS

  • Did you use a released version of the library or the version from the develop branch?
    I used the latest release 3.0.1

@nlohmann
Copy link
Owner

nlohmann commented Jan 7, 2018

In your createJsonFile function, myfile >> j means "parse file myfile into JSON value j". I don't think this is what you want to do, and this could explain the error.

@liorko87
Copy link
Author

liorko87 commented Jan 7, 2018

Yes you are right, this is no what I wanted.
How do I right this json object to a file? When I'm printing it using cout everything is fine.

@nlohmann
Copy link
Owner

nlohmann commented Jan 7, 2018

std::ofstream o("myoutfile.json");
o << j;

See https://github.com/nlohmann/json#tofrom-streams-eg-files-string-streams for examples.

@liorko87
Copy link
Author

liorko87 commented Jan 7, 2018

Thank you very much, It helped me. I will look at the attached link.

@liorko87 liorko87 closed this as completed Jan 7, 2018
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

2 participants