-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better organization and testability of tests
- Loading branch information
Showing
11 changed files
with
175 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,29 @@ | ||
|
||
SHELL=/bin/sh | ||
|
||
LD := $(CXX) | ||
MKDIR := mkdir -p | ||
OBJ_DIR := .obj | ||
|
||
.SUFFIXES: | ||
|
||
$(OBJ_DIR)/%.o: %.cc | ||
$(CXX) $(CXXFLAGS) -MMD -c $< -o $@ | ||
|
||
TARGET = cpp-json | ||
|
||
CXXFLAGS := -std=c++11 -pedantic -Wextra -Wall -Iinclude -O2 -g3 | ||
LDFLAGS := | ||
H_FILES := | ||
CXX_FILES := main.cc | ||
|
||
O_FILES := $(patsubst %.cc, $(OBJ_DIR)/%.o, $(CXX_FILES)) | ||
D_FILES := $(O_FILES:.o=.d) | ||
|
||
SOURCEFILES := $(H_FILES) $(CXX_FILES) | ||
.PRECIOUS: $(SOURCEFILES) | ||
.PHONY: all clean | ||
|
||
all: $(TARGET) | ||
TARGETS := example1 example2 example3 example4 | ||
|
||
$(O_FILES): | $(OBJ_DIR) | ||
all: $(TARGETS) | ||
|
||
$(D_FILES): | $(OBJ_DIR) | ||
example1: example1.cc | ||
$(CXX) $(CXXFLAGS) $^ -o $@ | ||
|
||
example2: example2.cc | ||
$(CXX) $(CXXFLAGS) $^ -o $@ | ||
|
||
$(OBJ_DIR) : | ||
@$(MKDIR) $@ | ||
example3: example3.cc | ||
$(CXX) $(CXXFLAGS) $^ -o $@ | ||
|
||
$(TARGET): $(O_FILES) | ||
$(LD) $(LDFLAGS) $^ -o $@ | ||
example4: example4.cc | ||
$(CXX) $(CXXFLAGS) $^ -o $@ | ||
|
||
clean: | ||
$(RM) $(O_FILES) $(D_FILES) $(TARGET) | ||
$(RM) $(TARGETS) | ||
|
||
-include $(D_FILES) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
#include "cpp-json/json.h" | ||
#include <iostream> | ||
#include <fstream> | ||
|
||
//------------------------------------------------------------------------------ | ||
// Name: main | ||
//------------------------------------------------------------------------------ | ||
int main() { | ||
|
||
// construct from a file | ||
std::ifstream file("example1.json"); | ||
if(file) { | ||
json::value v1 = json::parse(file); | ||
std::cout << stringify(v1, json::PRETTY_PRINT | json::ESCAPE_UNICODE) << std::endl; | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
#include "cpp-json/json.h" | ||
#include <iostream> | ||
|
||
//------------------------------------------------------------------------------ | ||
// Name: main | ||
//------------------------------------------------------------------------------ | ||
int main() { | ||
|
||
// construct programatically (legacy C++03 API) | ||
json::object obj1; | ||
obj1.insert("test1", "hello world") | ||
.insert("test2", 10) | ||
.insert("test3", json::object().insert("x", json::null)) | ||
|
||
// if we are using C++11, we have a MUCH more elegant array initializer option | ||
#if __cplusplus >= 201103L | ||
.insert("test4", json::array().append(1, 2, 3, 4, "hello", true, 1.3)); | ||
#else | ||
.insert("test4", json::array().append(1).append(2).append(3).append(4).append("hello").append(true).append(1.3)); | ||
#endif | ||
|
||
std::cout << stringify(obj1, json::PRETTY_PRINT) << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
#include "cpp-json/json.h" | ||
#include <iostream> | ||
|
||
//------------------------------------------------------------------------------ | ||
// Name: main | ||
//------------------------------------------------------------------------------ | ||
int main() { | ||
|
||
// construct from string (C++11 raw string literals work nicely here!) | ||
#if __cplusplus >= 201103L | ||
auto v = json::parse(R"( | ||
{ | ||
"test3" : { | ||
"x" : 123.456 | ||
}, | ||
"test4" : [ | ||
1, | ||
2, | ||
3, | ||
{ | ||
"z" : 12345 | ||
} | ||
], | ||
"test1" : "hello world", | ||
"test2" : "BLAH\uD840\uDC8ABLAH" | ||
} | ||
)"); | ||
#else | ||
json::value v = json::parse("{\"test3\" : {\"x\" : 123.456},\"test4\" : [1,2,3,{\"z\":12345}],\"test1\" : \"hello world\",\"test2\" : \"BLAH\\uD840\\uDC8ABLAH\"}"); | ||
#endif | ||
std::cout << stringify(v, json::PRETTY_PRINT) << std::endl; | ||
std::cout << "----------" << std::endl; | ||
|
||
// get a specific value | ||
json::value z = v["test4"][3]["z"]; | ||
std::cout << to_number(z) << std::endl; | ||
|
||
std::cout << stringify(v, json::ESCAPE_UNICODE) << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
#include "cpp-json/json.h" | ||
#include <iostream> | ||
|
||
//------------------------------------------------------------------------------ | ||
// Name: main | ||
//------------------------------------------------------------------------------ | ||
int main() { | ||
|
||
// construct programmatically using object literal syntax in C++11 | ||
#if __cplusplus >= 201103L | ||
json::array arr = { | ||
1, | ||
2, | ||
3, | ||
4, | ||
"Testing 1 2 3", json::object{ | ||
{ "hello", 1234 }, | ||
{ "world", 5678 } | ||
} | ||
}; | ||
std::cout << stringify(arr) << std::endl; | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"n2" : -1234, | ||
"b2" : false, | ||
"n1" : null, | ||
"b1" : true, | ||
"u" : "\u1234", | ||
"test\nreturn" : "hello\tworld", | ||
"object1" : { | ||
"object2" : { | ||
"object3" : { | ||
"test4" : [ | ||
1, | ||
2, | ||
3, | ||
4 | ||
], | ||
"test3" : 3 | ||
}, | ||
"test2" : 2 | ||
} | ||
}, | ||
"test1" : 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"test4" : [ | ||
1, | ||
2, | ||
3, | ||
4, | ||
"hello", | ||
true, | ||
1.300000 | ||
], | ||
"test3" : { | ||
"x" : null | ||
}, | ||
"test2" : 10, | ||
"test1" : "hello world" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"test2" : "BLAH𠂊BLAH", | ||
"test1" : "hello world", | ||
"test4" : [ | ||
1, | ||
2, | ||
3, | ||
{ | ||
"z" : 12345 | ||
} | ||
], | ||
"test3" : { | ||
"x" : 123.456 | ||
} | ||
} | ||
---------- | ||
12345 | ||
{"test2":"BLAH\uD840\uDC8ABLAH","test1":"hello world","test4":[1,2,3,{"z":12345}],"test3":{"x":123.456}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[1,2,3,4,"Testing 1 2 3",{"world":5678,"hello":1234}] |