Skip to content

Commit 4756025

Browse files
committed
Added example project.
Made escape and escapeKey functions public. Modified escape function to create " around strings to get the same look like json.
1 parent 41dab71 commit 4756025

File tree

12 files changed

+273
-78
lines changed

12 files changed

+273
-78
lines changed

Example/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(example)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
# Disable building cppcson tests
7+
set(CPPCSON_BUILD_TESTS OFF)
8+
9+
# Add cppcson as sub directory
10+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/.. ${CMAKE_CURRENT_BINARY_DIR}/cppcson)
11+
12+
# Define our executable and link to cppcson
13+
add_executable(example
14+
main.cpp
15+
)
16+
17+
target_link_libraries(example cppcson)

Example/Data/1.cson

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
# an array
3+
abc: [
4+
'a'
5+
'b'
6+
'c'
7+
]
8+
9+
# an object
10+
a:
11+
b: 'c'
12+
}

Example/Data/2.cson

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Look Ma! Comments!!!
2+
3+
# Look Ma! An Array with no commas!
4+
greatDocumentaries: [
5+
'earthlings.com'
6+
'forksoverknives.com'
7+
'cowspiracy.com'
8+
]
9+
10+
# Look Ma! An Object without braces!
11+
importantFacts:
12+
# Look Ma! Multi-Line Strings! Without Quote Escaping!
13+
emissions: '''
14+
Livestock and their byproducts account for at least 32,000 million tons of carbon dioxide (CO2) per year, or 51% of all worldwide greenhouse gas emissions.
15+
Goodland, R Anhang, J. “Livestock and Climate Change: What if the key actors in climate change were pigs, chickens and cows?”
16+
WorldWatch, November/December 2009. Worldwatch Institute, Washington, DC, USA. Pp. 10–19.
17+
http://www.worldwatch.org/node/6294
18+
'''
19+
20+
landuse: '''
21+
Livestock covers 45% of the earth’s total land.
22+
Thornton, Phillip, Mario Herrero, and Polly Ericksen. “Livestock and Climate Change.” Livestock Exchange, no. 3 (2011).
23+
https://cgspace.cgiar.org/bitstream/handle/10568/10601/IssueBrief3.pdf
24+
'''
25+
26+
burger: '''
27+
One hamburger requires 660 gallons of water to produce – the equivalent of 2 months’ worth of showers.
28+
Catanese, Christina. “Virtual Water, Real Impacts.” Greenversations: Official Blog of the U.S. EPA. 2012.
29+
http://blog.epa.gov/healthywaters/2012/03/virtual-water-real-impacts-world-water-day-2012/
30+
“50 Ways to Save Your River.” Friends of the River.
31+
http://www.friendsoftheriver.org/site/PageServer?pagename=50ways
32+
'''
33+
34+
milk: '''
35+
1,000 gallons of water are required to produce 1 gallon of milk.
36+
“Water trivia facts.” United States Environmental Protection Agency.
37+
http://water.epa.gov/learn/kids/drinkingwater/water_trivia_facts.cfm#_edn11
38+
'''
39+
40+
more: 'http://cowspiracy.com/facts'

Example/Data/3.cson

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# comment
2+
[1,2,3]
3+
# comment

Example/Data/4.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"abc":["a","b","c"],"a":{"b":"c"}}

Example/Data/8.cson

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

Example/Data/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Example Files
2+
3+
These files were taken from
4+
[here](https://github.com/bevry/cson/tree/master/test-fixtures/src).

Example/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# cppcson Example
2+
3+
This folder contains an example application illustrating how the cppcson
4+
library can be used.
5+
6+
The application reads in a passed \*.cson/\*.json file and prints the content in
7+
formatted cson to the console.
8+
9+
Input files for testing can be found in the *Data* folder.
10+
11+
## How to build?
12+
13+
On Unix systems:
14+
15+
```
16+
mkdir build
17+
cd build
18+
19+
cmake ..
20+
make
21+
```
22+
23+
On Windows:
24+
25+
In the Visual Studio command line:
26+
27+
```
28+
mkdir build
29+
cd build
30+
31+
cmake -G "NMake Makefiles" ..
32+
nmake
33+
```

Example/main.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <cppcson.hpp>
2+
#include <fstream>
3+
#include <iostream>
4+
5+
static void print(const cppcson::Value &value, bool isRoot, int32_t indent) {
6+
if (value.isNull()) {
7+
std::cout << "null";
8+
} else if (value.isBool()) {
9+
std::cout << value.asBool();
10+
} else if (value.isInt()) {
11+
std::cout << value.asInt();
12+
} else if (value.isFloat()) {
13+
std::cout << value.asFloat();
14+
} else if (value.isString()) {
15+
std::cout << cppcson::escape(value.asString());
16+
} else if (value.isArray()) {
17+
std::cout << "[";
18+
19+
indent += 2;
20+
for (auto &item : value) {
21+
std::cout << std::endl << std::string(indent, ' ');
22+
print(item, false, indent);
23+
}
24+
indent -= 2;
25+
26+
std::cout << std::endl << std::string(indent, ' ') << "]";
27+
} else if (value.isObject()) {
28+
if (!isRoot) {
29+
indent += 2;
30+
}
31+
32+
auto skipNewline = isRoot;
33+
34+
for (auto &key : value.keys()) {
35+
if (skipNewline) {
36+
skipNewline = false;
37+
} else {
38+
std::cout << std::endl;
39+
}
40+
41+
std::cout << std::string(indent, ' ') << cppcson::escapeKey(key) << ": ";
42+
print(value.item(key), false, indent);
43+
}
44+
}
45+
}
46+
47+
static void print(const cppcson::Value &value) {
48+
print(value, true, 0);
49+
std::cout << std::endl;
50+
}
51+
52+
int main(int argc, char const *const *argv) {
53+
if (argc != 2) {
54+
std::cout << "Error: Please pass a path to a cson file" << std::endl;
55+
return 1;
56+
}
57+
58+
std::string path(argv[1]);
59+
std::ifstream stream(path);
60+
61+
if (stream.fail()) {
62+
std::cout << "Error: File " << path << " could not be read" << std::endl;
63+
return 2;
64+
}
65+
66+
try {
67+
auto root = cppcson::parse(stream);
68+
69+
print(root);
70+
return 0;
71+
} catch (const cppcson::Error &e) {
72+
std::cout << "Error: The file " << path << " could not be parsed as cson"
73+
<< std::endl
74+
<< "Details:" << std::endl
75+
<< e.getLocation() << std::endl
76+
<< e.what() << std::endl;
77+
return 3;
78+
}
79+
}

Include/cppcson.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,8 @@ extern const Options DEFAULT_OPTIONS;
234234

235235
Value parse(std::istream &stream, const Options &options = DEFAULT_OPTIONS);
236236

237+
std::string escapeKey(const std::string &str);
238+
239+
std::string escape(const std::string &str);
240+
237241
} // namespace cppcson

0 commit comments

Comments
 (0)