Closed RochaStratovan closed 1 year ago
You are not calling the parse function, but pass strings to the constructor. This creates string values which have a size of 1 by design, see https://json.nlohmann.me/api/basic_json/size/#examples
Thank you for the clarification.
I've updated the code snippet, adding examples 4 and 5 to visualize/exercise what you are saying, and it confirms your statement.
Thank you.
Code:
#include <stdio.h>
#include <iostream>
#include <nlohmann/json.hpp>
#define DEBUG_DUMP(x) printf("%-3s : %s : %zu\n", \
#x, x.dump().c_str(), x.size())
int main(void)
{
// This is valid JSON and it's empty, no objects.
std::string emptyJson("{}");
std::string emptyJson2("{ }");
nlohmann::json j1(emptyJson);
DEBUG_DUMP(j1);
nlohmann::json j2(emptyJson2);
DEBUG_DUMP(j2);
// Parse nothing
nlohmann::json j3;
DEBUG_DUMP(j3);
nlohmann::json j4;
j4 = nlohmann::json::parse(emptyJson);
DEBUG_DUMP(j4);
nlohmann::json j5;
j5 = nlohmann::json::parse(j4.dump().c_str());
DEBUG_DUMP(j5);
return (0);
}
Output:
$ ./a.out
j1 : "{}" : 1
j2 : "{ }" : 1
j3 : null : 0
j4 : {} : 0
j5 : {} : 0
Description
An empty JSON object returns a size of 1rather than the expected size of 0.
This is problematic because:
accept()
indicates this is valid JSON (because it is)parse()
parses this as valid JSON (because it is)size()
indicates 1 so we expect there to be at least one KV pairReproduction steps
Create an empty JSON object using the string "
{}
" or "{ }
", and then check the size.According to https://www.json.org/json-en.html these strings are the minimum valid JSON string.
Expected vs. actual results
I expect and empty JSON object to return a size of 0.
However it is returning a value of 1.
Minimal code example
Output: