1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*-------------------------------------------------------------------------
*
* jsonpath.h
* JSONPath implementation routines for JSON data type support.
*
* The "JSONPath" implemented here is similar to, but not exactly the same as,
* the JSONPath described at http://goessner.net/articles/JsonPath/ .
* The main differences are stronger subscripting rules, special methods
* via function-call notation (currently, the only one provided is .char()),
* and that the '$' at the beginning of a JSONPath expression is optional.
* Also, array indices as a set (e.g. [0,1]) and filters/scripts are currently
* not implemented. Array indices are a planned feature. True filters
* would require an honest-to-goodness JavaScript engine, so perhaps
* someone will write a module for that in the future.
*
* Copyright (c) 2010, PostgreSQL Global Development Group
* Written by Joey Adams <[email protected]>.
*
*-------------------------------------------------------------------------
*/
#ifndef JSONPATH_H
#define JSONPATH_H
#include "json.h"
#include "nodes/pg_list.h"
typedef enum
{
JP_ROOT,
JP_WILDCARD,
JP_INDEX_SUBSCRIPT,
JP_KEY_SUBSCRIPT,
JP_CALL_CHAR
} jp_element_type;
/*
* A jp_element is a single piece of a JSONPath expression
* (e.g. [3], .foo, .char(3), ..*). It represents subscripting
* down one level in a JSON tree, or invoking a special method
* (like .char() ). However, if recursive_descent is set to true,
* the subscript applies to a value and all of its descendants.
*/
typedef struct
{
jp_element_type type;
union
{
long index;
struct
{
char *ptr;
size_t length;
} key;
} data;
/* If element was preceded by ".." in pattern */
bool recursive_descent;
} jp_element;
typedef enum
{
JP_REF_NODE,
JP_REF_CHAR
} JPRefType;
/*
* A JPRef is a reference to some part of a JSON tree,
* typically a value (but not always).
*
* JPRef* is really a "subclass" of JSON* . In the future, this structure
* will likely be merged into the JSON structure to improve performance.
*/
typedef struct
{
JPRefType type;
union
{
JSON *node;
struct
{
const char *bytes;
size_t length;
} chr;
} u;
} JPRef;
typedef List /* jp_element* */ JSONPath;
JSONPath *jp_parse(const char *pattern);
char *jp_show(JSONPath * jp);
List /* JPRef* */ *jp_match(JSONPath * jp, JSON * json);
void jp_set(JSONPath * jp, JSON * json, JSON * value);
/* Returns the JSON encoding of the given reference. */
char *jpref_encode(JPRef * ref);
#endif
|