Skip to content

Commit 4ba8562

Browse files
committed
Split into multiple files & add finish arrays
Signed-off-by: TymianekPL <[email protected]>
1 parent ceadea5 commit 4ba8562

File tree

5 files changed

+171
-173
lines changed

5 files changed

+171
-173
lines changed

Test/Test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <cppjson/object.hpp>
1+
#include <cppjson/cppjson.hpp>
22
#include <print>
33

44
int main()
@@ -13,6 +13,7 @@ int main()
1313
array[] = 6.0;
1414
array[0] = 1;
1515
array[] = "Stirng";
16+
array.EmplaceBack(nullptr);
1617
try
1718
{
1819
array[2] = true;

cppjson/include/cppjson/cppjson.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#pragma once
22

3-
namespace cppjson
4-
{
5-
void hello_world();
6-
} // namespace cppjson
3+
#include "object.hpp"
4+
#include "formatter.hpp"

cppjson/include/cppjson/formatter.hpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include "object.hpp"
2+
#include <format>
3+
4+
template <>
5+
struct std::formatter<cppjson::JsonObject>
6+
{
7+
constexpr auto parse(std::format_parse_context& context) { return context.begin(); }
8+
9+
auto format(const cppjson::JsonObject& object, std::format_context& context) const
10+
{
11+
switch (object._dataType)
12+
{
13+
case cppjson::JsonType::Null: return std::format_to(context.out(), "null");
14+
case cppjson::JsonType::Bool: return std::format_to(context.out(), "{}", object.DangerousAs<bool>());
15+
case cppjson::JsonType::Number: return std::format_to(context.out(), "{}", object.DangerousAs<double>());
16+
case cppjson::JsonType::String: return std::format_to(context.out(), "\"{}\"", object.DangerousAs<std::string>());
17+
case cppjson::JsonType::Object:
18+
{
19+
const auto& node = object.DangerousAs<cppjson::Object>();
20+
21+
std::string built = "{ ";
22+
for (const auto& [key, value] : node._nodes) built += std::format("\"{}\": {}, ", key, value);
23+
24+
if (!node._nodes.empty()) // remove trailing commas
25+
{
26+
built.pop_back();
27+
built.pop_back();
28+
built += " }";
29+
}
30+
else
31+
built += "}";
32+
33+
return std::format_to(context.out(), "{}", built);
34+
}
35+
case cppjson::JsonType::Array:
36+
{
37+
const auto& array = object.DangerousAs<cppjson::Array>();
38+
39+
std::string built = "[ ";
40+
for (const auto& element : array._objects) built += std::format("{}, ", element);
41+
42+
if (!array._objects.empty()) // remove trailing commas
43+
{
44+
built.pop_back();
45+
built.pop_back();
46+
built += " ]";
47+
}
48+
else
49+
built += "]";
50+
51+
return std::format_to(context.out(), "{}", built);
52+
}
53+
}
54+
55+
throw std::logic_error("Unknown type");
56+
}
57+
};
58+
59+
template <>
60+
struct std::formatter<cppjson::Object>
61+
{
62+
constexpr auto parse(std::format_parse_context& context) { return context.begin(); }
63+
64+
auto format(const cppjson::Object& object, std::format_context& context) const
65+
{
66+
std::string built = "{ ";
67+
for (const auto& [key, value] : object._nodes) built += std::format("\"{}\": {}, ", key, value);
68+
69+
if (!object._nodes.empty()) // remove trailing commas
70+
{
71+
built.pop_back();
72+
built.pop_back();
73+
built += " }";
74+
}
75+
else
76+
built += "}";
77+
78+
return std::format_to(context.out(), "{}", built);
79+
}
80+
};
81+
82+
template <>
83+
struct std::formatter<cppjson::Array>
84+
{
85+
constexpr auto parse(std::format_parse_context& context) { return context.begin(); }
86+
87+
auto format(const cppjson::Array& array, std::format_context& context) const
88+
{
89+
std::string built = "[ ";
90+
for (const auto& element : array._objects) built += std::format("{}, ", element);
91+
92+
if (!array._objects.empty()) // remove trailing commas
93+
{
94+
built.pop_back();
95+
built.pop_back();
96+
built += " ]";
97+
}
98+
else
99+
built += "]";
100+
101+
return std::format_to(context.out(), "{}", built);
102+
}
103+
};
104+
105+
template <>
106+
struct std::formatter<cppjson::Object::ObjectProxy>
107+
{
108+
constexpr auto parse(std::format_parse_context& context) { return context.begin(); }
109+
110+
auto format(const cppjson::Object::ObjectProxy& object, std::format_context& context) const
111+
{
112+
return std::format_to(context.out(), "{}", object._object.get());
113+
}
114+
};
115+
116+
template <>
117+
struct std::formatter<cppjson::Object::ConstObjectProxy>
118+
{
119+
constexpr auto parse(std::format_parse_context& context) { return context.begin(); }
120+
121+
auto format(const cppjson::Object::ConstObjectProxy& object, std::format_context& context) const
122+
{
123+
return std::format_to(context.out(), "{}", object._object.get());
124+
}
125+
};

0 commit comments

Comments
 (0)