Comments (3)
I can also workaround this problem by adding a manifest XML that sets my app's code page to CP_UTF8
on supported versions of windows.
In CMake I wrapped this in a function:
# target_add_manifest(<target> <manifest file>)
#
# You probably want to use ${MANIFEST_FILE_UTF8} defined below this function
#
# Adds a manifest file (https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests)
# to an EXE
function(target_add_manifest TARGET_NAME MANIFEST_FILE)
if(NOT TARGET_NAME)
message(FATAL_ERROR "You must provide a target")
endif()
if(NOT MANIFEST_FILE)
message(FATAL_ERROR "You must provide a manifest file")
endif()
add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND "mt.exe" -manifest \"${MANIFEST_FILE}\" \"-updateresource:$<TARGET_FILE:${TARGET_NAME}>\"
)
endfunction()
which is used like this (probably want to wrap in a platform check):
add_executable(myapp main.cpp)
target_add_manifest(myapp "${CMAKE_CURRENT_SOURCE_DIR}/cmake/utf8.manifest")
with utf8.manifest
being:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<application>
<windowsSettings>
<activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
</windowsSettings>
</application>
</assembly>
This solves the problem, if the app is running on at least Windows Version 1903. Still a bug but wanted to share this workaround because it's useful for many libraries that have the same issue.
from json.
Proposed diff to do the conversion to UTF-8 when targeting windows:
diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp
index 562089c3..a8b74688 100644
--- a/include/nlohmann/detail/conversions/to_json.hpp
+++ b/include/nlohmann/detail/conversions/to_json.hpp
@@ -413,10 +413,20 @@ inline void to_json(BasicJsonType& j, const T& t)
}
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
+#if defined(_WIN32)
+#include <windows.h>
+#endif
template<typename BasicJsonType>
inline void to_json(BasicJsonType& j, const std_fs::path& p)
{
+#if defined(_WIN32)
+ int len = ::WideCharToMultiByte(CP_UTF8, 0, &p.native()[0], p.native().size(), nullptr, 0, nullptr, nullptr);
+ std::string as_utf8(len, 0);
+ ::WideCharToMultiByte(CP_UTF8, 0, &p.native()[0], p.native().size(), &narrowed_string[0], len, nullptr, nullptr);
+ j = std::move(as_utf8);
+#else
j = p.string();
+#endif
}
#endif
from json.
path may be represented in some ways (native/generic_string/string/u8string/e.t.c), so, I think it should be decided on client side how to store it before put it to json object.
Just j = p.u8string();
may be should be there to corresponds the docs.
from json.
Related Issues (20)
- json.exception.type_error.302 HOT 3
- Error 302: type must be number, but is it number HOT 2
- nlohmann::ordered_json: not compatible with NLOHMANN_DEFINE_TYPE_INTRUSIVE HOT 1
- when enum is used as key in a map the json format is not an expected format HOT 2
- JSON can't parse a simple data HOT 2
- JSON_BuildTests fail when JSON_DisableEnumSerialization is set to ON
- VS 2022 latest version on win 10 latest version don't compile any recent version of JSON... HOT 2
- Crashes when I try to use βjson::at()β on a properly structured, non null, and correctly constructed β.jsonβ file HOT 2
- Unable to parse JSON string from snake case to camel case HOT 1
- Failing to read complex Unicode string embedded in JSON HOT 3
- CVE-2024-39705
- CVE-2024-22190
- CVE-2024-3651
- CVE-2024-22195, CVE-2024-34064
- CVE-2024-35195
- Tornado vulnerabilities
- CVE-2024-37891
- CVE-2024-5569
- CVE-2024-39689
- CVE-2024-34062
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json.