Created
May 22, 2023 20:32
-
-
Save lemire/abaf50efb841d879d948141fb215d93b to your computer and use it in GitHub Desktop.
long way to do object/string to string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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