Skip to content

Commit e6d6119

Browse files
committed
first commit
0 parents  commit e6d6119

File tree

8 files changed

+890
-0
lines changed

8 files changed

+890
-0
lines changed

.gitignore

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# This file is used to ignore files which are generated
2+
# ----------------------------------------------------------------------------
3+
4+
*~
5+
*.autosave
6+
*.a
7+
*.core
8+
*.moc
9+
*.o
10+
*.obj
11+
*.orig
12+
*.rej
13+
*.so
14+
*.so.*
15+
*_pch.h.cpp
16+
*_resource.rc
17+
*.qm
18+
.#*
19+
*.*#
20+
core
21+
!core/
22+
tags
23+
.DS_Store
24+
.directory
25+
*.debug
26+
Makefile*
27+
*.prl
28+
*.app
29+
moc_*.cpp
30+
ui_*.h
31+
qrc_*.cpp
32+
Thumbs.db
33+
*.res
34+
*.rc
35+
/.qmake.cache
36+
/.qmake.stash
37+
38+
# qtcreator generated files
39+
*.pro.user*
40+
CMakeLists.txt.user*
41+
42+
# xemacs temporary files
43+
*.flc
44+
45+
# Vim temporary files
46+
.*.swp
47+
48+
# Visual Studio generated files
49+
*.ib_pdb_index
50+
*.idb
51+
*.ilk
52+
*.pdb
53+
*.sln
54+
*.suo
55+
*.vcproj
56+
*vcproj.*.*.user
57+
*.ncb
58+
*.sdf
59+
*.opensdf
60+
*.vcxproj
61+
*vcxproj.*
62+
63+
# MinGW generated files
64+
*.Debug
65+
*.Release
66+
67+
# Python byte code
68+
*.pyc
69+
70+
# Binaries
71+
# --------
72+
*.dll
73+
*.exe
74+

CMakeLists.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(TaggedJsonObject LANGUAGES CXX)
4+
5+
set(CMAKE_AUTOUIC ON)
6+
set(CMAKE_AUTOMOC ON)
7+
set(CMAKE_AUTORCC ON)
8+
9+
set(CMAKE_CXX_STANDARD 11)
10+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
11+
12+
13+
######################### Examples ############################
14+
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
15+
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
16+
17+
add_executable(Example
18+
Examples/example.cpp
19+
inc/taggedjsonobject.h
20+
Examples/example.json
21+
inc/taggedjsonobjectmacros.h
22+
inc/map.h
23+
)
24+
target_include_directories(Example PRIVATE inc)
25+
target_link_libraries(Example Qt${QT_VERSION_MAJOR}::Core)
26+
27+
######################## Tests ###############################
28+
29+
# GTest package directives
30+
include(FetchContent)
31+
FetchContent_Declare(googletest
32+
GIT_REPOSITORY https://github.com/google/googletest
33+
GIT_TAG release-1.11.0)
34+
FetchContent_GetProperties(googletest)
35+
36+
if(NOT googletest_POPULATED)
37+
FetchContent_Populate(googletest)
38+
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BUILD_DIR})
39+
endif()
40+
41+
# Test files
42+
add_executable(testRunner Tests/taggedjsonobject_test.cpp
43+
)
44+
target_include_directories(testRunner PRIVATE inc)
45+
target_link_libraries(testRunner
46+
Qt${QT_VERSION_MAJOR}::Core
47+
gtest_main
48+
gmock_main)
49+
50+

Examples/example.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "taggedjsonobject.h"
2+
#include "taggedjsonobjectmacros.h"
3+
4+
/*
5+
Let's say, example.json file contains the following:
6+
7+
{
8+
"example_int": 42,
9+
"example_str": "Hello world!",
10+
"example_double": 35.5,
11+
"example_sub_class":{
12+
"example_sub_str": "Hello from an object!"
13+
},
14+
"example_arr": ["Hello", "World"],
15+
"example_mixed_arr": [42, "is", "the", "answer", "to", "everything"]
16+
}
17+
*/
18+
19+
DEFINE_JSON_TAGGED_OBJECT(InnerClass,
20+
(TaggedJSONString, example_sub_str))
21+
22+
DEFINE_JSON_TAGGED_OBJECT(OuterClass,
23+
(TaggedJSONInt, example_int),
24+
(TaggedJSONString, example_str),
25+
(TaggedJSONDouble, example_double),
26+
(InnerClass, example_sub_class),
27+
(TaggedJSONStringArray, example_arr),
28+
(TaggedJSONVariantArray, example_mixed_arr))
29+
30+
31+
int main(int argc, char *argv[])
32+
{
33+
QString jsonLocation("../../Examples/example.json");
34+
35+
//Create an instance of the tagged object that has been defined by the macro
36+
OuterClass exampleObject(jsonLocation);
37+
38+
//Alternatively exampleObject.example_int.get() can be used as well
39+
qDebug() << *exampleObject.example_int; //42
40+
41+
//Mutating stored values is allowed as long as the class isn't const
42+
exampleObject.example_int = -1;
43+
qDebug() << exampleObject.example_int; //-1
44+
45+
//Defining Tagged JSON objects inside another tagged JSON object allows chain access operations
46+
qDebug() << *exampleObject.example_sub_class.example_sub_str; // "Hello from an object!"
47+
48+
//Arrays can be accessed by indexing
49+
qDebug() << exampleObject.example_arr[0]; //"Hello"
50+
exampleObject.example_arr.at(0) = "Another";
51+
qDebug() << exampleObject.example_arr; //"Another World"
52+
53+
//Iterator access is available but it will be presented as QJsonValue
54+
[[maybe_unused]] const QJsonArray::const_iterator it = exampleObject.example_arr->begin();
55+
foreach(const QJsonValue& curText, *exampleObject.example_arr)
56+
qDebug() << curText.toString(); //"Another<newline>World";
57+
58+
//JSON arrays with varied types can be integrated by TaggedJSONVariantArray
59+
int answer_to_everything = exampleObject.example_mixed_arr[0].toInt();
60+
QString text_everything = exampleObject.example_mixed_arr[5].toString();
61+
62+
qDebug() << answer_to_everything;
63+
qDebug() << text_everything;
64+
65+
return 0;
66+
}

Examples/example.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"example_int": 42,
3+
"example_str": "Hello world!",
4+
"example_double": 35.5,
5+
"example_sub_class":{
6+
"example_sub_str": "Hello from an object!"
7+
},
8+
"example_arr": ["Hello", "World"],
9+
"example_mixed_arr": [42, "is", "the", "answer", "to", "everything"]
10+
}

0 commit comments

Comments
 (0)