33#include < userver/formats/json/inline.hpp>
44#include < userver/formats/json/value_builder.hpp>
55
6+ #include < schemas/array_of_xcpptype.hpp>
67#include < schemas/custom_cpp_type.hpp>
78
89USERVER_NAMESPACE_BEGIN
@@ -14,6 +15,9 @@ TEST(Custom, Int) {
1415
1516 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
1617 EXPECT_EQ (json_back, json);
18+
19+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
20+ EXPECT_EQ (custom2, custom);
1721}
1822
1923TEST (Custom, String) {
@@ -23,6 +27,9 @@ TEST(Custom, String) {
2327
2428 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
2529 EXPECT_EQ (json_back, json);
30+
31+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
32+ EXPECT_EQ (custom2, custom);
2633}
2734
2835TEST (Custom, Decimal) {
@@ -32,6 +39,9 @@ TEST(Custom, Decimal) {
3239
3340 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
3441 EXPECT_EQ (json_back, json);
42+
43+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
44+ EXPECT_EQ (custom2, custom);
3545}
3646
3747TEST (Custom, Boolean) {
@@ -41,6 +51,9 @@ TEST(Custom, Boolean) {
4151
4252 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
4353 EXPECT_EQ (json_back, json);
54+
55+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
56+ EXPECT_EQ (custom2, custom);
4457}
4558
4659TEST (Custom, Number) {
@@ -50,6 +63,9 @@ TEST(Custom, Number) {
5063
5164 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
5265 EXPECT_EQ (json_back, json);
66+
67+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
68+ EXPECT_EQ (custom2, custom);
5369}
5470
5571TEST (Custom, Object) {
@@ -59,6 +75,9 @@ TEST(Custom, Object) {
5975
6076 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
6177 EXPECT_EQ (json_back, json);
78+
79+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
80+ EXPECT_EQ (custom2, custom);
6281}
6382
6483TEST (Custom, XCppContainer) {
@@ -68,6 +87,9 @@ TEST(Custom, XCppContainer) {
6887
6988 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
7089 EXPECT_EQ (json_back, json);
90+
91+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
92+ EXPECT_EQ (custom2, custom);
7193}
7294
7395TEST (Custom, XCppType) {
@@ -77,6 +99,9 @@ TEST(Custom, XCppType) {
7799
78100 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
79101 EXPECT_EQ (json_back, json);
102+
103+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
104+ EXPECT_EQ (custom2, custom);
80105}
81106
82107TEST (Custom, OneOf) {
@@ -86,6 +111,9 @@ TEST(Custom, OneOf) {
86111
87112 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
88113 EXPECT_EQ (json_back, json) << ToString (json_back) << " " << ToString (json);
114+
115+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
116+ EXPECT_EQ (custom2, custom);
89117}
90118
91119TEST (Custom, OneOfWithDiscriminator) {
@@ -98,6 +126,9 @@ TEST(Custom, OneOfWithDiscriminator) {
98126
99127 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
100128 EXPECT_EQ (json_back, json);
129+
130+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
131+ EXPECT_EQ (custom2, custom);
101132}
102133
103134TEST (Custom, AllOf) {
@@ -107,6 +138,64 @@ TEST(Custom, AllOf) {
107138
108139 auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
109140 EXPECT_EQ (json_back, json);
141+
142+ const auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjWithCustom>{});
143+ EXPECT_EQ (custom2, custom);
144+ }
145+
146+ TEST (Custom, ArrayOfXCppType) {
147+ auto json = formats::json::MakeObject (
148+ " foo" ,
149+ formats::json::MakeArray (formats::json::MakeArray (1 , 2 )),
150+ " bar" ,
151+ formats::json::MakeArray (-1 , -2 ),
152+ " baz" ,
153+ formats::json::MakeArray (-3 , 100 , " test" ),
154+ " additional1" ,
155+ formats::json::MakeArray (formats::json::MakeObject (" lat" , 4 , " lon" , 3 )),
156+ " additional2" ,
157+ formats::json::MakeArray (
158+ formats::json::MakeObject (" lon" , 5 , " lat" , 6 ),
159+ formats::json::MakeObject (" lon" , 7 , " lat" , 8 )
160+ ),
161+ " additional3" ,
162+ formats::json::MakeArray ()
163+ );
164+
165+ auto custom = json.As <ns::ObjectArrayOfXCppType>();
166+ ns::ObjectArrayOfXCppType ethalon;
167+ ethalon.foo = std::vector<my::Point>{
168+ my::Point{1 , 2 },
169+ };
170+ ethalon.bar .emplace ();
171+ ethalon.bar ->push_back (my::CustomNumber{-1 });
172+ ethalon.bar ->push_back (my::CustomNumber{-2 });
173+
174+ ethalon.baz .emplace ();
175+ ethalon.baz ->push_back (my::CustomNumber{-3 });
176+ ethalon.baz ->push_back (my::CustomNumber{100 });
177+ ethalon.baz ->push_back (my::CustomString{" test" });
178+
179+ ethalon.extra = {
180+ {" additional1" , std::vector<my::Point>{my::Point{3 , 4 }}},
181+ {" additional2" , std::vector<my::Point>{my::Point{5 , 6 }, my::Point{7 , 8 }}},
182+ {" additional3" , std::vector<my::Point>{}},
183+ };
184+ EXPECT_EQ (custom, ethalon);
185+
186+ auto json_back = formats::json::ValueBuilder{custom}.ExtractValue ();
187+ EXPECT_EQ (json_back, json);
188+
189+ auto custom2 = FromJsonString (ToString (json), formats::parse::To<ns::ObjectArrayOfXCppType>{});
190+ EXPECT_EQ (custom2, ethalon);
191+
192+ auto changed_ethalon = ethalon;
193+ changed_ethalon.extra [" additional2" ][1 ].lat = 42 ;
194+ EXPECT_FALSE (changed_ethalon == ethalon);
195+
196+ changed_ethalon = ethalon;
197+ changed_ethalon.extra .erase (" additional3" );
198+ EXPECT_FALSE (changed_ethalon == ethalon);
110199}
111200
112201USERVER_NAMESPACE_END
0 commit comments