xref: /5.5.2/platform/include/JSON_checker.h (revision 5c6a39bc)
1/* JSON_checker.h */
2
3#pragma once
4
5#include <platform/JSON_checker-visibility.h>
6
7#ifdef __cplusplus
8
9#include <cstdint>
10#include <vector>
11#include <string>
12#include <stack>
13
14/**
15 * Variant of std::stack which exposes the clear() method from the
16 * underlying container.
17 *
18 * Adds an additional requirement that Container supports clear();
19 * however most STL containers (e.g. std::vector,std::deque and std::list)
20 * suppport this.
21 */
22template<class T, class Container = std::deque<T>>
23class ClearableStack : public std::stack<T, Container> {
24public:
25    void clear() {
26        this->c.clear();
27    }
28};
29
30namespace JSON_checker {
31    /**
32     * These modes can be pushed on the stack for the JSON parsing
33     */
34    enum class Modes : uint8_t {
35        ARRAY,
36        DONE,
37        KEY,
38        OBJECT
39    };
40
41    class Instance {
42    public:
43        /**
44         * Type to use for the state stack. We use ClearableStack for
45         * more efficient reset between validations.
46         */
47        using StackType = ClearableStack<Modes>;
48
49        Instance();
50
51        /**
52         * Reset the instance
53         */
54        void reset();
55
56        /**
57         * Push a mode onto the stack
58         * @return true on success
59         * @throws std::bad_alloc if we fail to grow the stack
60         */
61        bool push(Modes mode);
62
63        /**
64         * Pop the stack, assuring that the current mode matches the
65         * expectation.
66         * Return false if there is underflow or if the modes mismatch.
67         */
68        bool pop(Modes mode);
69
70        int state;
71        StackType stack;
72    };
73
74    class JSON_CHECKER_PUBLIC_API Validator {
75    public:
76        Validator();
77
78        /**
79         * Parse a chunk of data to see if it is valid JSON
80         *
81         * @param data pointer to the data to check
82         * @param size the number of bytes to check
83         * @return true if it is valid json, false otherwise
84         * @throws std::bad_alloc for memory allocation problems related to
85         *         the internal state array
86         */
87        bool validate(const uint8_t* data, size_t size);
88
89        /**
90         * Parse a chunk of data to see if it is valid JSON
91         *
92         * @param data the data to check
93         * @return true if it is valid json, false otherwise
94         * @throws std::bad_alloc for memory allocation problems related to
95         *         the internal state array
96         */
97        bool validate(const std::vector<uint8_t>& data);
98
99        /**
100         * Parse a chunk of data to see if it is valid JSON
101         *
102         * @param data the data to check
103         * @return true if it is valid json, false otherwise
104         * @throws std::bad_alloc for memory allocation problems related to
105         *         the internal state array
106         */
107        bool validate(const std::string& data);
108
109    private:
110        Instance instance;
111    };
112}
113
114extern "C" {
115#endif
116
117/**
118 * Allocate a json checker and parse data. This method is
119 * deprecated and will be removed once all usage of it is
120 * fixed
121 */
122JSON_CHECKER_PUBLIC_API
123bool checkUTF8JSON(const unsigned char* data, size_t size);
124
125#ifdef __cplusplus
126}
127#endif
128