/
ExampleLibraryConversion.cpp
179 lines (145 loc) · 5.83 KB
/
ExampleLibraryConversion.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// ExampleLibraryConversion.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
// nlohmann json needs this in order to serialize a std::wstring
[[nodiscard]] inline
std::string VFormatStdString(_Printf_format_string_ char const* const pFmt, va_list args)
{
char buffer[2048];
vsprintf_s(buffer, pFmt, args);
return std::string{ buffer };
}
[[nodiscard]] inline
std::string FormatStdString(_Printf_format_string_ char const* const pFmt, ...)
{
va_list args;
va_start(args, pFmt);
auto result = VFormatStdString(pFmt, args);
va_end(args);
return result;
}
namespace std
{
void to_json(json& j, std::wstring const& ws)
{
j = FormatStdString("%ls", ws.c_str());
}
} // namespace std
//-------------------------------------------------------------------------------------------------
#define USE_RAPIDJSON 0
#if USE_RAPIDJSON
// 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 <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();
}
#endif
//-------------------------------------------------------------------------------------------------
// 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;
// Use nlohmann/json to serialize the data
// Copilot generated all the code after typing "json j;" and hitting enter.
json j;
j["majorVersion"] = systemInfo.MajorVersion;
j["minorVersion"] = systemInfo.MinorVersion;
j["processId"] = systemInfo.ProcessId;
j["userName"] = systemInfo.UserName;
j["system"]["processorArchitecture"] = systemInfo.System.ProcessorArchitecture;
j["system"]["processorLevel"] = systemInfo.System.ProcessorLevel;
j["system"]["processorRevision"] = systemInfo.System.ProcessorRevision;
j["system"]["numberOfProcessors"] = systemInfo.System.NumberOfProcessors;
j["system"]["productType"] = systemInfo.System.ProductType;
// Copilot generated this line but made minor edits.
std::string jsonText = j.dump(2);
// Construct an empty JSON document
//WDocument document(json::Type::kObjectType);
//auto allocator = document.GetAllocator();
// The code below for creating o and s objects was generated by copilot. If
// you delete the code and hit enter after o is created, copilot will generate
// the remaining code for you: just hit tab to accept the suggestion and enter
// to move to the next line.
//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("%hs\n", jsonText.c_str());
}