summaryrefslogtreecommitdiff
path: root/src/json.h
blob: efde542db3bf2397edce21020a8bf15fcbb19ae9 (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
#pragma once

#include "tokenizer.h"

#include <cstddef>
#include <map>
#include <memory>
#include <string>
#include <vector>

namespace TehJSON
{
	class JSON
	{
	public:
		JSON();
		JSON(const JSON& other) = default;
		~JSON();

		// Writing methods
		std::string getSerialized();
		std::string _getSerialized(int currIndent);

		// Reading methods
		void readFromString(std::string s);

		// Leaf methods
		template <typename T>
		T& get();
		template <typename T>
		void set(T value);
		std::string leafType();
		template <typename T>
		static std::string serializeData(std::shared_ptr<void> data);

		// Non leaf methods
		JSON& operator[](std::string name);
		size_t childCount();
		// std::vector<std::string> childNames();
	private:
		bool isLeaf = false;

		// Reading data fields
		std::vector<Token> tokens;
		int tokenPos = 0;
		Token consume();
		Token consume(TokenType type);
		TokenType nextTokenType();
		int readFromTokens(std::vector<Token> tokens, int pos);

		// Leaf data fields
		std::shared_ptr<void> data;
		std::string (*dataSerializer)(std::shared_ptr<void>);

		// Not leaf data fields
		std::map<std::string, JSON> children;
	};
}