The task is to parse json, coming by socket. There is no possibility to read all json into memory, you need to parse in parts. The processing code is approximately as follows:

char buffer[1024]; JsonParser parser; do { int length = socket.read(buffer, 1024); // возвращает длину прочитанного parser.parse(buffer, length); } while (length == 1024); JsonObject result = parser.getResult(); 

Those. you need a library that you can feed short arrays to the input, and then at the end get the result of parsing. What library can I use for this task, so as not to reinvent the wheel?

1 answer 1

First, we subtract json to the end and put it in std :: stringstream (in any case, we subtract it to the end and we’ll have to store all the text in memory):

 char buffer[1024]; std::stringstream ss; do { int length = socket.read(buffer, 1024); ss << buffer; } while (length == 1024); 

And then use one of the libraries, either nlohmann / json or Tencent / rapidjson :

 /// nlohmann/json nlohmann::json json; ss >> json; /// Tencent/rapidjson rapidjson::Document json; json.parse(ss.str()); 
  • In this case, the entire incoming message will be copied to stringstream, which is unacceptable, since the total length of this message can be quite large. The task is precisely to avoid the need to read the message completely and parse the pieces on the fly - Andrey Kurulyov
  • @AndreyKurulev Well, nothing that the object that you receive as a result will be even more than a string? (unless there are heaps of basic types: int, double, etc.) - outoftime
  • The object is fine, this can not be avoided. But from the preliminary reading of the entire message I want to get rid of. One message can be several megabytes long, so I just really don’t want to read it as a whole into memory - Andrey Kuruljov
  • @AndreyKurulev well, let's be different. Here, you have an object that will be built upon receipt of new data. We know that its size will be larger than the string representation due to overhead. You suggest not to run a string in memory because it is large but to keep another BIG json object .... - outoftime
  • I will need this json-object in any case, and therefore I cannot refuse to keep it in memory, and the string itself, which will occupy the memory capacity of the same order as the object itself, is absolutely not necessary for me - Andrei Kurulyov