nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.25k stars 6.57k forks source link

Crashes when I try to use ‘json::at()’ on a properly structured, non null, and correctly constructed ‘.json’ file #4387

Closed SlugsGate closed 3 weeks ago

SlugsGate commented 4 weeks ago

Description

Just a plain line like this triggers a Segmentation fault (core dumped):

name = j.at("name").get<std::string>();

This also doesn’t get caught with try and catch blocks even when I try to catch exception &e.

json::dump() prints correctly structured, non null, and correctly constructed. Despite of all of these, it still crashes. the even weirder part is that this line was working before.

I have tried making a string, inserting a structured looking json in it, parse it as a json, and try to deserialize it into my Item class which ends perfectly, oddly enough.

Reproduction steps

Serialize a new json file from an inventory, then try to deserialize it into another new inventory (though it crashes way before it finishes).

Expected vs. actual results

The expected result is that json::at() properly returns values appropriately but instead crashes with Segmentation fault (core dumped).

Minimal code example

inventory.cpp | Inventory::deserialization:

void Inventory::deserialize(const std::string &filename) {
  cout << "Deserializing inventory..." << endl;
  std::ifstream file(filename);

  if (file.is_open()) {
    std::cout << "Reading from file..." << std::endl;
    json inventoryJson;
    file >> inventoryJson;
    file.close();

    for (int i = 0; i < maxSlots; ++i) {
      cout << "Deserializing slot " << i << endl;
      if (!inventoryJson["slots"][i].is_null()) {
        cout << "Slot j " << i << " is not null" << endl;
        slots[i]->from_json(inventoryJson["slots"][i]);
        isOccupied[i] = true;
        cout << "Slot " << i << " deserialized" << endl;

…

Slot.cpp | Slot::from_json:

void Slot::from_json(const json &j) {
  reset();

  if (!j.is_null()) {
    try {
      setQuantity( item->from_json(j) );
      maxStackQuantity = item->getMaxStackQuantity();
      status = (quantity < maxStackQuantity) ? Status::FILLING : Status::FULL;

    } catch (const json::type_error &e) {
      std::cerr << "Type error during Slot deserialization: " << e.what()
                << std::endl;
      throw;
    } catch (const std::exception &e) {
      std::cerr << "Error during Slot deserialization: " << e.what()
                << std::endl;
      throw;
    }
  }
}

Item.cpp | Item::from_json:

int Item::from_json(const json &j) {
  try {
    std::cout << "Input JSON: " << j.dump(4)
              << std::endl; // Pretty print JSON for debugging

    // Check and assign JSON fields
    if (j.contains("name"))
      name = j.at("name").get<std::string>();

   …
  }
}

Error messages

Segmentation fault (core dumped)

Compiler and operating system

clang++ UNIX (replit.com)

Library version

I strongly believe this is C++11

Validation

SlugsGate commented 3 weeks ago

nevermind, fixed it.

SlugsGate commented 3 weeks ago

was just my code's fault