Skip to content

Commit 4e83d94

Browse files
committed
test and include dir
1 parent 5910fe0 commit 4e83d94

File tree

6 files changed

+119
-5
lines changed

6 files changed

+119
-5
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
test/
2-
.vscode/
1+
.vscode/
2+
build/
3+
test/build/

include/json.hh renamed to include/jpp.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file json.hh
33
* @author Simone Ancona
44
* @brief A JSON parser for C++
5-
* @version 1.1.1
5+
* @version 1.1.2
66
* @date 2023-07-15
77
*
88
* @copyright Copyright (c) 2023

src/json.cc renamed to src/jpp.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
* @file json.cc
33
* @author Simone Ancona
44
* @brief
5-
* @version 1.1.1
5+
* @version 1.1.2
66
* @date 2023-07-15
77
*
88
* @copyright Copyright (c) 2023
99
*
1010
*/
1111

12-
#include "../include/json.hh"
12+
#include "jpp.hh"
1313

1414
bool Jpp::Json::is_array()
1515
{

test/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.0.0)
2+
set(CMAKE_CXX_STANDARD 20)
3+
project(jpp_test)
4+
add_executable(jpp_test test.cc ../src/jpp.cc)
5+
file(COPY json/ DESTINATION Debug/json)
6+
target_include_directories(jpp_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../include")

test/json/e1.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"quiz": {
3+
"sport": {
4+
"q1": {
5+
"question": "Which one is correct team name in NBA?",
6+
"options": [
7+
"New York Bulls",
8+
"Los Angeles Kings",
9+
"Golden State Warriros",
10+
"Huston Rocket"
11+
],
12+
"answer": "Huston Rocket"
13+
}
14+
},
15+
"maths": {
16+
"difficult": true,
17+
"q1": {
18+
"question": "5 + 7 = ?",
19+
"options": [
20+
"10",
21+
"11",
22+
"12",
23+
"13"
24+
],
25+
"answer": "12"
26+
},
27+
"q2": {
28+
"question": "12 - 8 = ?",
29+
"options": [
30+
"1",
31+
"2",
32+
"3",
33+
"4"
34+
],
35+
"answer": "4"
36+
}
37+
}
38+
}
39+
}

test/test.cc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "jpp.hh"
2+
#include <fstream>
3+
#include <sstream>
4+
#include <iostream>
5+
6+
std::string read_string_from_file(const std::string &);
7+
8+
int main(int argc, char **argv)
9+
{
10+
try
11+
{
12+
Jpp::Json json;
13+
Jpp::Json json1;
14+
json.parse(read_string_from_file("json/e1.json"));
15+
std::cout << json.to_string() << "\n"
16+
<< std::endl;
17+
std::cout << json["quiz"]["maths"].to_string() << "\n"
18+
<< std::endl;
19+
json["hello"] = "world";
20+
std::cout << json.to_string() << "\n"
21+
<< std::endl;
22+
23+
std::cout << json1.to_string() << "\n"
24+
<< std::endl;
25+
json1["name"] = "simon";
26+
json1["surname"] = Jpp::Json(nullptr);
27+
std::cout << json1.to_string() << "\n"
28+
<< std::endl;
29+
30+
Jpp::Json car;
31+
Jpp::Json car_collection;
32+
car["brand"] = "Brand1";
33+
car["age"] = 10;
34+
car["model"] = "Model1";
35+
36+
car_collection["favoriteCar"] = car;
37+
38+
std::cout << car_collection.to_string() << "\n"
39+
<< std::endl;
40+
41+
Jpp::Json array;
42+
array.parse("[1, 2, 3, \"Hello World\"]");
43+
std::cout << array.to_string() << std::endl;
44+
for (auto a : array)
45+
{
46+
std::cout << a.second.to_string() << std::endl;
47+
}
48+
}
49+
catch (const std::exception e)
50+
{
51+
std::cout << e.what() << std::endl;
52+
}
53+
}
54+
55+
std::string read_string_from_file(const std::string &file_path)
56+
{
57+
const std::ifstream input_stream(file_path, std::ios_base::binary);
58+
59+
if (input_stream.fail())
60+
{
61+
throw std::runtime_error("Failed to open file");
62+
}
63+
64+
std::stringstream buffer;
65+
buffer << input_stream.rdbuf();
66+
67+
return buffer.str();
68+
}

0 commit comments

Comments
 (0)