QtProtobuf  0.6
Protobuf plugin to generate Qt classes
microjson.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
5  *
6  * This file is part of microjson project https://git.semlanik.org/semlanik/microjson
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
9  * software and associated documentation files (the "Software"), to deal in the Software
10  * without restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
12  * to permit persons to whom the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all copies
16  * or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <stddef.h>
27 #include <stdint.h>
28 
29 #include <functional>
30 
31 #include <string>
32 #include <vector>
33 #include <unordered_map>
34 
35 namespace microjson {
36 enum JsonType {
37  JsonNumberType,
38  JsonStringType,
39  JsonBoolType,
40  JsonObjectType,
41  JsonArrayType,
42  JsonInvalidType
43 };
44 
45 struct JsonValue {
46  JsonValue() : type(JsonInvalidType) {}
47  JsonValue(const std::string &_value, JsonType _type) :
48  value(_value)
49  , type(_type) {}
50  std::string value;
51  JsonType type;
52 };
53 
54 struct JsonProperty {
55  JsonProperty() : nameBegin(SIZE_MAX)
56  , nameEnd(SIZE_MAX)
57  , valueBegin(SIZE_MAX)
58  , valueEnd(SIZE_MAX)
59  , type(microjson::JsonInvalidType){}
60 
61  size_t nameBegin;
62  size_t nameEnd;
63  size_t valueBegin;
64  size_t valueEnd;
65  microjson::JsonType type;
66 
67  size_t nameSize() const {
68  return nameEnd - nameBegin;
69  }
70 
71  size_t valueSize() const {
72  return valueEnd - valueBegin + 1;
73  }
74 
75  bool checkValue() const {
76  return type != microjson::JsonInvalidType && valueBegin != SIZE_MAX && valueEnd != SIZE_MAX;
77  }
78 
79  bool checkEof() const {
80  return (type == microjson::JsonNumberType || type == microjson::JsonBoolType) &&
81  valueBegin != SIZE_MAX && valueEnd == SIZE_MAX;
82  }
83 
84  bool check() const {
85  return checkValue() && nameBegin != SIZE_MAX && nameEnd != SIZE_MAX &&
86  nameBegin < nameEnd && nameEnd < valueBegin;
87  }
88 };
89 
90 using JsonObject = std::unordered_map<std::string, JsonValue>;
91 using JsonArray = std::vector<JsonValue>;
92 
93 extern JsonArray parseJsonArray(const char *buffer, size_t size);
94 extern JsonObject parseJsonObject(const char *buffer, size_t size);
95 
96 inline bool skipWhiteSpace(const char byte) {
97  return byte == '\n' || byte == ' ' || byte == '\r' || byte == '\t' || byte == '\f' || byte == '\v';
98 }
99 
100 extern bool extractValue(const char *buffer, size_t size, size_t &i, const char expectedEndByte, microjson::JsonProperty &property);
101 extern bool extractProperty(const char *buffer, size_t size, size_t &i, const char expectedEndByte, microjson::JsonProperty &property);
102 }
Definition: microjson.h:54
Definition: microjson.h:45