Skip to content

Commit 51cbce7

Browse files
Merge pull request #5 from terbshaeusser/develop
Release 0.2
2 parents 0505402 + 09d851a commit 51cbce7

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

Example/main.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33
#include <iostream>
44

55
static void print(const cppcson::Value &value, bool isRoot, int32_t indent) {
6-
if (value.isNull()) {
7-
std::cout << "null";
8-
} else if (value.isBool()) {
6+
switch (value.getKind()) {
7+
case cppcson::Value::Kind::Bool: {
98
std::cout << value.asBool();
10-
} else if (value.isInt()) {
9+
break;
10+
}
11+
case cppcson::Value::Kind::Int: {
1112
std::cout << value.asInt();
12-
} else if (value.isFloat()) {
13+
break;
14+
}
15+
case cppcson::Value::Kind::Float: {
1316
std::cout << value.asFloat();
14-
} else if (value.isString()) {
17+
break;
18+
}
19+
case cppcson::Value::Kind::String: {
1520
std::cout << cppcson::escape(value.asString());
16-
} else if (value.isArray()) {
21+
break;
22+
}
23+
case cppcson::Value::Kind::Null: {
24+
std::cout << "null";
25+
break;
26+
}
27+
case cppcson::Value::Kind::Array: {
1728
std::cout << "[";
1829

1930
indent += 2;
@@ -24,23 +35,32 @@ static void print(const cppcson::Value &value, bool isRoot, int32_t indent) {
2435
indent -= 2;
2536

2637
std::cout << std::endl << std::string(indent, ' ') << "]";
27-
} else if (value.isObject()) {
38+
break;
39+
}
40+
case cppcson::Value::Kind::Object: {
2841
if (!isRoot) {
2942
indent += 2;
3043
}
3144

3245
auto skipNewline = isRoot;
3346

34-
for (auto &key : value.keys()) {
35-
if (skipNewline) {
36-
skipNewline = false;
37-
} else {
38-
std::cout << std::endl;
39-
}
47+
if (value.getItemCount() == 0) {
48+
std::cout << "{}";
49+
} else {
50+
for (auto &key : value.keys()) {
51+
if (skipNewline) {
52+
skipNewline = false;
53+
} else {
54+
std::cout << std::endl;
55+
}
4056

41-
std::cout << std::string(indent, ' ') << cppcson::escapeKey(key) << ": ";
42-
print(value.item(key), false, indent);
57+
std::cout << std::string(indent, ' ') << cppcson::escapeKey(key)
58+
<< ": ";
59+
print(value.item(key), false, indent);
60+
}
4361
}
62+
break;
63+
}
4464
}
4565
}
4666

Include/cppcson.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ class Keys {
116116
class Value {
117117
friend class Parser;
118118

119-
private:
119+
public:
120120
enum class Kind { Bool, Int, Float, String, Null, Array, Object };
121121

122+
private:
122123
union NonStrValue {
123124
bool boolValue;
124125
int64_t intValue;
@@ -177,6 +178,8 @@ class Value {
177178

178179
const std::string &getPath() const;
179180

181+
Kind getKind() const;
182+
180183
bool isBool() const;
181184

182185
bool isInt() const;

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ Features:
88
* Meta information for parsed values (line, column etc.)
99
* Proper error reporting
1010
* No dependencies
11+
* Thread safe
12+
* Support for \u escapes in strings
1113

1214
Tested on:
1315

1416
* Linux g++ 8.3.0
1517
* macOS Clang 10.0.1
1618
* Windows Visual Studio 2019
1719

20+
Note:
21+
22+
* The parser interprets the input as encoded in UTF-8. To use other Unicode
23+
encodings such as UTF-16, have a look on
24+
[utf8streams](https://github.com/terbshaeusser/utf8streams).
25+
1826
## Requirements
1927

2028
* CMake (version 3.10 or later)

Source/cppcson.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ const Location &Value::getLocation() const { return location; }
256256

257257
const std::string &Value::getPath() const { return path; }
258258

259+
Value::Kind Value::getKind() const { return kind; }
260+
259261
bool Value::isBool() const { return kind == Kind::Bool; }
260262

261263
bool Value::isInt() const { return kind == Kind::Int; }

0 commit comments

Comments
 (0)