summaryrefslogtreecommitdiff
path: root/src/json.cpp
blob: c11ca6e2d027bafae3cc014e23c06045e6dbe923 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "json.h"

#include <cstddef>
#include <stdexcept>

namespace TehJSON
{
	JSON::JSON()
		:children()
	{
	}
	
	JSON::~JSON()
	{
	}

	std::string JSON::leafType()
	{
		if(!isLeaf)
			throw std::runtime_error("Node is not a leaf!");
		throw std::runtime_error("Not implemented yet");
	}

	std::string JSON::getSerialized()
	{
		return _getSerialized(0);
	}
	std::string JSON::_getSerialized(int currIndent)
	{
		if(isLeaf)
			return dataSerializer(data);

		std::string serialized = "{\n";

		int childrenLeft = childCount();
		for(auto [childName, child]: children)
		{
			childrenLeft--;
			for(int i = 0; i < currIndent + 1; i++)
				serialized += '\t';
			serialized += "\"" + childName + "\": ";
			serialized += child._getSerialized(currIndent + 1);
			if(childrenLeft != 0)
				serialized += ',';
			serialized += '\n';
		}
		for(int i = 0; i < currIndent; i++)
			serialized += '\t';
		serialized += "}";

		return serialized;
	}

	JSON& JSON::operator[](std::string name)
	{
		if(isLeaf)
			throw std::runtime_error("Node is a leaf!");
		// if(children.count(name) == 0)
		// 	throw std::out_of_range("Child \"" + name + "\" does not exist");
		return children[name];
	}

	size_t JSON::childCount()
	{
		if(isLeaf)
			throw std::runtime_error("Node is a leaf!");
		return children.size();
	}
}