0

I am trying to understand JSON to string and string to JSON conversion using nlohmann framework for JSON library in C++. However I am getting problem with exception generated and I am not understanding the reason why it is being generated.

The following is the code I have used from nlohmann framework. I tried to create a JSON object "j_string", add attribute "transformation_matrix" and add a corresponding value to it, of type string from a predefined variable "greetings". Now I am trying to convert from string to JSON, then parse it and again convert to string. Basically I am trying to send these data over UDP, which is whole idea behind my project. To do that, I need to convert from string to JSON, do some computation to extract a certain value of an attribute and then convert it back to string and send over UDP. When I try to convert from string to JSON, I get exception from compiler. I am using Visual Studio 2019. The following is the exception generated:

"Unhandled exception at 0x772718A2 in jsoncpp2.exe: Microsoft C++ exception: nlohmann::detail::type_error at memory location 0x00E9F504."

Let me know why such exception is being generated. When I compile the code, it says no error. However, when I execute it, I get an exception. I have added necessary files and headers.

#include<iostream>
#include<sstream>
#include<nlohmann/json.hpp>

using json = nlohmann::json;

int main() {
    std::string greetings = "greetings from string";
    json j_string;
    j_string["transformation matrix"] = greetings;

    auto cpp_string = j_string.get<std::string>();

    std::string serialized_string = j_string.dump();

    std::cout << serialized_string << '\n';
}

My expected result is:

[
"transfornation matrix" : "greetings from string"
]
  • I don't have my dev. environment close but if I remember correctly, nlohmann's json throws if you give it non-utf8 input. it's illegal according to the standard. Try catching exceptions. It's a wild guess. – Ted Lyngmo May 03 '19 at 18:12
  • I am sorry, I am quite new to JSON. What is UTF8? Why is it illegal? – prashanth kumar May 03 '19 at 18:22
  • UTF8 is the only combination of bytes that is legal in JSON. nlohmann's json is pretty good at signaling that if I remember correctly. UTF8 is a chapter of its own but it's not magic, it's just cumbersum. Intro: [UTF8 everywhere](http://utf8everywhere.org/) – Ted Lyngmo May 03 '19 at 18:24

1 Answers1

0

For general safety, you should be making sure that j_string.is_string() before you call j_string.get<std::string>() to avoid such exceptions.

Also, note that since you just used object-style assignment with j_string["transformation matrix"] = greetings;, j_string becomes an object type. Had you instead assigned a string to j_string directly, it would have instead taken on a string type.

This is covered in the first two lines of the nlohmann/json examples.

// create an empty structure (null)
json j;

// add a number that is stored as double (note the implicit conversion of j to an object)
j["pi"] = 3.141;
alter_igel
  • 6,899
  • 3
  • 21
  • 40