DAW JSON Link
daw_json_stack_tracer.h
Go to the documentation of this file.
1 // Copyright (c) Darrell Wright
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5 //
6 // Official repository: https://github.com/beached/daw_json_link
7 //
8 
9 #pragma once
10 
11 #include "daw_json_assert.h"
12 #include "daw_json_event_parser.h"
13 
14 #include <algorithm>
15 #include <string>
16 #include <vector>
17 
18 namespace daw::json {
19 
20  inline std::string find_json_path_to( json_exception const &jex,
21  char const *doc_start ) {
22  if( not jex.parse_location( ) or not doc_start ) {
23  return { };
24  }
25 
26  struct stack_value {
27  std::string name;
28  bool is_class;
29  };
30 
31  struct handler_t {
32  char const *first;
33  char const *last;
34  std::vector<stack_value> parse_stack{ };
35  std::string member_name{ };
36  long long index = -1;
37 
38  bool handle_on_value( json_pair jp ) {
39  if( last >= jp.value.get_string_view( ).data( ) ) {
40  return false;
41  }
42  if( parse_stack.empty( ) ) {
43  return true;
44  }
45  using namespace std::string_literals;
46  if( parse_stack.back( ).is_class ) {
47  member_name = "."s + ( jp.name ? static_cast<std::string>( *jp.name )
48  : std::string{ } );
49  return true;
50  }
51  ++index;
52  member_name = "["s + std::to_string( index ) + "]"s;
53  return true;
54  }
55 
56  bool handle_on_array_start( json_value jv ) {
57  parse_stack.push_back( { member_name, false } );
58  return true;
59  }
60 
61  bool handle_on_array_end( json_value jv ) {
62  parse_stack.pop_back( );
63  return true;
64  }
65 
66  bool handle_on_class_start( json_value jv ) {
67  parse_stack.push_back( { member_name, false } );
68  return true;
69  }
70 
71  bool handle_on_class_end( json_value jv ) {
72  parse_stack.pop_back( );
73  return true;
74  }
75  } handler{ doc_start, jex.parse_location( ) + 1 };
76  json_event_parser( doc_start, handler );
77 
78  return std::accumulate(
79  handler.parse_stack.begin( ), handler.parse_stack.end( ), std::string{ },
80  []( std::string state, stack_value const &sv ) { return sv.name; } );
81  }
82 } // namespace daw::json
daw_json_event_parser.h
daw::json::basic_json_pair
Definition: daw_json_value.h:34
daw::json::to_string
constexpr std::string_view to_string(JsonBaseParseTypes pt)
Definition: daw_json_parse_common.h:312
daw::json
Definition: daw_json_assert.h:50
daw::json::basic_json_pair::name
std::optional< std::string_view > name
Definition: daw_json_value.h:35
daw::json::json_event_parser
constexpr void json_event_parser(daw::json::basic_json_value< ParsePolicy > jvalue, Handler &&handler)
Definition: daw_json_event_parser.h:291
daw::json::find_json_path_to
std::string find_json_path_to(json_exception const &jex, char const *doc_start)
Definition: daw_json_stack_tracer.h:20
daw::json::basic_json_value
Definition: daw_json_value.h:297
daw::json::basic_json_pair::value
basic_json_value< Range > value
Definition: daw_json_value.h:36
daw_json_assert.h
daw::json::json_exception
Definition: daw_json_assert.h:218
daw::json::json_exception::parse_location
char const * parse_location() const
Definition: daw_json_assert.h:279