1 // __ _____ _____ _____ 2 // __| | __| | | | JSON for Modern C++ (supporting code) 3 // | | |__ | | | | | | version 3.11.2 4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json 5 // 6 // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me> 7 // SPDX-License-Identifier: MIT 8 9 #include "doctest_compatibility.h" 10 11 #include <nlohmann/json.hpp> 12 using nlohmann::json; 13 14 // ICPC errors out on multibyte character sequences in source files 15 #ifndef __INTEL_COMPILER 16 namespace 17 { 18 bool wstring_is_utf16(); wstring_is_utf16()19bool wstring_is_utf16() 20 { 21 return (std::wstring(L"") == std::wstring(L"\U0001F4A9")); 22 } 23 24 bool u16string_is_utf16(); u16string_is_utf16()25bool u16string_is_utf16() 26 { 27 return (std::u16string(u"") == std::u16string(u"\U0001F4A9")); 28 } 29 30 bool u32string_is_utf32(); u32string_is_utf32()31bool u32string_is_utf32() 32 { 33 return (std::u32string(U"") == std::u32string(U"\U0001F4A9")); 34 } 35 } // namespace 36 37 TEST_CASE("wide strings") 38 { 39 SECTION("std::wstring") 40 { 41 if (wstring_is_utf16()) 42 { 43 std::wstring w = L"[12.2,\"Ⴥaäö\"]"; 44 json j = json::parse(w); 45 CHECK(j.dump() == "[12.2,\"Ⴥaäö\"]"); 46 } 47 } 48 49 SECTION("invalid std::wstring") 50 { 51 if (wstring_is_utf16()) 52 { 53 std::wstring w = L"\"\xDBFF"; 54 json _; 55 CHECK_THROWS_AS(_ = json::parse(w), json::parse_error&); 56 } 57 } 58 59 SECTION("std::u16string") 60 { 61 if (u16string_is_utf16()) 62 { 63 std::u16string w = u"[12.2,\"Ⴥaäö\"]"; 64 json j = json::parse(w); 65 CHECK(j.dump() == "[12.2,\"Ⴥaäö\"]"); 66 } 67 } 68 69 SECTION("invalid std::u16string") 70 { 71 if (wstring_is_utf16()) 72 { 73 std::u16string w = u"\"\xDBFF"; 74 json _; 75 CHECK_THROWS_AS(_ = json::parse(w), json::parse_error&); 76 } 77 } 78 79 SECTION("std::u32string") 80 { 81 if (u32string_is_utf32()) 82 { 83 std::u32string w = U"[12.2,\"Ⴥaäö\"]"; 84 json j = json::parse(w); 85 CHECK(j.dump() == "[12.2,\"Ⴥaäö\"]"); 86 } 87 } 88 89 SECTION("invalid std::u32string") 90 { 91 if (u32string_is_utf32()) 92 { 93 std::u32string w = U"\"\x110000"; 94 json _; 95 CHECK_THROWS_AS(_ = json::parse(w), json::parse_error&); 96 } 97 } 98 } 99 #endif 100