Skip to content

Commit d4bba92

Browse files
committed
Add libjson for JSON parsing & serialization
1 parent 0ba18c0 commit d4bba92

File tree

4 files changed

+416
-0
lines changed

4 files changed

+416
-0
lines changed

applications/libjson/build.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
ROOT="../.."
3+
if [ -f "$ROOT/variables.sh" ]; then
4+
. "$ROOT/variables.sh"
5+
fi
6+
. "$ROOT/ghost.sh"
7+
8+
# Define build setup
9+
ARTIFACT_NAME="libjson.a"
10+
ARTIFACT_NAME_SHARED="libjson.so"
11+
12+
# Include application build tasks
13+
. "../libraries.sh"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2+
* *
3+
* Ghost, a micro-kernel based operating system for the x86 architecture *
4+
* Copyright (C) 2025, Max Schlüssel <[email protected]> *
5+
* *
6+
* This program is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* *
11+
* This program is distributed in the hope that it will be useful, *
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14+
* GNU General Public License for more details. *
15+
* *
16+
* You should have received a copy of the GNU General Public License *
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
18+
* *
19+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
20+
21+
#ifndef LIBJSON_JSON
22+
#define LIBJSON_JSON
23+
24+
#include "./json_node.hpp"
25+
#include <string>
26+
27+
class g_json
28+
{
29+
const char* start = nullptr;
30+
const char* source = nullptr;
31+
32+
void skipWhitespace();
33+
bool consume(char c);
34+
35+
std::string parseString();
36+
double parseNumber();
37+
g_json_node parseValue();
38+
g_json_node parseArray();
39+
g_json_node parseObject();
40+
41+
public:
42+
g_json_node parse(const std::string& s);
43+
std::string stringify(g_json_node& node);
44+
};
45+
46+
#endif
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2+
* *
3+
* Ghost, a micro-kernel based operating system for the x86 architecture *
4+
* Copyright (C) 2025, Max Schlüssel <[email protected]> *
5+
* *
6+
* This program is free software: you can redistribute it and/or modify *
7+
* it under the terms of the GNU General Public License as published by *
8+
* the Free Software Foundation, either version 3 of the License, or *
9+
* (at your option) any later version. *
10+
* *
11+
* This program is distributed in the hope that it will be useful, *
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14+
* GNU General Public License for more details. *
15+
* *
16+
* You should have received a copy of the GNU General Public License *
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
18+
* *
19+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
20+
21+
#ifndef LIBJSON_JSON_NODE
22+
#define LIBJSON_JSON_NODE
23+
24+
#include <string>
25+
#include <variant>
26+
#include <vector>
27+
#include <map>
28+
29+
struct g_json_node
30+
{
31+
using object = std::map<std::string, g_json_node>;
32+
using array = std::vector<g_json_node>;
33+
using value = std::variant<std::nullptr_t, bool, double, std::string, array, object>;
34+
35+
value v{};
36+
37+
g_json_node() : v(nullptr)
38+
{
39+
}
40+
41+
g_json_node(std::nullptr_t) : v(nullptr)
42+
{
43+
}
44+
45+
g_json_node(bool b) : v(b)
46+
{
47+
}
48+
49+
g_json_node(double d) : v(d)
50+
{
51+
}
52+
53+
g_json_node(const char* s) : v(std::string(s))
54+
{
55+
}
56+
57+
g_json_node(std::string s) : v(std::move(s))
58+
{
59+
}
60+
61+
g_json_node(array a) : v(std::move(a))
62+
{
63+
}
64+
65+
g_json_node(object o) : v(std::move(o))
66+
{
67+
}
68+
69+
bool isObject() const { return std::holds_alternative<object>(v); }
70+
bool isArray() const { return std::holds_alternative<array>(v); }
71+
bool isString() const { return std::holds_alternative<std::string>(v); }
72+
bool isNumber() const { return std::holds_alternative<double>(v); }
73+
bool isBool() const { return std::holds_alternative<bool>(v); }
74+
bool isNull() const { return std::holds_alternative<std::nullptr_t>(v); }
75+
76+
object& asObject() { return std::get<object>(v); }
77+
array& asArray() { return std::get<array>(v); }
78+
std::string& asString() { return std::get<std::string>(v); }
79+
double& asNumber() { return std::get<double>(v); }
80+
bool& asBool() { return std::get<bool>(v); }
81+
};
82+
83+
#endif

0 commit comments

Comments
 (0)