{"id":3370,"date":"2020-07-29T16:14:31","date_gmt":"2020-07-29T16:14:31","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=3370"},"modified":"2020-07-29T16:14:34","modified_gmt":"2020-07-29T16:14:34","slug":"writing-json","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/networking\/json\/rapidjson\/writing-json","title":{"rendered":"Writing Json"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\/\/rapidjson needs:\n#include \"rapidjson\/writer.h\"\n#include \"rapidjson\/stringbuffer.h\"\n#include &lt;iostream>\nusing namespace rapidjson;\nusing namespace std;\n\n\n\tStringBuffer json_string_buffer;\n\tWriter&lt;StringBuffer> json_writer(json_string_buffer);\n\n\tjson_writer.StartObject();\t\t\/\/&lt;&lt;&lt;There must be a root object surrounding everything\n\n\t\tjson_writer.Key(\"name1\");\n\t\tjson_writer.Uint(1);\n\t\t\n\t\tjson_writer.Key(\"name2\");\n\t\tjson_writer.Uint(2);\n\t\t\n\t\t\/\/A simple array\n\t\tjson_writer.Key(\"MyArrayOfThings\");\n\t\tjson_writer.StartArray();\n\t\tfor (i = 0; i&lt; 3; i++)\n\t\t{\n\t\t\tjson_writer.Uint(1));\n\t\t}\n\t\tjson_writer.EndArray();\n\n\t\t\/\/A simple Key + Value array\n\t\tjson_writer.Key(\"MyArrayOfThings\");\n\t\tjson_writer.StartObject();\n\t\tfor (i = 0; i&lt; 3; i++)\n\t\t{\n\t\t\tjson_writer.Key(\"Something\");\n\t\t\tjson_writer.Uint(i + 10));\n\t\t}\n\t\tjson_writer.EndObject();\n\n\t\t\/\/A multi part array\n\t\tjson_writer.Key(\"MyArrayOfThings\");\n\t\tjson_writer.StartArray();\n\t\tfor (i = 0; i&lt; 3; i++)\n\t\t{\n\t\t\tjson_writer.StartObject();\n\t\t\t\tjson_writer.Key(\"Something\");\n\t\t\t\tjson_writer.Uint(1));\n\n\t\t\t\tjson_writer.Key(\"SomethingElse\");\n\t\t\t\tjson_writer.Uint(2));\n\t\t\tjson_writer.EndObject();\n\t\t}\n\t\tjson_writer.EndArray();\n\n\n\tjson_writer.EndObject();\n\n\tcout &lt;&lt; json_string_buffer.GetString() &lt;&lt; endl;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Element&nbsp;types<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tjson_writer.Key(\"name\");\n\tjson_writer.String(\"abcd\");\t\t\t\t\n\n\tjson_writer.Key(\"name\");\n\tjson_writer.Int(some_variable);\n\t\t\t\t\n\tjson_writer.Key(\"name\");\n\tjson_writer.Uint(some_variable);\n\t\n\tjson_writer.Key(\"name\");\n\tjson_writer.Uint64(some_variable);\n\n\tjson_writer.Key(\"name\");\n\tjson_writer.Double(3.1416);\n\n\tjson_writer.Key(\"name\");\n\tjson_writer.Bool(true);\n\n\tjson_writer.Key(\"name\");\n\tjson_writer.Null();\n\n\tjson_writer.Key(\"name\");\n\tjson_writer.StartArray();\n\tfor (unsigned i = 0; i &lt; 4; i++)\n\t\tjson_writer.Uint(i);\n\tjson_writer.EndArray();<\/code><\/pre>\n\n\n\n<p><em>This is not a complete list<\/em><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">2 Level Array<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\twriter.Key(\"year\");\n\twriter.Uint(year);\n\n\twriter.Key(\"available\");\n\twriter.StartArray();\n\tfor (day_of_month = 1; day_of_month &lt;= 31; day_of_month++)\n\t{\n\t\twriter.StartArray();\n\t\tfor (hour = 0; hour &lt; 24; hour++)\n\t\t{\n\t\t\twriter.Bool(true);\n\t\t}\n\t\twriter.EndArray();\n\t}\n\twriter.EndArray();<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttp:\/\/miloyip.github.io\/rapidjson\/md_doc_sax.html#Writer\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Strings<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\twriter.String(\"uid\");\n\twriter.String((char*)MyCharArray);\n\twriter.String(MyString.c_str());\n\twriter.String((char*)ss1.str().c_str());<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Element&nbsp;types This is not a complete list 2 Level Array Strings<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[],"class_list":["post-3370","post","type-post","status-publish","format-standard","hentry","category-rapidjson"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=3370"}],"version-history":[{"count":1,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3370\/revisions"}],"predecessor-version":[{"id":3371,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/3370\/revisions\/3371"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=3370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=3370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=3370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}