Skip to content

Sample code in GIF slide outdated (cannot use emplace() with array) #2457

Closed
@windy32

Description

@windy32

What is the issue you have?

I followed the code in the GIF slide to learn to use the library:

// g++ object.cpp -std=c++11 -o object
#include <iomanip>
#include <iostream>
#include "json.hpp"

using json = nlohmann::json;

int main()
{
    // create null value
    json j;

    // add via member function
    j.push_back({"key", "value"});
    j.emplace("key2", "value");

    // add via += operator
    j += {"another key", false};

    // add via key
    j["pi"] = 3.1415926;
    j["answer"]["everything"] = 42;

    std::cout << std::setw(4) << j << std::endl;
}

Under ubuntu 20.04 (g++ 9.3.0), the output is like this

$ g++ object.cpp -std=c++11 -o object
$ ./object 
terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.311] cannot use emplace() with array
Aborted

Please describe the steps to reproduce the issue.

See steps above

Can you provide a small but working code example?

See code above

What is the expected behavior?

The code in the GIF slides should work without exceptions

And what is the actual behavior instead?

See output above

Which compiler and operating system are you using?

  • Compiler: g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
  • Operating system: Ubuntu 20.04

Which version of the library did you use?

  • latest release version 3.9.1
  • other release - please state the version: ___
  • the develop branch

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions