// ExampleLibraryConversion.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include #include //------------------------------------------------------------------------------------------------- // RapidJson-specific code // Configures RapidJson OSS library for our use, most notably // it defines UNICODE versions of their types so that we can // serialize UNICODE strings. #include #include #include #include #include #include // Library configuration. Most of this is straightforward, except for the // "SizeType" configuration. By default RapidJson uses std::size_t for its // size type and std::size_t has different sizes depending on the platform. // For metadata we want a type that is consistent across all platforms. #define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN #define RAPIDJSON_HAS_STDSTRING 1 #define RAPIDJSON_NO_SIZETYPEDEFINE 1 namespace rapidjson { using SizeType = uint32_t; } // RapidJson triggers this warning #pragma warning(push) #pragma warning(disable:4464) // relative include path contains '..' #include #include #include #include #include #include #pragma warning(pop) // Convenience alias namespace json = rapidjson; // Aliases that provide UNICODE versions of all the RapidJson types we use using WDocument = json::GenericDocument>; using WStringBuffer = json::GenericStringBuffer>; using WStringRef = json::GenericStringRef>; using WValue = json::GenericValue>; template using WPrettyWriter = json::PrettyWriter>; // ToString uses RapidJson's visitor pattern to generate JSON text std::wstring ToString(WValue const& value) { WStringBuffer stringBuffer; WPrettyWriter writer(stringBuffer); value.Accept(writer); return stringBuffer.GetString(); } //------------------------------------------------------------------------------------------------- // Example structure to serialize; struct SystemInfo { uint32_t MajorVersion; uint32_t MinorVersion; uint32_t ProcessId; struct { uint16_t ProcessorArchitecture; uint16_t ProcessorLevel; uint16_t ProcessorRevision; uint8_t NumberOfProcessors; uint8_t ProductType; } System; std::wstring UserName; }; int main() { // Example data SystemInfo systemInfo; systemInfo.MajorVersion = 10; systemInfo.MinorVersion = 0; systemInfo.ProcessId = 1234; systemInfo.UserName = L"UserName"; systemInfo.System.NumberOfProcessors = 4; systemInfo.System.ProcessorArchitecture = 9; systemInfo.System.ProcessorLevel = 10; systemInfo.System.ProcessorRevision = 11; systemInfo.System.ProductType = 12; // Construct a WDocument document(json::Type::kObjectType); auto allocator = document.GetAllocator(); // 1. All of the below was generated by copilot WValue o(json::Type::kObjectType); o.AddMember(L"majorVersion", systemInfo.MajorVersion, allocator); o.AddMember(L"minorVersion", systemInfo.MinorVersion, allocator); o.AddMember(L"processId", systemInfo.ProcessId, allocator); o.AddMember(L"userName", systemInfo.UserName, allocator); WValue s(json::Type::kObjectType); s.AddMember(L"processorArchitecture", systemInfo.System.ProcessorArchitecture, allocator); s.AddMember(L"processorLevel", systemInfo.System.ProcessorLevel, allocator); s.AddMember(L"processorRevision", systemInfo.System.ProcessorRevision, allocator); s.AddMember(L"numberOfProcessors", systemInfo.System.NumberOfProcessors, allocator); s.AddMember(L"productType", systemInfo.System.ProductType, allocator); o.AddMember(L"system", s, allocator); std::wstring jsonText = ToString(o); printf("%ls\n", jsonText.c_str()); }