Skip to content

Version 2.0 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
service_name: travis-pro
repo_token: F9bs4Nop10JRgqPQXRcifyQKYhb3FczkS
16 changes: 15 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
language: cpp

compiler:
- clang
- gcc

before_install:
- sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
- sudo apt-get update -qq
- if [ "$CXX" = "g++" ]; then sudo apt-get install -qq g++-4.8; fi
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi
- sudo pip install cpp-coveralls

before_script:
- autoreconf -iv
Expand All @@ -10,3 +17,10 @@ before_script:
script:
- make
- make check

after_success:
- make clean
- make json_unit CXXFLAGS="-fprofile-arcs -ftest-coverage"
- ./json_unit
- coveralls --exclude lib --exclude tests --gcov-options '\-lp'

2,331 changes: 2,331 additions & 0 deletions Doxyfile

Large diffs are not rendered by default.

34 changes: 18 additions & 16 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
noinst_PROGRAMS = json json98 json98benchmark
TESTS = ./json ./json98
noinst_PROGRAMS = json json_unit
TESTS = ./json ./json_unit

FLAGS = -Wall -Wextra -pedantic -Weffc++ -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch -Wundef -Wno-unused -Wnon-virtual-dtor -Wreorder

json_SOURCES = src/JSON.cc src/JSON.h test/JSON_test.cc
json_CXXFLAGS = -std=c++11
json_CXXFLAGS = $(FLAGS) -Weffc++ -std=c++11
json_CPPFLAGS = -I$(top_srcdir)/src

json98_SOURCES = src/JSON.cc src/JSON.h test/JSON_test.cc
json98_CXXFLAGS = -std=c++98
json98_CPPFLAGS = -I$(top_srcdir)/src
json_unit_SOURCES = src/JSON.cc src/JSON.h test/catch.hpp test/JSON_unit.cc
json_unit_CXXFLAGS = $(FLAGS) -std=c++11
json_unit_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/test -Dprivate=public

json98benchmark_SOURCES = src/JSON.cc src/JSON.h benchmark/JSON_benchmark.cc
json98benchmark_CXXFLAGS = -std=c++98 -O3
json98benchmark_CPPFLAGS = -I$(top_srcdir)/src -DNDEBUG
cppcheck:
cppcheck --enable=all --inconclusive --std=c++11 src/JSON.*

svn-clean: maintainer-clean
rm -fr configure INSTALL aclocal.m4 build-aux depcomp install-sh missing test-driver cover_html *.gcda *.gcno coverage*.info
rm -fr configure INSTALL aclocal.m4 build-aux depcomp install-sh missing test-driver
for DIR in $(DIST_SUBDIRS) .; do rm -f $$DIR/Makefile.in; done

cover:
make clean
make json98 CXXFLAGS+="--coverage -g -fprofile-arcs -ftest-coverage" CPPFLAGS+="-DNDEBUG"
./json98
lcov --capture --directory . --output-file coverage98.info
genhtml coverage*.info --output-directory cover_html --show-details --title "$(PACKAGE_STRING)" --legend --demangle-cpp
pretty:
astyle --style=allman --indent=spaces=4 --indent-modifiers \
--indent-switches --indent-preproc-block --indent-preproc-define \
--indent-col1-comments --pad-oper --pad-header --align-pointer=type \
--align-reference=type --add-brackets --convert-tabs --close-templates \
--lineend=linux --preserve-date --suffix=none \
$(SOURCES)
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

[![Build Status](https://travis-ci.org/nlohmann/json.png?branch=master)](https://travis-ci.org/nlohmann/json)

[![Coverage Status](https://img.shields.io/coveralls/nlohmann/json.svg)](https://coveralls.io/r/nlohmann/json)

## Design goals

There are myriads of [JSON](http://json.org) libraries out there, and each may even have its reason to exist. Our class had these design goals:

- **Trivial integration**. Our whole code consists of just two files: A header file `JSON.h` and a source file `JSON.cc`. That's it. No library, no subproject, no dependencies. The class is written in vanilla C++98 and -- if possible -- uses some features of C++11 such as move constructors. All in all, the class should require no adjustment of your compiler flags or project settings.
- **Trivial integration**. Our whole code consists of just two files: A header file `JSON.h` and a source file `JSON.cc`. That's it. No library, no subproject, no dependencies. The class is written in vanilla C++11. All in all, the class should require no adjustment of your compiler flags or project settings.

- **Intiuitve syntax**. In languages such as Python, JSON feels like a first class data type. We used all the operator magic of C++ to achieve the same feeling in your code. Check out the [examples below](#examples) and you know, what I mean.
- **Intuitive syntax**. In languages such as Python, JSON feels like a first class data type. We used all the operator magic of C++ to achieve the same feeling in your code. Check out the [examples below](#examples) and you know, what I mean.

Other aspects were not so important to us:

- **Memory efficiency**. Each JSON object has an overhead of one pointer (the maximal size of a union) and one enumeration element (1 byte). We use the following C++ data types: `std::string` for strings, `int` or `double` for numbers, `std::map` for objects, `std::vector` for arrays, and `bool` for Booleans. We know that there are more efficient ways to store the values, but we are happy enough right now. And by the way: [Valgrind](http://valgrind.org) says our code is free of leaks.

- **Speed**. We currently implement the parser as naive [recursive descent parser](http://en.wikipedia.org/wiki/Recursive_descent_parser) with hand coded string handling. It is fast enough, but a [LALR-parser](http://en.wikipedia.org/wiki/LALR_parser) with a decent regular expression processor should be even faster (but would consist of more files which makes the integration harder).

- **Rigourous standard compliance**. We followed the [specification](http://json.org) as close as possible, but did not invest too much in a 100% compliance with respect to Unicode support. As a result, there might be edge cases of false positives and false negatives, but as long as we have a hand-written parser, we won't invest too much to be fully compliant.
- **Rigorous standard compliance**. We followed the [specification](http://json.org) as close as possible, but did not invest too much in a 100% compliance with respect to Unicode support. As a result, there might be edge cases of false positives and false negatives, but as long as we have a hand-written parser, we won't invest too much to be fully compliant.

## Integration

Expand Down Expand Up @@ -45,11 +47,17 @@ j["happy"] = true;
// add a string that is stored as std::string
j["name"] = "Niels";

// add a null object
j["nothing"] = nullptr;

// add an object inside the object
j["further"]["entry"] = 42;

// add an array that is stored as std::vector (C++11)
// add an array that is stored as std::vector
j["list"] = { 1, 0, 2 };

// add another object
j["object"] = { {"currency", "USD"}, {"value", "42.99"} };
```

### Input / Output
Expand All @@ -65,6 +73,11 @@ std::cout << j;

These operators work for any subclasses of `std::istream` or `std::ostream`.

```cpp
// create object from string literal
JSON j = "{ \"pi\": 3.141, \"happy\": true }"_json;
```

### STL-like access

```cpp
Expand All @@ -79,19 +92,19 @@ for (JSON::iterator it = j.begin(); it != j.end(); ++it) {
std::cout << *it << '\n';
}

// C++11 style
// range-based for
for (auto element : j) {
std::cout << element << '\n';
}

// getter/setter
std::string tmp = j[0];
const std::string tmp = j[0];
j[1] = 42;

// other stuff
j.size(); // 3
j.empty(); // false
j.type(); // JSON::array
j.type(); // JSON::valeu_type::array

// create an object
JSON o;
Expand Down
17 changes: 0 additions & 17 deletions benchmark/JSON_benchmark.cc

This file was deleted.

11 changes: 0 additions & 11 deletions benchmark/benchmark.py

This file was deleted.

7 changes: 0 additions & 7 deletions benchmark/download.sh

This file was deleted.

2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AC_INIT([JSON], [1.0], [[email protected]])
AC_INIT([JSON], [2.0], [[email protected]])
AC_CONFIG_SRCDIR([src/JSON.cc])

AM_INIT_AUTOMAKE([foreign subdir-objects])
Expand Down
Loading