Skip to content

Commit bc2d331

Browse files
committed
vscode intellisense bug finally fixedgit status
1 parent d132fa5 commit bc2d331

File tree

6 files changed

+322
-346
lines changed

6 files changed

+322
-346
lines changed

example/main.cpp

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
11
// Includes required
2-
#include <json/object.h>
32
#include <json/parse.h>
43

54
// Using namespace
65
using namespace json;
76

8-
int main(int argc, char **argv) {
7+
int main(int argc, char *argv[]) {
98
object person;
9+
person["name"] = "Adit";
1010
person["age"] = 21;
11-
person.insert("name", "Adit");
12-
person["height"] = 186.5;
11+
person.insert("height", 186.5);
1312
person["alive"] = true;
14-
15-
object languages;
16-
languages.push_back("German");
17-
languages.push_back("English");
18-
languages[0] = "Hindi";
19-
20-
person["languages"] = languages;
21-
person["languages"].push_back("French");
22-
logger::info(person.dumps(2));
13+
person["links"]["github"] = "https://github.com/jadit19";
14+
person["links"]["linkedin"] = "https://www.linkedin.com/in/jadit19/";
15+
16+
object college;
17+
college["name"] = "Indian Institute of Technology Kanpur";
18+
college["graduation"] = 2024;
19+
college["major"] = "Electrical Engineering";
20+
college["minors"].push_back("Operating Systems");
21+
college["minors"].push_back("Machine Learning");
22+
college["minors"].resize(3);
23+
college["minors"][2] = "English Literature";
24+
person["college"] = college;
25+
26+
person["languages"].resize(1);
27+
person["languages"][0] = "English";
28+
person["languages"].push_back("Hindi");
29+
person["languages"].push_back("Spanish");
30+
person["languages"][2] = "French";
31+
32+
parser parser;
33+
std::string data = person.dumps();
34+
object duplicate = parser.loads(data);
35+
36+
logger::info(duplicate.dumps(2));
2337

2438
std::string name = person["name"];
25-
logger::info(name);
26-
2739
int age = person["age"];
28-
logger::info(std::to_string(age));
29-
3040
bool alive = person["alive"];
31-
logger::info(alive ? "Alive" : "Dead");
32-
33-
parser jsonParser;
34-
object obj = jsonParser.loads(person.dumps(2));
35-
logger::success(obj.dumps(2));
41+
logger::info(name + " is " + std::to_string(age) + " years old and is " +
42+
(alive ? "alive" : "dead") + ".");
3643

3744
return EXIT_SUCCESS;
3845
}

include/json/object.h

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ namespace json {
1515

1616
class object {
1717
private:
18-
using value =
18+
using Value =
1919
std::variant<std::nullptr_t, bool, long long int, double, std::string>;
20-
using all_values =
21-
std::variant<std::nullptr_t, bool, long long int, double, std::string,
22-
std::vector<object>, std::map<std::string, object>>;
20+
using AllValues = std::variant<std::nullptr_t, bool, long long int, double,
21+
std::string, std::vector<json::object>,
22+
std::map<std::string, json::object>>;
2323

24-
type _type;
24+
json::TYPE type;
2525

2626
void clearVariant();
2727

@@ -34,58 +34,63 @@ class object {
3434
void assertIsMap();
3535

3636
protected:
37-
value _value;
38-
std::vector<object> _array;
39-
std::map<std::string, object> _map;
37+
json::object::Value value;
38+
std::vector<json::object> array;
39+
std::map<std::string, json::object> map;
4040

4141
public:
42+
// Constructors
4243
object();
43-
object(all_values data);
44+
object(json::object::AllValues data);
45+
object(const char *data);
4446
~object();
4547

46-
// Equal to operators
47-
object &operator=(std::nullptr_t data);
48-
object &operator=(bool data);
49-
object &operator=(int data);
50-
object &operator=(long long int data);
51-
object &operator=(double data);
52-
object &operator=(const char *data);
53-
object &operator=(std::vector<object> data);
54-
object &operator=(std::map<std::string, object> data);
55-
56-
// Array operators
57-
object &operator[](const size_t index);
58-
const object &operator[](const size_t index) const;
48+
// Assignment operators
49+
json::object &operator=(std::nullptr_t data);
50+
json::object &operator=(bool data);
51+
json::object &operator=(int data);
52+
json::object &operator=(long long int data);
53+
json::object &operator=(double data);
54+
json::object &operator=(const char *data);
55+
json::object &operator=(const std::string &data);
56+
json::object &operator=(std::vector<json::object> data);
57+
json::object &operator=(std::map<std::string, json::object> data);
58+
59+
// Array operations
60+
json::object &operator[](const size_t index);
61+
json::object &operator[](const int index);
62+
void push_back(const json::object &data);
5963
void pop_back();
60-
void push_back(all_values data);
61-
void push_back(object data);
62-
void reserve(size_t size);
63-
void resize(size_t size);
64+
void reserve(const size_t size);
65+
void resize(const size_t size);
6466
void shrink_to_fit();
6567

66-
// Map operators
67-
object &operator[](const std::string &key);
68-
const object &operator[](const std::string &key) const;
69-
void erase(const std::string key);
68+
// Map operations
69+
json::object &operator[](const char *key);
70+
json::object &operator[](const std::string &key);
7071
std::map<std::string, json::object>::iterator find(const std::string key);
71-
void insert(const std::string key, all_values data);
72+
std::map<std::string, json::object>::iterator begin();
73+
std::map<std::string, json::object>::iterator end();
74+
void insert(const std::string key, json::object::AllValues value);
7275

7376
// Array and Map operators
74-
size_t size();
7577
void clear();
78+
size_t size() const;
7679

77-
// object operators
80+
// Object operators
7881
void reset();
79-
void dump(std::string path, size_t indent = 0);
80-
std::string dumps(size_t indent = 0, size_t baseIndent = 0);
82+
void dump(std::string filename, size_t indent = 0) const;
83+
std::string dumps(size_t indent = 0, size_t baseIndent = 0) const;
8184

82-
// Output functions
83-
friend std::ostream &operator<<(std::ostream &os, object &obj);
84-
85-
operator std::nullptr_t() const;
86-
// operator bool() const;
87-
operator double() const;
85+
// Conversion Operators
8886
operator std::string() const;
87+
operator int() const;
88+
operator long long int() const;
89+
operator double() const;
90+
operator bool() const;
91+
92+
// Output functions
93+
friend std::ostream &operator<<(std::ostream &os, const json::object &obj);
8994
};
9095

9196
} // namespace json

include/json/type.h

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,15 @@
22

33
namespace json {
44

5-
class type {
6-
private:
7-
int _type;
8-
9-
public:
10-
type();
11-
~type();
12-
13-
static const int undefined = 0;
14-
static const int null = 1;
15-
static const int boolean = 2;
16-
static const int integer = 3;
17-
static const int number = 4;
18-
static const int string = 5;
19-
static const int array = 6;
20-
static const int map = 7;
21-
22-
void set(int newType);
23-
int get() const;
5+
enum TYPE {
6+
UNDEFINED = 0,
7+
NUL = 1,
8+
BOOLEAN = 2,
9+
INTEGER = 3,
10+
NUMBER = 4,
11+
STRING = 5,
12+
ARRAY = 6,
13+
OBJECT = 7
2414
};
2515

2616
} // namespace json

0 commit comments

Comments
 (0)