{"id":1990,"date":"2020-01-06T01:40:39","date_gmt":"2020-01-05T23:40:39","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=1990"},"modified":"2020-01-06T02:18:11","modified_gmt":"2020-01-06T00:18:11","slug":"best-json-and-base64-libraries-for-c-and-windows","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/1990","title":{"rendered":"Best JSON and Base64 libraries for C++ and Windows"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The application in previous post <a href=\"https:\/\/alax.info\/blog\/1987\">Is my system a hybrid (switchable) graphics system?<\/a> is implemented in C++\/WinRT, which I finally decided to give a try.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p> For authoring and consuming Windows Runtime APIs using C++, there is C++\/WinRT. This is Microsoft&#8217;s recommended replacement for the&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/cppcx\/visual-c-language-reference-c-cx?branch=live\">C++\/CX<\/a>&nbsp;language projection, and the&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/windows\/windows-runtime-cpp-template-library-wrl?branch=live\">Windows Runtime C++ Template Library (WRL)<\/a>. <\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">The real question here is this: is WRL at all adopted yet? However it is already superseded by a new thing!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is really cool about C++\/WinRT though &#8211; and I do like the most of it &#8211; is that good old fashioned apps like desktop and even console ones can seamlessly consume UWP APIs. And this is the field where new and revised API appear now in first place.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you, for instance, have a need in JSON lib for C++ code, <code><a href=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/Windows.Data.Json\">Windows.Data.Json<\/a><\/code> API is at your service. You no longer need a lib for this and many other routine tasks. Here is how you would parse and stringify JSON in plain C++:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std::wstring EncodeBase64(const BYTE* Data, SIZE_T DataSize)\n{\n\twinrt::Windows::Storage::Streams::Buffer Buffer(static_cast&lt;UINT32>(DataSize));\n\tstd::memcpy(Buffer.data(), Data, DataSize);\n\tBuffer.Length(static_cast&lt;UINT32>(DataSize));\n\tconst winrt::hstring Text = winrt::Windows::Security::Cryptography::CryptographicBuffer::EncodeToBase64String(Buffer);\n\treturn static_cast&lt;std::wstring>(Text);\n}\nstd::wstring EncodeBase64(const std::string&amp; Value)\n{\n\treturn EncodeBase64(reinterpret_cast&lt;const BYTE*>(Value.data()), Value.size());\n}\nstd::wstring EncodeBase64(const std::wstring&amp; Value)\n{\n\treturn EncodeBase64(ToMultiByte(Value, CP_UTF8));\n}\nstd::wstring EncodeBase64(const winrt::Windows::Data::Json::JsonObject&amp; Value)\n{\n\treturn EncodeBase64(static_cast&lt;std::wstring>(Value.Stringify()));\n}\n\nstd::wstring DecodeBase64(const std::wstring&amp; Value)\n{\n\twinrt::Windows::Storage::Streams::IBuffer Buffer { winrt::Windows::Security::Cryptography::CryptographicBuffer::DecodeFromBase64String(Value) };\n\tconst UINT32 Length = Buffer.Length();\n\tstd::string Result(Length, 0);\n\tstd::memcpy(Result.data(), Buffer.data(), Length);\n\treturn FromMultiByte(Result);\n}\nwinrt::Windows::Data::Json::JsonObject DecodeBase64AsJsonObject(const std::wstring&amp; Value)\n{\n\treturn { winrt::Windows::Data::Json::JsonObject::Parse(DecodeBase64(Value)) };\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This thing can cope with XAML too, and if you get the hump just go read on on C++\/WinRT use of <code><a href=\"https:\/\/blogs.msdn.microsoft.com\/yizhang\/2017\/10\/28\/c-coroutine-tutorial\/\">co_await<\/a><\/code> and friends for built-in support for UWP <code><a href=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/api\/windows.foundation.iasyncaction\">IAsyncAction<\/a><\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The application in previous post Is my system a hybrid (switchable) graphics system? is implemented in C++\/WinRT, which I finally decided to give a try. For authoring and consuming Windows Runtime APIs using C++, there is C++\/WinRT. This is Microsoft&#8217;s recommended replacement for the&nbsp;C++\/CX&nbsp;language projection, and the&nbsp;Windows Runtime C++ Template Library (WRL). The real question&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/1990\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[591,38,588,590,549,589],"class_list":["post-1990","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-base64","tag-c","tag-c-winrt","tag-json","tag-uwp","tag-wrl"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/1990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=1990"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/1990\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=1990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=1990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=1990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}