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

Issues when adding values to firebase database #704

Closed
jussihi opened this issue Aug 22, 2017 · 10 comments
Closed

Issues when adding values to firebase database #704

jussihi opened this issue Aug 22, 2017 · 10 comments

Comments

@jussihi
Copy link

jussihi commented Aug 22, 2017

Hello, I'm having some issues adding values to a firebase database using this json library.

The test["pi"] adds into the firebase very well, but then when I try to add the test["pia"]["pia"] it responds with a "error" : "Invalid data; couldn't parse JSON object, array, or value."

Here is my code:

`json test;
 test["pi"] = 1.41;
 test["pia"]["pia"] = 2.0; 
 CURLcode res;
 struct curl_slist *headers = NULL;
 headers = curl_slist_append(headers, "Content-Type: application/json");
 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
 curl_easy_setopt(curl, CURLOPT_URL, "https://aaaa.firebaseio.com/bbbb.json");
 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, moi.dump());
 res = curl_easy_perform(curl);
 std::cout << res << std::endl;
 curl_slist_free_all(headers);
 curl_easy_cleanup(curl);`
@jussihi
Copy link
Author

jussihi commented Aug 22, 2017

I found this with a solution that I should wrap my JSON into single quotes:
https://stackoverflow.com/questions/33683757/firebase-invalid-data-couldnt-parse-json-object

Can I do it automatically with this json library?

@jussihi
Copy link
Author

jussihi commented Aug 22, 2017

More input; if I wirte this code:

test["asd2"] = {{"asd3","asd2"},{"asd4","asd5"}};
std::string s = test.dump();
std::cout << s << std::endl;

I get output {"asd2":{"asd3":"asd2","asd4":"asd5"}} from the cmd.

Now if I use libcurl from cmd, with input:
curl -H 'Content-Type:application/json' -X PUT -d "{\"asd2\" :{\"asd3\": \"asd2\", \"asd4\":\"asd5\"}}" "https://....

I get the exact same output ( {"asd2":{"asd3":"asd2","asd4":"asd5"}} ), but only using libcurl from terminal succees.. What is going on?

@nlohmann
Copy link
Owner

No, adding single quotes around a JSON text is not supported by the library, but can be done without problems in client code.

Unfortunately, I do not understand your third comment #704 (comment). What is the issue?

@jussihi
Copy link
Author

jussihi commented Aug 22, 2017

Thank you very much for responding. I'm currently pulling my hair off because of this. Yes as you can see from this post it doesn't seem to need those single quotes anymore.. But here is what I got:

So, giving this exact input to my curl.exe straight from terminal (windows CMD) works;
curl -H 'Content-Type:application/json' -X PUT -d "{\"asd2\" :1.41, \"pi2\" : 2.82}" "https://

It inputs no problem; also giving raw string from my C++ program like this works;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"pi\":1.41,\"pi2\":2.82}");

but then when I try to feed it with your json library output, it doesn't work:
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, j.dump());

and on top of that both "{\"pi\":1.41,\"pi2\":2.82}" and j.dump() seem exactly the same (at least to my eye) when printed out from my C++ program.

and more info: I'm using google firebase and its REST api documented here https://firebase.google.com/docs/reference/rest/database/

@nlohmann
Copy link
Owner

That's weird. Can you make sure the strings are identical to the byte? There may be some subtle issue with escaping.

@jussihi
Copy link
Author

jussihi commented Aug 22, 2017

Can you point me a way to make clear they are identical?

Can I just memcmp with j.dump().data() and string.data() ?

@nlohmann
Copy link
Owner

Well, storing both in a std::string and check if they are equal with == should be sufficient. It's just a guess, but the first thing I would try if they "look" the same, but treated differently.

@theodelrieu
Copy link
Contributor

I might miss something here, but are you passing a std::string to the curl API?
Does that even compile?

@jussihi
Copy link
Author

jussihi commented Aug 22, 2017

@nlohmann the strings are the same

@theodelrieu yes it does, without warnings. and now as you ask, yes, it works if I put in j.dump().data()

Now the firebase response is "No data supplied" :(

nvm, it works, you can close it. Thanks @theodelrieu for opening my eyes.

@jussihi jussihi closed this as completed Aug 22, 2017
@theodelrieu
Copy link
Contributor

@jussihi It's because of varargs I guess, that's weird, glad it fixes your problem!

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