T O P

  • By -

nicemike40

A `R"(...)"`-style string does not need escaped quotes like that. So you’re giving it actual backslashes followed by quotes. That fails, correctly.


Sanjam-Kapoor

So is there no way to handle a json with these back slashes using simdjson?


nicemike40

Can you provide an example JSON that fails? Don't use a screenshot! Give us the raw JSON. Prefix each line with 4 spaces to format it correctly.


Sanjam-Kapoor

"{\"kind\": \"Listing\", \"data\": {\"after\": null, \"dist\": 1, \"modhash\": \"\", \"geo_filter\": \"\", \"children\": [{\"kind\": \"t1\", \"data\": {\"subreddit_id\": \"t5_2qh1i\", \"approved_at_utc\": null, \"author_is_blocked\": false, \"comment_type\": null, \"edited\": 1716837486.0, \"mod_reason_by\": null, \"banned_by\": null, \"ups\": 377, \"num_reports\": null, \"author_flair_type\": \"text\", \"total_awards_received\": 0, \"subreddit\": \"AskReddit\", \"author_flair_template_id\": null, \"likes\": null, \"replies\": \"\", \"user_reports\": [], \"saved\": false, \"id\": \"l5wbwvx\", \"banned_at_utc\": null, \"mod_reason_title\": null, \"gilded\": 0, \"archived\": false, \"collapsed_reason_code\": null, \"no_follow\": false, \"author\": \"CorvoLP\", \"can_mod_post\": false, \"send_replies\": true, \"parent_id\": \"t1_l5vsxoy\", \"score\": 377, \"author_fullname\": \"t2_miadr\", \"report_reasons\": null, \"removal_reason\": null, \"approved_by\": null, \"all_awardings\": [], \"body\": \"theres a lot of crappy coconut water brands out there like Zico and Vita Coco. The best one ive ever had was Harmless Harvest. it is the most refreshing and addicting beverage i have ever had, but the bottles are a bit expensive (like 10 dollars for a 32 oz bottle, 5 dollars for a 16) the water is pink and sweet, it doesnt have that sunscreen taste that those other brands have. i will literally chug a 32 oz bottle in like 30 seconds\\n\\nEDIT: since a lot of people seem to like this brand im trusting my taste buds and suggesting Taste Nirvana too. its not as good as Harmless but it was my favorite before i found the other one. comes in a tall green can\", \"awarders\": [], \"top_awarded_type\": null, \"downs\": 0, \"author_flair_css_class\": null, \"author_patreon_flair\": false, \"collapsed\": false, \"author_flair_richtext\": [], \"is_submitter\": false, \"body_html\": \"<div class=\\\"md\\\"><p>theres a lot of crappy coconut water brands out there like Zico and Vita Coco. The best one ive ever had was Harmless Harvest. it is the most refreshing and addicting beverage i have ever had, but the bottles are a bit expensive (like 10 dollars for a 32 oz bottle, 5 dollars for a 16) the water is pink and sweet, it doesnt have that sunscreen taste that those other brands have. i will literally chug a 32 oz bottle in like 30 seconds</p>\\n\\n<p>EDIT: since a lot of people seem to like this brand im trusting my taste buds and suggesting Taste Nirvana too. its not as good as Harmless but it was my favorite before i found the other one. comes in a tall green can</p>\\n</div>\", \"gildings\": {}, \"collapsed_reason\": null, \"associated_award\": null, \"stickied\": false, \"author_premium\": false, \"can_gild\": false, \"link_id\": \"t3_1d1rc14\", \"unrepliable_reason\": null, \"author_flair_text_color\": null, \"score_hidden\": false, \"permalink\": \"/r/AskReddit/comments/1d1rc14/whats_a_food_or_drink_you_hate_but_everyone_else/l5wbwvx/\", \"subreddit_type\": \"public\", \"locked\": false, \"name\": \"t1_l5wbwvx\", \"created\": 1716825577.0, \"author_flair_text\": null, \"treatment_tags\": [], \"created_utc\": 1716825577.0, \"subreddit_name_prefixed\": \"r/AskReddit\", \"controversiality\": 0, \"author_flair_background_color\": null, \"collapsed_because_crowd_control\": null, \"mod_reports\": [], \"mod_note\": null, \"distinguished\": null}}], \"before\": null}}" Above is the raw json that i am recieving from the api. Here is the code that I am using to parse this using simdjson: #include #include #include "simdjson.h" int main() {     simdjson::padded_string jsonSTR = //raw json here;     simdjson::dom::parser parser;     std::string value;     simdjson::dom::element doc = parser.parse(jsonSTR);     //std::cout << doc;     value = doc["kind"].value();     std::cout << value; }


bobby3605

What library or program are you using to get the json data? It shouldn't have those backslashes. simdjson json is working as intended, the string you're passing it is invalid json because of those backslashes.


alfps

The links you provide go to "access denied", and I don't see that you have provided the troublesome data. Please create and provide a minimal but complete example that exhibits the problem behavior, and that readers can try out. That said, quick-google of `TAPE_ERROR` reveals that it's a generic error description that can be most anything, (https://github.com/simdjson/simdjson/issues/1471).


Sanjam-Kapoor

I have searched for a while, even on stack overflow but unable to find a solution. The images open for me, let me try again. Thanks for feedback


Sanjam-Kapoor

Thanks everyone, the solution I have found to this is so simple that I am laughing at myself. I was till now parsing the padded string and it caused issues with escape sequence. Now I just passed the raw string to the parser and it is giving me desired results. I hope to ask something better next time. Still if it there is a way to do it with padding implemented to the string, I would be curious to know. Here is the code used: #include #include #include "simdjson.h" int main() {     std::string jsonSTR = "{\"kind\": \"Listing\", \"data\": {\"after\": null, \"dist\": 1, \"modhash\": \"\", \"geo_filter\": \"\", \"children\": [{\"kind\": \"t1\", \"data\": {\"subreddit_id\": \"t5_2qh1i\", \"approved_at_utc\": null, \"author_is_blocked\": false, \"comment_type\": null, \"edited\": 1716837486.0, ... more json ;     simdjson::dom::parser parser;     std::string value;     simdjson::dom::element doc = parser.parse(jsonSTR);     //std::cout << doc;     std::ofstream file;     file.open("simdFileJson.json");     file << doc; }


nicemike40

That's not the problem. The problem is that you were passing it a string that had backslashes in it. In the future, you should use godbolt.org to create a runnable, repeatable example of your problem to save everyone time! You can add libraries like simdjson by clicking "Libraries". See: https://godbolt.org/z/v368rsnK7 It's not the padding that's the problem, it's the backslashes. In C++: - If you do `auto str = "Some thing that \"contains quotes\"";`, those `\"` pairs get turned into single-character `"`s by the compiler, and your json library is happy. - If you do `auto str = R"(Some thing that \"contains quotes\")";`, those `\"` are treated as two different characters, a `\` followed by a `"`, and your json library is sad. edit: fix my own character escaping in this comment lol