1 /*
2 The MIT License (MIT)
3 
4 Copyright (c) 2013-2015 Ivan Vashchaev
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in
8 the Software without restriction, including without limitation the rights to
9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 the Software, and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 
24 #pragma once
25 
26 #include <cassert>
27 #include <cstddef>
28 #include <cstdint>
29 
30 enum JsonTag
31 {
32     JSON_NUMBER = 0,
33     JSON_STRING,
34     JSON_ARRAY,
35     JSON_OBJECT,
36     JSON_TRUE,
37     JSON_FALSE,
38     JSON_NULL = 0xF
39 };
40 
41 struct JsonNode;
42 
43 #define JSON_VALUE_PAYLOAD_MASK 0x00007FFFFFFFFFFFULL
44 #define JSON_VALUE_NAN_MASK     0x7FF8000000000000ULL
45 #define JSON_VALUE_TAG_MASK     0xF
46 #define JSON_VALUE_TAG_SHIFT    47
47 
48 union JsonValue {
49     uint64_t ival;
50     double fval;
51 
JsonValue(double x)52     JsonValue(double x) : fval(x) {}
53     JsonValue(JsonTag tag = JSON_NULL, void *payload = nullptr)
54     {
55 #if !defined(__arm__)
56         assert((uint64_t)(uintptr_t)payload <= JSON_VALUE_PAYLOAD_MASK);
57 #endif
58         ival = JSON_VALUE_NAN_MASK | ((uint64_t)tag << JSON_VALUE_TAG_SHIFT) | (uintptr_t)payload;
59     }
isDouble()60     bool isDouble() const { return (int64_t)ival <= (int64_t)JSON_VALUE_NAN_MASK; }
getTag()61     JsonTag getTag() const
62     {
63         return isDouble() ? JSON_NUMBER : JsonTag((ival >> JSON_VALUE_TAG_SHIFT) & JSON_VALUE_TAG_MASK);
64     }
getPayload()65     uint64_t getPayload() const
66     {
67         assert(!isDouble());
68         return ival & JSON_VALUE_PAYLOAD_MASK;
69     }
toNumber()70     double toNumber() const
71     {
72         assert(getTag() == JSON_NUMBER);
73         return fval;
74     }
toString()75     char *toString() const
76     {
77         assert(getTag() == JSON_STRING);
78         return (char *)getPayload();
79     }
toNode()80     JsonNode *toNode() const
81     {
82         assert(getTag() == JSON_ARRAY || getTag() == JSON_OBJECT);
83         return (JsonNode *)getPayload();
84     }
85 };
86 
87 struct JsonNode
88 {
89     JsonValue value;
90     JsonNode *next;
91     char *key;
92 };
93 
94 struct JsonIterator
95 {
96     JsonNode *p;
97 
98     void operator++() { p = p->next; }
99     bool operator!=(const JsonIterator &x) const { return p != x.p; }
100     JsonNode *operator*() const { return p; }
101     JsonNode *operator->() const { return p; }
102 };
103 
begin(JsonValue o)104 inline JsonIterator begin(JsonValue o)
105 {
106     return JsonIterator{ o.toNode() };
107 }
end(JsonValue)108 inline JsonIterator end(JsonValue)
109 {
110     return JsonIterator{ nullptr };
111 }
112 
113 #define JSON_ERRNO_MAP(XX)                           \
114     XX(OK, "ok")                                     \
115     XX(BAD_NUMBER, "bad number")                     \
116     XX(BAD_STRING, "bad string")                     \
117     XX(BAD_IDENTIFIER, "bad identifier")             \
118     XX(STACK_OVERFLOW, "stack overflow")             \
119     XX(STACK_UNDERFLOW, "stack underflow")           \
120     XX(MISMATCH_BRACKET, "mismatch bracket")         \
121     XX(UNEXPECTED_CHARACTER, "unexpected character") \
122     XX(UNQUOTED_KEY, "unquoted key")                 \
123     XX(BREAKING_BAD, "breaking bad")                 \
124     XX(ALLOCATION_FAILURE, "allocation failure")
125 
126 enum JsonErrno
127 {
128 #define XX(no, str) JSON_##no,
129     JSON_ERRNO_MAP(XX)
130 #undef XX
131 };
132 
133 const char *jsonStrError(int err);
134 
135 class JsonAllocator
136 {
137     struct Zone
138     {
139         Zone *next;
140         size_t used;
141     } *head = nullptr;
142 
143   public:
144     JsonAllocator()                      = default;
145     JsonAllocator(const JsonAllocator &) = delete;
146     JsonAllocator &operator=(const JsonAllocator &) = delete;
JsonAllocator(JsonAllocator && x)147     JsonAllocator(JsonAllocator &&x) : head(x.head) { x.head = nullptr; }
148     JsonAllocator &operator=(JsonAllocator &&x)
149     {
150         head   = x.head;
151         x.head = nullptr;
152         return *this;
153     }
~JsonAllocator()154     ~JsonAllocator() { deallocate(); }
155     void *allocate(size_t size);
156     void deallocate();
157 };
158 
159 int jsonParse(char *str, char **endptr, JsonValue *value, JsonAllocator &allocator);
160