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

Adding Values after call to clear() fails. #1533

Open
VicKrawciw opened this issue Mar 8, 2024 · 1 comment
Open

Adding Values after call to clear() fails. #1533

VicKrawciw opened this issue Mar 8, 2024 · 1 comment

Comments

@VicKrawciw
Copy link

Describe the bug
Adding Values after call to clear() fails.

To Reproduce
Steps to reproduce the behavior:
1.
bool result = false;

Json::Value root;
Json::Reader reader;

result = reader.parse(request, request + strlen(request), root, false);
if (result)
{
std::string command;
int advanced;
try
{
command = root[0]["command"].asString();
advanced = root[0]["advanced"].asInt();

  //Done with command
  root.clear();

  if (command.compare("GetStationDesc") == 0)
  {
   // If we use the root above after clear() this addition fails.
    // Json::Value root;
    root["name"] = "S2000Test";
    root["type"] = "S2000";

Expected behavior
After the root.clear() I should be able to add items.

Desktop (please complete the following information):

  • OS: Embedded RTX with Keil compiler.
  • Meson version
  • Ninja version

Additional context
Add any other context about the problem here.

@alajovic
Copy link

Please always provide a full minimum working example when opening an issue – that is, the code should be ready to compile. In this particular case, you have omitted the content of the request variable, so the example does not compile straight out of the box, which makes it significantly harder to investigate.

Given that you are extracting information from root[0] before proceeding to the call to root.clear(), I assume that root contains a JSON array. Jsoncpp documentation for Value::clear() explicitly says that the value type remains unchanged after the call to clear(). So at the point where you are doing root["name"] = "S2000Test", you are assigning to an array as if it was an object. The library – correctly – rejects this attempt.

If you want to reuse root as an object, do root = Json::objectValue first.

The mistake clearly seems to be in the supplied code, not in the library, so the issue may be closed.

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