simdjson: include/simdjson/jsonpathutil.h Source File

1#ifndef SIMDJSON_JSONPATHUTIL_H

2#define SIMDJSON_JSONPATHUTIL_H

3

4#include <string>

5#include "simdjson/common_defs.h"

6

7#include <utility>

8

16 size_t i = 0;

17

18

19 if (!json_path.empty() && json_path.front() == '$') {

20 i = 1;

21 }

22 if (i >= json_path.size() || (json_path[i] != '.' &&

23 json_path[i] != '[')) {

24 return "-1";

25 }

26

27 std::string result;

28

29

30 result.reserve(json_path.size() * 2);

31

32 while (i < json_path.length()) {

33 if (json_path[i] == '.') {

34 result += '/';

35 } else if (json_path[i] == '[') {

36 result += '/';

37 ++i;

38 while (i < json_path.length() && json_path[i] != ']') {

39 if (json_path[i] == '~') {

40 result += "~0";

41 } else if (json_path[i] == '/') {

42 result += "~1";

43 } else {

44 result += json_path[i];

45 }

46 ++i;

47 }

48 if (i == json_path.length() || json_path[i] != ']') {

49 return "-1";

50 }

51 } else {

52 if (json_path[i] == '~') {

53 result += "~0";

54 } else if (json_path[i] == '/') {

55 result += "~1";

56 } else {

57 result += json_path[i];

58 }

59 }

60 ++i;

61 }

62

63 return result;

64}

65

66inline std::pair<std::string_view, std::string_view> get_next_key_and_json_path(std::string_view& json_path) {

67 std::string_view key;

68

69 if (json_path.empty()) {

70 return {key, json_path};

71 }

72 size_t i = 0;

73

74

75 if (json_path.front() == '$') {

76 i = 1;

77 }

78

79

80 if (i < json_path.length() && json_path[i] == '.') {

81 i += 1;

82 size_t key_start = i;

83

84 while (i < json_path.length() && json_path[i] != '[' && json_path[i] != '.') {

85 ++i;

86 }

87

88 key = json_path.substr(key_start, i - key_start);

89 } else if ((i+1 < json_path.size()) && json_path[i] == '[' && (json_path[i+1] == '\'' || json_path[i+1] == '"')) {

90 i += 2;

91 size_t key_start = i;

92 while (i < json_path.length() && json_path[i] != '\'' && json_path[i] != '"') {

93 ++i;

94 }

95

96 key = json_path.substr(key_start, i - key_start);

97

98 i += 2;

99 } else if ((i+2 < json_path.size()) && json_path[i] == '[' && json_path[i+1] == '*' && json_path[i+2] == ']') {

100 key = "*";

101 i += 3;

102 }

103

104

105 return std::make_pair(key, json_path.substr(i));

106}

107

108}

109#endif

The top level simdjson namespace, containing everything the library provides.

std::string json_path_to_pointer_conversion(std::string_view json_path)

Converts JSONPath to JSON Pointer.