Alternate e697dbe9c5997e35395fe158628dd8c5209481da
for Visual Studio 2022 and Windows 11.
読み取り中…
検索中…
一致する文字列を見つけられません
PicoJsonTest.cpp
[詳解]
1
5
6#include "pch.h"
7#include "OSSLibrary.h"
8
9using namespace Microsoft::VisualStudio::CppUnitTestFramework;
10
11namespace OSSLibraryTest
12{
13 TEST_CLASS (PicoJsonTest)
14 {
15 public:
16 TEST_CLASS_INITIALIZE (ClassInitialize)
17 {
18 Logger::WriteMessage ("class initialize.\n");
19 }
20
21 TEST_CLASS_CLEANUP (ClassCleanup)
22 {
23 Logger::WriteMessage ("class cleanup.\n");
24 }
25
26 TEST_METHOD_INITIALIZE (MethodInitialize)
27 {
28 Logger::WriteMessage ("method initialize.\n");
29 }
30
31 TEST_METHOD_CLEANUP (MethodCleanup)
32 {
33 Logger::WriteMessage ("method cleanup.\n");
34 }
35
36 void ShowValue (std::string key, std::string value)
37 {
38 std::string response = key + ":" + value + "\n";
39 Logger::WriteMessage (response.c_str ());
40 }
41
42 TEST_METHOD (PicoJsonTest1)
43 {
44 std::ifstream ifs;
45 ifs.open ("test.json"); // CAUTION:ビルドパッチで実行環境にコピーしています。
46 if (ifs.fail ())
47 {
48 std::cerr << "fail to read test.json" << std::endl;
49 Assert::Fail (_T ("test.jsonのオープンに失敗しました。"));
50 }
51
52 const std::string json (
53 (std::istreambuf_iterator<char> (ifs)),
54 std::istreambuf_iterator<char> ());
55 ifs.close ();
56
58 const std::string error = picojson::parse (val, json);
59 if (!error.empty ())
60 {
61 std::cerr << error << std::endl;
62 Assert::Fail (_T ("picojson::parse()に失敗しました。"));
63 }
64
65 // @sa https://qiita.com/usagi/items/da3568d8fa61e4aafede
66 /*
67 picojson::valueの中身の可能性
68 double JSONの数値
69 std::string JSONの文字列
70 picojson::array JSONの配列
71 picojson::object JSONのオブジェクト(=辞書)
72 picojson::null JSONのnull
73 */
74
75 try
76 {
78
79 int Numeric = (int)Root["Numeric"].get<double> ();
80 ShowValue ("Numeric", std::to_string (Numeric));
81
82 auto String = Root["String"].get<std::string> ();
83 ShowValue ("String", String);
84
85 bool Boolean = Root["Boolean"].get<bool> ();
86 ShowValue ("Boolean", std::to_string (Boolean));
87
88 ShowValue ("Object", ">>");
89 picojson::object Object = Root["Object"].get<picojson::object> ();
90
91 double _Numeric = Object["numeric"].get<double> ();
92 ShowValue (">numeric", std::to_string (_Numeric));
93
94 auto _String = Object["string"].get<std::string> ();
95 ShowValue (">string", _String);
96
97 bool _Boolean = Object["boolean"].get<bool> ();
98 ShowValue (">boolean", std::to_string (_Boolean));
99
100 auto _Array1 = Object["Array1"].get<picojson::array> ();
101 ShowValue (">Array1", ">>");
102 for (auto item : _Array1)
103 {
104 ShowValue (">>Array1Value", std::to_string (item.get<double> ()));
105 }
106
107 auto _Array2 = Object["Array2"].get<picojson::array> ();
108 ShowValue (">Array2", ">>");
109 for (auto item : _Array2)
110 {
111 ShowValue (">>Array2Value", item.get<std::string> ());
112 }
113
114 auto Array1 = Root["Array1"].get<picojson::array> ();
115 ShowValue ("Array1Value", ">>");
116 for (auto item : Array1)
117 {
118 ShowValue (">Array1Value", std::to_string (item.get<double> ()));
119 }
120
121 auto Array2 = Root["Array2"].get<picojson::array> ();
122 ShowValue ("Array2", ">>");
123 for (auto item : Array2)
124 {
125 ShowValue (">Array2Value", item.get<std::string> ());
126
127 }
128 }
129 catch (...)
130 {
131 Logger::WriteMessage ("catched anything.\n");
132 Assert::Fail (_T ("JSON分析中に例外が発生しました。"));
133 }
134 }
135
136 TEST_METHOD (FlowSettingsTest1)
137 {
138 std::ifstream ifs;
139 ifs.open ("FlowSettings.json"); // CAUTION:ビルドパッチで実行環境にコピーしています。
140 if (ifs.fail ())
141 {
142 std::cerr << "fail to read FlowSettings.json" << std::endl;
143 Assert::Fail (_T ("FlowSettings.jsonのオープンに失敗しました。"));
144 }
145
146 const std::string json (
147 (std::istreambuf_iterator<char> (ifs)),
148 std::istreambuf_iterator<char> ());
149 ifs.close ();
150
151 picojson::value val;
152 const std::string error = picojson::parse (val, json);
153 if (!error.empty ())
154 {
155 std::cerr << error << std::endl;
156 Assert::Fail (_T ("picojson::parse()に失敗しました。"));
157 }
158
159 try
160 {
162
163 for (const auto& item : Root)
164 {
165 ShowValue ("Key:", item.first);
166
167 picojson::object Element = Root[item.first].get<picojson::object> ();
168 auto id = Element["id"].get<std::string> ();
169 ShowValue (" id", id);
170 auto name = Element["name"].get<std::string> ();
171 ShowValue (" name", name);
172 auto object = Element["object"].get<std::string> ();
173 ShowValue (" object", object);
174 auto product = Element["product"].get<std::string> ();
175 ShowValue (" product", product);
176
177 if (Element["next"].is<picojson::array> ())
178 {
179 auto nextElements = Element["next"].get<picojson::array> ();
180 for (const auto& next : nextElements)
181 {
182 ShowValue (" >next", next.get<std::string> ());
183 }
184 }
185 else
186 {
187 auto next = Element["next"].get<std::string> ();
188 ShowValue (" next", next);
189 }
190
191 }
192 }
193 catch (...)
194 {
195 std::cout << "catched anything." << std::endl;
196 Assert::Fail (_T ("JSON分析中に例外が発生しました。"));
197 }
198 }
199 };
200}
Open Source Software を集約したプロジェクトファイル
プリコンパイル済みヘッダー ファイルです。
const T & get() const
TEST_CLASS(OSSLibraryTest)
std::string parse(value &out, Iter &pos, const Iter &last)
Definition: picojson.h:1072
value::object object
Definition: picojson.h:209
value::array array
Definition: picojson.h:208