Skip to content

Commit 5a41ab8

Browse files
committed
README.md added
1 parent e6d6119 commit 5a41ab8

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
# TaggedJSONObject
3+
4+
Using the JSON support from the Qt Framework, this library allows user to define labeled JSON objects that understands the type it currently holds, inspired from the Typescript's JSON interfaces. This comes with the following benefits:
5+
1) Introduces the type awareness for the target JSON field. This allows the IDE to autocomplete the fields and their potential methods. Type awareness also prevents potential runtime errors that comes from mistyping or type safety related mistakes.
6+
2) The object checks for the missing fields from the json data (it can be disabled)
7+
3) Class interface reduces the verbosity while accessing the desired field
8+
9+
10+
11+
12+
13+
## Installation
14+
15+
Requirements for this library are as follows:
16+
1) QT Framework (Qt5 or later)
17+
2) C++11 or later
18+
\
19+
This library only uses the header files, so building the project isn't required. \
20+
Building the project executes the tests and runs the code example.
21+
22+
## Usage/Examples
23+
24+
The following example is based on the "Examples/example.cpp" example file.\
25+
Let's say, a file named "example.json" contains the following json data:
26+
```json
27+
{
28+
"example_int": 42,
29+
"example_str": "Hello world!",
30+
"example_double": 35.5,
31+
"example_sub_class":{
32+
"example_sub_str": "Hello from an object!"
33+
},
34+
"example_arr": ["Hello", "World"],
35+
"example_mixed_arr": [42, "is", "the", "answer", "to", "everything"]
36+
}
37+
```
38+
To parse this data, first we need to define our tagged JSON object class with the help of the "DEFINE_JSON_TAGGED_OBJECT" preprocessor macro. Each field of the defined class should match with the field names of the json data
39+
40+
```c++
41+
/*
42+
This is the definition for the inner object which will be used for defining the outer object.
43+
Alternatively, the inner class can be defined as TaggedQJsonObject at the outer class but this
44+
prevents inner class members to be accessed directly, which defeats the purpose of this library.
45+
*/
46+
DEFINE_JSON_TAGGED_OBJECT(InnerClass,
47+
(TaggedJSONString, example_sub_str))
48+
49+
/*
50+
The first parameter of the macro is the name of the class, which is OuterClass for this instance.
51+
Other parameters are the type-field pairs that defines the members of the defined object.
52+
It should be noted that TaggedJSONObject class variants should be used for the field types instead of
53+
the regular types like int, double etc.
54+
*/
55+
DEFINE_JSON_TAGGED_OBJECT(OuterClass,
56+
(TaggedJSONInt, example_int),
57+
(TaggedJSONString, example_str),
58+
(TaggedJSONDouble, example_double),
59+
(InnerClass, example_sub_class),
60+
(TaggedJSONStringArray, example_arr),
61+
(TaggedJSONVariantArray, example_mixed_arr))
62+
```
63+
The intention of the statements above has the similar intentions with the statements form the following Typescript interfaces
64+
65+
```typescript
66+
interface InnerClass{
67+
example_sub_str: string
68+
}
69+
70+
interface OuterClass{
71+
example_int: number;
72+
example_str: string;
73+
example_double: number;
74+
example_sub_class: InnerClass;
75+
example_arr: Array<string>;
76+
example_mixed_arr: Array<any>;
77+
}
78+
```
79+
Later on, we can declare an instance by, either passing a QByteArray that stores the json data, by passing a QJsonObject or by passing the QString that holds the path of the json document.
80+
81+
```c++
82+
QString jsonLocation("../../Examples/example.json");
83+
84+
//Create an instance of the tagged object that has been defined by the macro
85+
OuterClass exampleObject(jsonLocation);
86+
87+
//Alternatively exampleObject.example_int.get() can be used as well
88+
qDebug() << *exampleObject.example_int; //42
89+
90+
//Mutating stored values is allowed as long as the class isn't const
91+
exampleObject.example_int = -1;
92+
qDebug() << exampleObject.example_int; //-1
93+
94+
//Defining Tagged JSON objects inside another tagged JSON object allows chain access operations
95+
qDebug() << *exampleObject.example_sub_class.example_sub_str; // "Hello from an object!"
96+
97+
//Arrays can be accessed by indexing
98+
qDebug() << exampleObject.example_arr[0]; //"Hello"
99+
exampleObject.example_arr.at(0) = "Another";
100+
qDebug() << exampleObject.example_arr; //"Another World"
101+
102+
//Iterator access is available but it will be presented as QJsonValue
103+
const QJsonArray::const_iterator it = exampleObject.example_arr->begin();
104+
foreach(const QJsonValue& curText, *exampleObject.example_arr)
105+
qDebug() << curText.toString(); //"Another<newline>World";
106+
107+
//JSON arrays with varied types can be integrated by TaggedJSONVariantArray
108+
int answer_to_everything = exampleObject.example_mixed_arr[0].toInt();
109+
QString text_everything = exampleObject.example_mixed_arr[5].toString();
110+
111+
qDebug() << answer_to_everything; //42
112+
qDebug() << text_everything; //everything
113+
```
114+
115+
116+
## License
117+
118+
Like Qt library, this library is under the dual license LGPLv3 and GPLv2.
119+

0 commit comments

Comments
 (0)