-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleLibraryConversion.cpp
More file actions
126 lines (99 loc) · 4.05 KB
/
ExampleLibraryConversion.cpp
File metadata and controls
126 lines (99 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// ExampleLibraryConversion.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <stdint.h>
#include <stdio.h>
//-------------------------------------------------------------------------------------------------
// 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 <stdarg.h>
#include <string>
#include <type_traits>
#include <vector>
#include <cwchar>
#include <guiddef.h>
// 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 <rapidjson/document.h>
#include <rapidjson/encodings.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/error/en.h>
#include <rapidjson/prettywriter.h>
#pragma warning(pop)
// Convenience alias
namespace json = rapidjson;
// Aliases that provide UNICODE versions of all the RapidJson types we use
using WDocument = json::GenericDocument<json::UTF16<>>;
using WStringBuffer = json::GenericStringBuffer<json::UTF16<>>;
using WStringRef = json::GenericStringRef<json::UTF16<>>;
using WValue = json::GenericValue<json::UTF16<>>;
template <typename OutputStream>
using WPrettyWriter = json::PrettyWriter<OutputStream, json::UTF16<>>;
// ToString uses RapidJson's visitor pattern to generate JSON text
std::wstring ToString(WValue const& value)
{
WStringBuffer stringBuffer;
WPrettyWriter<WStringBuffer> 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());
}