Create an example directory and add some code examples. (#944)

* update example directory

* modify some compile error.

* update with clang-format

* update

* update

* add_definitions("../include/json")

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Jul 10 21:26:16 2019 +0800
#
# On branch code_example
# Your branch is up-to-date with 'origin/code_example'.
#
# Changes to be committed:
#	modified:   example/CMakeLists.txt
#

* change CMakeLists.txt

* update streamWrite.cpp

* update

* Update readFromStream.cpp

* fix typo
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
new file mode 100644
index 0000000..c8eba53
--- /dev/null
+++ b/example/CMakeLists.txt
@@ -0,0 +1,29 @@
+#vim: et ts =4 sts = 4 sw = 4 tw = 0
+cmake_minimum_required(VERSION 3.1)
+
+set(EXAMPLES
+	readFromString
+	readFromStream
+	stringWrite
+	streamWrite
+   )
+add_definitions(-D_GLIBCXX_USE_CXX11_ABI)
+set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS})
+
+if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
+	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra ")
+else()	
+	add_definitions(
+	-D_SCL_SECURE_NO_WARNINGS
+	-D_CRT_SECURE_NO_WARNINGS
+	-D_WIN32_WINNT=0x601
+	-D_WINSOCK_DEPRECATED_NO_WARNINGS)
+endif()
+
+foreach (example ${EXAMPLES})
+	add_executable(${example} ${example}/${example}.cpp)
+	target_include_directories(${example} PUBLIC ${CMAKE_SOURCE_DIR}/include)
+	target_link_libraries(${example} jsoncpp_lib)
+endforeach()
+
+add_custom_target(examples ALL DEPENDS ${EXAMPLES})
diff --git a/example/README.md b/example/README.md
new file mode 100644
index 0000000..b1ae4c8
--- /dev/null
+++ b/example/README.md
@@ -0,0 +1,13 @@
+***NOTE*** 
+
+If you get linker errors about undefined references to symbols that involve types in the `std::__cxx11` namespace or the tag
+`[abi:cxx11]` then it probably indicates that you are trying to link together object files that were compiled with different
+values for the _GLIBCXX_USE_CXX11_ABI marco. This commonly happens when linking to a third-party library that was compiled with 
+an older version of GCC. If the third-party library cannot be rebuilt with the new ABI, then you need to recompile your code with
+the old ABI,just like:
+**g++ stringWrite.cpp -ljsoncpp -std=c++11 -D_GLIBCXX_USE_CXX11_ABI=0 -o stringWrite**
+
+Not all of uses of the new ABI will cause changes in symbol names, for example a class with a `std::string` member variable will
+have the same mangled name whether compiled with the older or new ABI. In order to detect such problems, the new types and functions
+are annotated with the abi_tag attribute, allowing the compiler to warn about potential ABI incompatibilities in code using them.
+Those warnings can be enabled with the `-Wabi-tag` option.
diff --git a/example/readFromStream/errorFormat.json b/example/readFromStream/errorFormat.json
new file mode 100644
index 0000000..2d13290
--- /dev/null
+++ b/example/readFromStream/errorFormat.json
@@ -0,0 +1,3 @@
+{
+    1: "value"
+}
\ No newline at end of file
diff --git a/example/readFromStream/readFromStream.cpp b/example/readFromStream/readFromStream.cpp
new file mode 100644
index 0000000..358d2ca
--- /dev/null
+++ b/example/readFromStream/readFromStream.cpp
@@ -0,0 +1,30 @@
+#include "json/json.h"
+#include <fstream>
+#include <iostream>
+/** \brief Parse from stream, collect comments and capture error info.
+ * Example Usage:
+ * $g++ readFromStream.cpp -ljsoncpp -std=c++11 -o readFromStream
+ * $./readFromStream
+ * // comment head
+ * {
+ *    // comment before
+ *    "key" : "value"
+ * }
+ * // comment after
+ * // comment tail
+ */
+int main(int argc, char* argv[]) {
+  Json::Value root;
+  std::ifstream ifs;
+  ifs.open(argv[1]);
+
+  Json::CharReaderBuilder builder;
+  builder["collectComments"] = true;
+  JSONCPP_STRING errs;
+  if (!parseFromStream(builder, ifs, &root, &errs)) {
+    std::cout << errs << std::endl;
+    return EXIT_FAILURE;
+  }
+  std::cout << root << std::endl;
+  return EXIT_SUCCESS;
+}
diff --git a/example/readFromStream/withComment.json b/example/readFromStream/withComment.json
new file mode 100644
index 0000000..7a8c828
--- /dev/null
+++ b/example/readFromStream/withComment.json
@@ -0,0 +1,6 @@
+// comment head
+{
+    // comment before
+    "key" : "value"
+    // comment after
+}// comment tail
diff --git a/example/readFromString/readFromString.cpp b/example/readFromString/readFromString.cpp
new file mode 100644
index 0000000..753f9c9
--- /dev/null
+++ b/example/readFromString/readFromString.cpp
@@ -0,0 +1,37 @@
+#include "json/json.h"
+#include <iostream>
+/**
+ * \brief Parse a raw string into Value object using the CharReaderBuilder
+ * class, or the legacy Reader class. 
+ * Example Usage: 
+ * $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString
+ * $./readFromString
+ * colin
+ * 20
+ */
+int main() {
+  const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
+  const int rawJsonLength = static_cast<int>(rawJson.length());
+  constexpr bool shouldUseOldWay = false;
+  JSONCPP_STRING err;
+  Json::Value root;
+
+  if (shouldUseOldWay) {
+    Json::Reader reader;
+    reader.parse(rawJson, root);
+  } else {
+    Json::CharReaderBuilder builder;
+    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
+    if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
+                       &err)) {
+      std::cout << "error" << std::endl;
+      return EXIT_FAILURE;
+    }
+  }
+  const std::string name = root["Name"].asString();
+  const int age = root["Age"].asInt();
+
+  std::cout << name << std::endl;
+  std::cout << age << std::endl;
+  return EXIT_SUCCESS;
+}
diff --git a/example/streamWrite/streamWrite.cpp b/example/streamWrite/streamWrite.cpp
new file mode 100644
index 0000000..6f7f797
--- /dev/null
+++ b/example/streamWrite/streamWrite.cpp
@@ -0,0 +1,22 @@
+#include "json/json.h"
+#include <iostream>
+/** \brief Write the Value object to a stream.
+ * Example Usage:
+ * $g++ streamWrite.cpp -ljsoncpp -std=c++11 -o streamWrite
+ * $./streamWrite
+ * {
+ *     "Age" : 20,
+ *     "Name" : "robin"
+ * }
+ */
+int main() {
+  Json::Value root;
+  Json::StreamWriterBuilder builder;
+  const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
+
+  root["Name"] = "robin";
+  root["Age"] = 20;
+  writer->write(root, &std::cout);
+
+  return EXIT_SUCCESS;
+}
diff --git a/example/stringWrite/stringWrite.cpp b/example/stringWrite/stringWrite.cpp
new file mode 100644
index 0000000..d501ba9
--- /dev/null
+++ b/example/stringWrite/stringWrite.cpp
@@ -0,0 +1,33 @@
+#include "json/json.h"
+#include <iostream>
+/** \brief Write a Value object to a string.
+ * Example Usage:
+ * $g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite
+ * $./stringWrite
+ * {
+ *     "action" : "run",
+ *     "data" :
+ *     {
+ *         "number" : 1
+ *     }
+ * }
+ */
+int main() {
+  Json::Value root;
+  Json::Value data;
+  constexpr bool shouldUseOldWay = false;
+  root["action"] = "run";
+  data["number"] = 1;
+  root["data"] = data;
+
+  if (shouldUseOldWay) {
+    Json::FastWriter writer;
+    const std::string json_file = writer.write(root);
+    std::cout << json_file << std::endl;
+  } else {
+    Json::StreamWriterBuilder builder;
+    const std::string json_file = Json::writeString(builder, root);
+    std::cout << json_file << std::endl;
+  }
+  return EXIT_SUCCESS;
+}