Skip to content

Commit eb4f35b

Browse files
committed
- Обновил версию simstr
- Исправил ошибку сериализации - Дополнил тесты
1 parent c2e6b12 commit eb4f35b

3 files changed

Lines changed: 101 additions & 37 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function(add_simstr)
6262
simstr
6363
GIT_REPOSITORY https://github.com/orefkov/simstr.git
6464
GIT_SHALLOW TRUE
65-
GIT_TAG fb0e2f1
65+
GIT_TAG rel.1.2.1
6666
FIND_PACKAGE_ARGS NAMES simstr
6767
)
6868
FetchContent_MakeAvailable(simstr)

src/json.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*
1+
/*
22
* (c) Проект "SimJson", Александр Орефков orefkov@gmail.com
33
* ver. 1.0
44
* Классы для работы с JSON
55
*/
66

7-
#include <simjson/json.h>
7+
#include <simjson/json.h>
88
#include <cmath>
99
#include <algorithm>
1010
#include <fstream>
@@ -16,6 +16,7 @@ using namespace simstr::literals;
1616
template<typename K>
1717
struct expr_json_str {
1818
using symb_type = K;
19+
using test_type = std::make_unsigned_t<K>;
1920
using ssType = typename JsonValueTempl<K>::ssType;
2021
ssType text;
2122
size_t l;
@@ -28,7 +29,7 @@ struct expr_json_str {
2829
const K* ptr = text.symbols();
2930
size_t add = 0;
3031
for (size_t i = text.length(); i-- ;) {
31-
K s = *ptr++;
32+
test_type s = (test_type)*ptr++;
3233
switch (s) {
3334
case '\b':
3435
case '\f':
@@ -86,7 +87,7 @@ struct expr_json_str {
8687
const K* r = text.symbols();
8788
size_t lenOfText = text.length(), lenOfTail = l - lenOfText;
8889
while (lenOfTail) {
89-
K s = *r++;
90+
test_type s = (test_type)*r++;
9091
switch (s) {
9192
case '\"':
9293
*ptr++ = '\\';

tests/test_json.cpp

Lines changed: 95 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,78 @@ TEST(SimJson, JsonStoreString) {
378378
})");
379379
}
380380

381+
TEST(SimJson, JsonStoreString16) {
382+
JsonValueU json {
383+
{u"p1", 1},
384+
{u"p3"_h, true},
385+
{u"p2"_h, 2},
386+
{u"p4"_h, {1, 2, false, Json::null, u"test\ntest\x01", Json::emptyObject, Json::emptyArray}}
387+
};
388+
EXPECT_EQ(stringu{json.store(false, true)}, uR"({"p1":1,"p2":2,"p3":true,"p4":[1,2,false,null,"test\ntest\u0001",{},[]]})");
389+
EXPECT_EQ(stringu{json.store(true, true)}, uR"({
390+
"p1": 1,
391+
"p2": 2,
392+
"p3": true,
393+
"p4": [
394+
1,
395+
2,
396+
false,
397+
null,
398+
"test\ntest\u0001",
399+
{},
400+
[]
401+
]
402+
})");
403+
}
404+
405+
TEST(SimJson, JsonStoreString32) {
406+
JsonValueUU json {
407+
{U"p1", 1},
408+
{U"p3"_h, true},
409+
{U"p2"_h, 2},
410+
{U"p4"_h, {1, 2, false, Json::null, U"test\ntest\x01", Json::emptyObject, Json::emptyArray}}
411+
};
412+
EXPECT_EQ(stringuu{json.store(false, true)}, UR"({"p1":1,"p2":2,"p3":true,"p4":[1,2,false,null,"test\ntest\u0001",{},[]]})");
413+
EXPECT_EQ(stringuu{json.store(true, true)}, UR"({
414+
"p1": 1,
415+
"p2": 2,
416+
"p3": true,
417+
"p4": [
418+
1,
419+
2,
420+
false,
421+
null,
422+
"test\ntest\u0001",
423+
{},
424+
[]
425+
]
426+
})");
427+
}
428+
429+
TEST(SimJson, JsonStoreStringW) {
430+
JsonValueW json {
431+
{L"p1", 1},
432+
{L"p3"_h, true},
433+
{L"p2"_h, 2},
434+
{L"p4"_h, {1, 2, false, Json::null, L"test\ntest\x01", Json::emptyObject, Json::emptyArray}}
435+
};
436+
EXPECT_EQ(stringw{json.store(false, true)}, LR"({"p1":1,"p2":2,"p3":true,"p4":[1,2,false,null,"test\ntest\u0001",{},[]]})");
437+
EXPECT_EQ(stringw{json.store(true, true)}, LR"({
438+
"p1": 1,
439+
"p2": 2,
440+
"p3": true,
441+
"p4": [
442+
1,
443+
2,
444+
false,
445+
null,
446+
"test\ntest\u0001",
447+
{},
448+
[]
449+
]
450+
})");
451+
}
452+
381453
TEST(SimJson, JsonThrow) {
382454
JsonValue val = 10;
383455
lstringa<100> err_descr;
@@ -428,44 +500,35 @@ TEST(SimJson, JsonStdInitMap) {
428500
EXPECT_EQ(json2.type(), Json::Object);
429501
EXPECT_EQ(json2.store(false, true), "{\"four\":4,\"one\":1,\"three\":3,\"two\":2}");
430502
}
431-
/*
432-
stringa getFileContent(stra fileName) {
433-
std::ifstream file(fileName, std::ios::binary | std::ios::ate);
434-
std::streamsize size = file.tellg();
435-
file.seekg(0, std::ios::beg);
436-
lstringsa<0> result;
437-
file.read(result.reserve(size), size);
438-
return result;
439-
}
440503

504+
#if 0
441505
TEST(SimJson, JsonParseBig) {
442-
{
443-
stringa content1 = get_file_content("citm_catalog.json");
444-
stringa content2 = get_file_content("canada.json");
445-
stringa content3 = get_file_content("twitter.json");
446-
447-
auto t1 = std::chrono::high_resolution_clock::now();
448-
449-
for (size_t idx = 0; idx < 10; idx++) {
450-
auto json1 = JsonValue::parse(content1);
451-
EXPECT_EQ(std::get<1>(json1), JsonParseResult::Success);
452-
auto json2 = JsonValue::parse(content2);
453-
EXPECT_EQ(std::get<1>(json2), JsonParseResult::Success);
454-
auto json3 = JsonValue::parse(content3);
455-
EXPECT_EQ(std::get<1>(json3), JsonParseResult::Success);
456-
}
506+
stringa content1 = get_file_content("citm_catalog.json");
507+
stringa content2 = get_file_content("canada.json");
508+
stringa content3 = get_file_content("twitter.json");
509+
510+
auto t1 = std::chrono::high_resolution_clock::now();
511+
512+
for (size_t idx = 0; idx < 10; idx++) {
513+
auto json1 = JsonValue::parse(content1);
514+
EXPECT_EQ(std::get<1>(json1), JsonParseResult::Success);
515+
stringa t = std::get<0>(json1).store(true, false, ' ', 4);
516+
EXPECT_EQ(content1.length(), t.length());
517+
auto json2 = JsonValue::parse(content2);
518+
EXPECT_EQ(std::get<1>(json2), JsonParseResult::Success);
519+
t = std::get<0>(json2).store();
520+
auto json3 = JsonValue::parse(content3);
521+
EXPECT_EQ(std::get<1>(json3), JsonParseResult::Success);
522+
t = std::get<0>(json3).store();
523+
}
457524

458-
auto t2 = std::chrono::high_resolution_clock::now();
525+
auto t2 = std::chrono::high_resolution_clock::now();
459526

460-
std::chrono::duration<double, std::milli> ms_double = t2 - t1;
527+
std::chrono::duration<double, std::milli> ms_double = t2 - t1;
461528

462-
std::cout << "Parse at " << ms_double.count() / 10.0 << std::endl;
529+
std::cout << "Parse and store at " << ms_double.count() / 10.0 << std::endl;
463530

464-
//EXPECT_TRUE(json1.is_object());
465-
//EXPECT_TRUE(json2.is_object());
466-
//EXPECT_TRUE(json3.is_object());
467-
}
468531
}
469-
*/
532+
#endif
470533

471534
} // namespace simjson::tests

0 commit comments

Comments
 (0)