MCPcopy Create free account
hub / github.com/a2flo/floor / create_document_from_string

Function create_document_from_string

core/json.cpp:907–936  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

905}
906
907document create_document_from_string(const string& json_data, const string identifier) {
908 const auto is_valid_utf8 = unicode::validate_utf8_string(json_data);
909 if(!is_valid_utf8.first) {
910 log_error("JSON data \"$\" is not UTF-8 encoded or contains invalid UTF-8 code points!",
911 identifier);
912 return {};
913 }
914
915 // create translation unit
916 auto json_tu = make_unique<translation_unit>(identifier);
917 json_tu->source.insert(0, json_data.c_str(), json_data.size());
918
919 // lexing
920 json_lexer::map_characters(*json_tu);
921 if(!json_lexer::lex(*json_tu)) {
922 log_error("lexing of JSON data \"$\" failed!", identifier);
923 return {};
924 }
925 json_lexer::assign_token_sub_types(*json_tu);
926
927 // parsing and document construction
928 document doc;
929 parser_context json_parser_ctx { *json_tu };
930 json_grammar json_grammar_parser;
931 if(!json_grammar_parser.parse(json_parser_ctx, doc)) {
932 log_error("parsing of JSON data \"$\" failed!", identifier);
933 return {};
934 }
935 return doc;
936}
937
938template <typename T> static pair<bool, T> extract_value(const document& doc, const string& path) {
939 // empty path -> return root value

Callers 1

create_documentFunction · 0.85

Calls 4

validate_utf8_stringFunction · 0.85
parseMethod · 0.80
insertMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected