| 905 | } |
| 906 | |
| 907 | document 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 | |
| 938 | template <typename T> static pair<bool, T> extract_value(const document& doc, const string& path) { |
| 939 | // empty path -> return root value |
no test coverage detected