summaryrefslogtreecommitdiff
path: root/test/main.cpp
blob: e86ec068a9ce1f0b3c9e82ef31fee57221872167 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <json.hpp>
#include <tokenizer.h>

#include <iostream>
#include <memory>

using std::cout, std::endl;

class TestClass {
private:
    int id;
    // std::string* testData;

public:
    // 1. Default Constructor: Sets initial "empty" state [20, 24]
    TestClass() : id(-1)/*, testData(new std::string("default"))*/ {
        std::cout << "Default Constructor called" << std::endl;
    }

    // 2. Parameterized Constructor: For dependency injection/direct testing [24, 25]
    TestClass(int id/*, std::string val*/) : id(id)/*, testData(new std::string(val))*/ {
        std::cout << "Parameterized Constructor (id: " << id << ")" << std::endl;
    }

    // 3. Copy Constructor (Rule of 5): Deep copies resource [14, 23]
    TestClass(const TestClass& other) : id(other.id+1)/*, testData(new std::string(*other.testData))*/ {
        std::cout << "Copy Constructor called" << id << std::endl;
    }

    // 4. Move Constructor (Rule of 5): Transfers ownership of resource [14, 16]
    TestClass(TestClass&& other) noexcept : id(other.id+1)/*, testData(other.testData)*/ {
        // other.testData = nullptr;
        other.id = -1;
        std::cout << "Move Constructor called" << id << std::endl;
    }

    // 5. Destructor: Essential for RAII and preventing memory leaks [14, 29]
    ~TestClass() {
        // delete testData;
        std::cout << "Destructor called" << id << std::endl;
    }

    // Assignment Operators (Standard Rule of 5)
    TestClass& operator=(const TestClass& other) {
        if (this == &other) return *this;
        // delete testData;
        id = other.id + 1;
        // testData = new std::string(*other.testData);
		std::cout << "Assignment op 1 called" << id << std::endl;
        return *this;
    }

    TestClass& operator=(TestClass&& other) noexcept {
        if (this == &other) return *this;
        // delete testData;
        id = other.id + 1;
        // testData = other.testData;
        // other.testData = nullptr;
		std::cout << "Assignment op 2 called" << id << std::endl;
        return *this;
    }

    // Helper for verification [18, 21]
    int getId() const { return id; }
    // std::string getData() const { return testData ? *testData : "null"; }
};


template <> std::string TehJSON::JSON::serializeData<int>(std::shared_ptr<void> data)
{
	return std::to_string(*static_cast<int*>(data.get()));
}
template <> std::string TehJSON::JSON::serializeData<float>(std::shared_ptr<void> data)
{
	return std::to_string(*static_cast<float*>(data.get()));
}
template <> std::string TehJSON::JSON::serializeData<TestClass>(std::shared_ptr<void> data)
{
	return "test class";
}
template <> std::string TehJSON::JSON::serializeData<std::string>(std::shared_ptr<void> data)
{
	return '"' + *static_cast<std::string*>(data.get()) + '"';
}


int main()
{
	TehJSON::JSON jsonWriter;
	// TestClass test1;
	// json["test1"].set(test1);
	jsonWriter["test_string"].set<std::string>("stringy");
	jsonWriter["test_int"].set<int>(123);
	jsonWriter["test_float"].set<float>(51.8);
	jsonWriter["test_object"]["test_int"].set<int>(100);
	jsonWriter["test_object"]["test_float"].set<float>(100);

	std::string jsonString = jsonWriter.getSerialized();
	cout << jsonString << endl;

	// TehJSON::Tokenizer tokenizer;
	// tokenizer.appendInput(jsonString);
	// std::vector<TehJSON::Token> tokens = tokenizer.tokenize();

	// for(const auto& token : tokens)
	// {
	// 	cout << TehJSON::getTokenName(token.type) << ": " << token.content << endl;
	// }

	TehJSON::JSON jsonReader;
	cout << "Reading: " << endl;
	jsonReader.readFromString(jsonString);
	cout << jsonReader.getSerialized() << endl;
}