项目作者: 5cript

项目描述 :
A JSON stringifier / parser that uses boost fusion introspection methods for automagic struct <-> JSON conversion (including allmost all STL containers)
高级语言: C++
项目地址: git://github.com/5cript/SimpleJSON.git
创建时间: 2015-01-25T00:10:53Z
项目社区:https://github.com/5cript/SimpleJSON

开源协议:MIT License

下载


" class="reference-link">SimpleJSON Travis CI

  1. Preface
  2. Introduction
  3. Example 1
  4. Example 2
  5. Useful Utility
  6. Details
  7. Reference

Preface

Please submit pull requests if you don’t agree with some behavior or found a bug, I would appreciate it.
The library is further matured now and changes less.

This library can parse and stringify and is designed for ease of use.
Nobody wants to write parsing and stringification methods for every class they write. We rather want it
to work “just like that” without thinking about it. This is where this library fits in.
This idea of producing and consuming JSON has become the “Hello World of Introspection”.

Since release 0.3, the library also features basic JSON beautification using boost iostreams.

Introduction

A JSON stringifier / parser that uses boost fusion introspection methods for automagic struct <-> JSON conversion

It supports almost all STL contstructs in stringify and the most important for parse.
With the STL as a basis it is an easy to extend mechanism using classes. Use boost fusion and the provided utility
(see example below) or provide your own parse/stringify methods.

NOTE: The performance of this library is mostly influenced by boost property tree which is used for parsing JSON.
The main focus of this library is not speed, but ease of use and convenience. If you want to be fast, try RapidJson (not saying it is particularly slow, but probably not suitable for high data frequency or big bulk data application)

Dependencies:

boost/property_tree

boost/fusion

boost/mpl

Example 0

  1. #ifndef Q_MOC_RUN // A Qt workaround, for those of you who use Qt
  2. # include <SimpleJSON/parse/jsd.hpp>
  3. # include <SimpleJSON/stringify/jss.hpp>
  4. # include <SimpleJSON/stringify/jss_fusion_adapted_struct.hpp>
  5. #endif
  6. struct Object : public JSON::Stringifiable <Object>
  7. , public JSON::Parsable <Object>
  8. {
  9. int A;
  10. std::string B;
  11. float C;
  12. };
  13. BOOST_FUSION_ADAPT_STRUCT(Object, A, B, C)
  14. int main()
  15. {
  16. auto o = JSON::make_from_json <Object> (R"(
  17. {
  18. "A": 0,
  19. "B": "Hello",
  20. "C": 2.4
  21. }
  22. )");
  23. }

Example 1

  1. #include <string>
  2. #include <vector>
  3. #include <sstream>
  4. struct ConfigContent
  5. {
  6. int id;
  7. std::string libPath;
  8. std::vector <std::string> someContainer;
  9. };
  10. BOOST_FUSION_ADAPT_STRUCT
  11. (
  12. ConfigContent,
  13. (int, id)
  14. (std::string, libPath)
  15. (std::vector <std::string>, someContainer)
  16. )
  17. SJSON_INJECT_STRINGIFY(ConfigContent)
  18. SJSON_INJECT_PARSE(ConfigContent)
  19. int main()
  20. {
  21. ConfigContent cc;
  22. cc.id = 2;
  23. cc.libPath = "./somewhere";
  24. cc.someContainer = {"Hello", "World"};
  25. std::stringstream sstr;
  26. JSON::stringify(sstr, "", cc);
  27. ConfigContent unnecessarilyComplexCopy;
  28. JSON::parse(unnecessarilyComplexCopy, "", JSON::parse_json(sstr));
  29. /////////////////////////////////////////////////////////////////////////
  30. // Lets check if we got what we set
  31. /////////////////////////////////////////////////////////////////////////
  32. std::cout << sstr.str() << "\n\n";
  33. std::cout << unnecessarilyComplexCopy.id << "\n";
  34. std::cout << unnecessarilyComplexCopy.libPath << "\n";
  35. for (auto const& i : unnecessarilyComplexCopy.someContainer)
  36. std::cout << i << "\n";
  37. }

Example 1B

An alternatvie to deriving. Does not handle polymorphic structs.
Especially useful for structs, you did not create.

  1. struct ConfigContent
  2. {
  3. int id;
  4. std::string libPath;
  5. std::vector <std::string> someContainer;
  6. };
  7. BOOST_FUSION_ADAPT_STRUCT
  8. (
  9. ConfigContent,
  10. (int, id)
  11. (std::string, libPath)
  12. (std::vector <std::string>, someContainer)
  13. )
  14. SJSON_INJECT_STRINGIFY(ConfigContent)
  15. SJSON_INJECT_PARSE(ConfigContent)

Example 2

(Showing polymorphic serlialization / deserialization)

  1. #ifndef Q_MOC_RUN // A Qt workaround, for those of you who use Qt
  2. # include <SimpleJSON/parse/jsd.hpp>
  3. # include <SimpleJSON/parse/jsd_convenience.hpp>
  4. # include <SimpleJSON/stringify/jss.hpp>
  5. # include <SimpleJSON/stringify/jss_fusion_adapted_struct.hpp>
  6. #endif
  7. #include "SimpleJSON/utility/polymorphy.hpp"
  8. #include <string>
  9. #include <vector>
  10. #include <sstream>
  11. #include <fstream>
  12. struct Base
  13. {
  14. virtual ~Base() = default;
  15. };
  16. struct Line : Base
  17. , public JSON::Stringifiable <Line>
  18. , public JSON::Parsable <Line>
  19. {
  20. std::string text;
  21. };
  22. struct PrimalList : Base
  23. , public JSON::Stringifiable <PrimalList>
  24. , public JSON::Parsable <PrimalList>
  25. {
  26. std::vector <std::shared_ptr <Base>> elements;
  27. };
  28. JSON_DECLARE_POLYMORPHIC
  29. (
  30. Base, (Line)(PrimalList)
  31. )
  32. BOOST_FUSION_ADAPT_STRUCT
  33. (
  34. Base
  35. )
  36. BOOST_FUSION_ADAPT_STRUCT
  37. (
  38. Line,
  39. text
  40. )
  41. BOOST_FUSION_ADAPT_STRUCT
  42. (
  43. PrimalList,
  44. elements
  45. )
  46. template <typename T>
  47. void parse(T& cc, std::istream& json)
  48. {
  49. auto tree = JSON::parse_json(json);
  50. JSON::parse(cc, "json_pno", tree);
  51. }
  52. template <typename T>
  53. std::ostream& stringify(std::ostream& stream, T const& cc)
  54. {
  55. stream << "{";
  56. JSON::stringify(stream, "json_pno", cc, JSON::ProduceNamedOutput);
  57. stream << "}";
  58. return stream;
  59. }
  60. template <typename T>
  61. //using smart_ptr_t = std::shared_ptr <T>;
  62. using smart_ptr_t = std::unique_ptr<T>;
  63. int main()
  64. {
  65. smart_ptr_t <Base> a {new Line};
  66. static_cast <Line*> (a.get())->text = "test";
  67. std::stringstream sstr;
  68. stringify(sstr, a);
  69. smart_ptr_t <Base> b;
  70. parse(b, sstr);
  71. if (b)
  72. std::cout << "b!\n";
  73. //std::cout << JSON::polydecls <Base>::identify_type(b.get()) << "\n";
  74. /////////////////////////////////////////////////////////////////////////
  75. // Lets check if we got what we set
  76. /////////////////////////////////////////////////////////////////////////
  77. std::cout << static_cast <Line*> (b.get())->text << "\n";
  78. }

Example 3

Shows formatted output to a file

  1. #ifndef Q_MOC_RUN // A Qt workaround, for those of you who use Qt
  2. # include <SimpleJSON/parse/jsd.hpp>
  3. # include <SimpleJSON/stringify/jss.hpp>
  4. # include <SimpleJSON/stringify/jss_fusion_adapted_struct.hpp>
  5. #endif
  6. struct Object : public JSON::Stringifiable <Object>
  7. , public JSON::Parsable <Object>
  8. {
  9. int A;
  10. std::string B;
  11. float C;
  12. };
  13. BOOST_FUSION_ADAPT_STRUCT(Object, A, B, C)
  14. int main()
  15. {
  16. Object o;
  17. try_stringifiy_beautiful("myfile.json", "root", o);
  18. // alternative:
  19. {
  20. boost::iostreams::filtering_ostream filter;
  21. // or however else, refer to boost::iostreams doc
  22. filter.push(JSON::BeautifiedStreamWrapper{});
  23. filter.push(boost::iostreams::file_sink(file));
  24. // will not compile with a readable message,
  25. // if Object is not a JSON::Stringifiable
  26. JSON::try_stringify_start(filter, o);
  27. }
  28. }

Useful Utility

JSON::Base64 < T> mem;

mem will be handled as a base64 string. useful if mem can contain any character or binary sequence, including quotes.

JSON::fill_missing

  1. auto tree = JSON::parse_json(str); // str contains some JSON.
  2. JSON::fill_missing <MyJsonType> ("", tree); // missing members in the tree of MyJsonType get default constructed.

JSON::rename

Renames a member. This is necessary if something is not a valid C++ identifier, such as “.” or “6”.

  1. struct MyJsonType : /* ... */
  2. {
  3. /**
  4. * SJSON_SHORT_STRING -> handles up to 16 characters. (prefer whenever possible)
  5. * SJSON_STRING -> handles up to 64 characters.
  6. * SJSON_LONG_STRING -> 256 chars. (dont use if possible, heavy compiler performance hit)
  7. * SJSON_LONG_LONG_STRING -> 1024 chars. (dont use if possible, heavy compiler performance hit)
  8. */
  9. JSON::rename <std::string, SJSON_SHORT_STRING("__.bla")> blaProperty;
  10. };

Details

How does stringify work?

There is just one function for stringification. This stringify function is heavily overloaded and makes use of SFINAE in almost every overload. The compiler then finds the correct overload to stringify the parameter.

  • The library supports almost all STL containers (stringify supports even more) as well as fundamental types.
  • Containers will decay into json arrays, if their value_type is a stringifiable (this is recursive).
  • Classes will turn into objects, if it is adapted, derives from Stringifiable<> and each member is stringifiable.
  • For other classes, the “stringify” method is called, if provided, but then you will have to make sure on your own to produce a valid output. (The base64 wrapper uses this).

stringify behaviour (and STL stuff stringification)

It is quite import to know how certain STL constructs translate into JSON and here is a brief summary:

What it is What it becomes Remarks
std::vector […]
std::deque […]
std::list […]
std::forward_list […] Who uses forward_list anyway?
std::array […]
std::pair {“first”: …, “second”: …}
std::atomic What T would produce. Caution! Obviously calls load().
std::bitset [1, 0, …]
std::string “…”
fundamental types themselves
JSON::IteratorRange […]
std::map {“key”: “value”, “key2”: “value2”, …} A map basically represents a JSON object
std::mutex nothing ??? why would you???
std::set […]
std::stack […] Don’t use if possible, better use deque.
std::queue […] Don’t use if possible, better use deque.
std::shared_ptr What T would produce. Throws if invalid.
std::unique_ptr What T would produce. Throws if invalid.
std::weak_ptr What T would produce Throws if invalid.
std::tuple {“_1”: …, “_2”: …, …}
std::valarray […]
boost::optional T or nothing Very useful for making things optional… ofc

Reference

Please visit the wiki: Wiki