Replies: 10 comments 3 replies
-
If I use this iterate:
it gives the same error. |
Beta Was this translation helpful? Give feedback.
-
This is unrelated to simdjson. You misunderstand the sizeof operator in C/C++. Given any pointer, It does not do what you think it does. You should be able to do... const char * data = "my data";
simdjson::padded_string my_padded_data(data, 7);
ondemand::parser parser;
ondemand::document doc = parser.iterate(my_padded_data); Give it a try! |
Beta Was this translation helpful? Give feedback.
-
Since we are on a topic, why I cannot handle square brackets?
produces: Square brackets are for the arrays, but an array can contain just a single element. |
Beta Was this translation helpful? Give feedback.
-
nm, I need to iterate using object. |
Beta Was this translation helpful? Give feedback.
-
@pmuvb Your JSON document is
It is an array with just one element in it. You are trying to do Are you sure you do not mean... #include "simdjson.h"
#include <iostream>
using namespace simdjson;
int main(void) {
ondemand::parser parser;
auto json = R"({"sym":"AAPL","vw":223.5643})"_padded;
std::cout << json << std::endl;
ondemand::document doc = parser.iterate(json);
std::cout << std::string_view(doc["sym"]) << std::endl;
return 0;
} I suspect that your difficulty is with the C++ syntax here. |
Beta Was this translation helpful? Give feedback.
-
That's how I receive the data, sometimes they contain just one element sometimes multiple, but vendor is always using square brackets: [{"ev":"A","sym":"NVDA","v":110,"av":1028901,"vw":135.5109,"o":135.51,"c":135.51,"h":135.51,"l":135.51,"a":135.245,"z":55,"s":1730457518000,"e":1730457519000}] but, yes, I know how to handle it:
TX for responding I don't know why I am having trouble with code formatting here, it doesn't do it for me. |
Beta Was this translation helpful? Give feedback.
-
@pmuvb You can also use JSON Pointer expressions if you really just need to pick a few bits of information, such as in this example... #include "simdjson.h"
#include <iostream>
using namespace simdjson;
int main(void) {
ondemand::parser parser;
auto json = R"([{"sym":"AAPL","vw":223.5643}])"_padded;
ondemand::document doc = parser.iterate(json);
std::cout << doc.at_pointer("/0/sym") << std::endl;
return 0;
} This will not be performant if you do this repeatedly, however. The loop you are doing is the proper way to handle it. |
Beta Was this translation helpful? Give feedback.
-
These are multiple elements in array: [{"ev":"A","sym":"FTV","v":100,"av":1861991,"op":71.4,"vw":71.26,"o":71.26,"c":71.26,"h":71.26,"l":71.26,"a":71.7974,"z":100,"s":1730483301000,"e":1730483302000},{"ev":"A","sym":"VMD","v":229,"av":18870,"op":8.61,"vw":8.6286,"o":8.63,"c":8.63,"h":8.63,"l":8.63,"a":8.6591,"z":57,"s":1730483301000,"e":1730483302000},{"ev":"A","sym":"BBIO","v":100,"av":924557,"op":23.51,"vw":25.21,"o":25.21,"c":25.21,"h":25.21,"l":25.21,"a":24.8885,"z":100,"s":1730483301000,"e":1730483302000}] Right, I need to read those in a loop, hundreds, if not thousands, in a second. Hence, performance is a concern, I will be parsing all those values into a structure. |
Beta Was this translation helpful? Give feedback.
-
Since offered, could you take a look at my parsing code for a quick comment)) What to look:
I did test it for error handling, it works. build: $ clang++ -std=c++23 -g3 -O0 -W'all' -W'extra' -pedantic -DSIMDJSON_DEVELOPMENT_CHECKS=1
|
Beta Was this translation helpful? Give feedback.
-
Got rid of the strlen and replaced strncpy with memcpy, simplified retrieving the values. I believe I got the most out of the lib. I appreciate your code review! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Here is the code:
Here is the output:
strlen=7, sizeof=8.
terminate called after throwing an instance of 'simdjson::simdjson_error'
what(): INSUFFICIENT_PADDING: simdjson requires the input JSON string to have at least SIMDJSON_PADDING extra bytes allocated, beyond the string's length. Consider using the simdjson::padded_string class if needed.
Aborted
Just downloaded simdjson 3.10.1.
The sizeof() should be 72 bytes.
Why is the error?
Tx
Beta Was this translation helpful? Give feedback.
All reactions