Skip to content

Instantly share code, notes, and snippets.

@lemire
Created May 22, 2023 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemire/abaf50efb841d879d948141fb215d93b to your computer and use it in GitHub Desktop.
Save lemire/abaf50efb841d879d948141fb215d93b to your computer and use it in GitHub Desktop.
long way to do object/string to string
#include "simdjson.h"
#include <iostream>
using namespace simdjson; // optional
int main() {
padded_string json = R"( {"exports": {}} )"_padded;
ondemand::parser parser;
ondemand::document doc = parser.iterate(json);
ondemand::object obj;
auto err = doc.get_object().get(obj);
if (err) {
return EXIT_FAILURE;
}
ondemand::value val;
err = obj["exports"].get(val);
if (err) {
// missing key
return EXIT_FAILURE;
}
// we have the value
ondemand::json_type type;
err = val.type().get(type);
if (err) {
// can't determine the type
return EXIT_FAILURE;
}
// we have the type
std::string_view result{};
if (type == ondemand::json_type::string) {
if (val.get_string().get(result)) {
return EXIT_FAILURE;
}
} else if (type == ondemand::json_type::object) {
ondemand::object subobj;
if (val.get_object().get(subobj)) {
return EXIT_FAILURE;
}
if (subobj.raw_json().get(result)) {
return EXIT_FAILURE;
}
} else {
// unexpected type
return EXIT_FAILURE;
}
std::cout << result << std::endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment