Skip to content

Commit 648c18f

Browse files
committed
Fixing locale issues in json library, on Linux.
1 parent fe1a299 commit 648c18f

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

Release/include/cpprest/asyncrt_utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace pplx = Concurrency;
4848

4949
#ifndef _MS_WINDOWS
5050
#include <boost/algorithm/string.hpp>
51+
#include <xlocale.h>
5152
#endif
5253

5354
/// Various utilities for string conversions and date and time manipulation.
@@ -171,6 +172,8 @@ namespace details
171172
#ifdef _MS_WINDOWS
172173
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
173174
m_prevLocale = setlocale(LC_ALL, locale);
175+
#else
176+
m_prevLocale = uselocale(newlocale(LC_ALL, locale, nullptr));
174177
#endif
175178
}
176179

@@ -180,13 +183,18 @@ namespace details
180183
{
181184
#ifdef _MS_WINDOWS
182185
setlocale(LC_ALL, m_prevLocale);
186+
#else
187+
locale_t original = uselocale(m_prevLocale);
188+
freelocale(original);
183189
#endif
184190
}
185191
}
186192

187193
private:
188194
#ifdef _MS_WINDOWS
189195
char * m_prevLocale;
196+
#else
197+
locale_t m_prevLocale;
190198
#endif
191199
};
192200

Release/src/json/json_serialization.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "stdafx.h"
2929
#include <stdio.h>
30+
#include <inttypes.h>
3031

3132
using namespace web;
3233
using namespace web::json;
@@ -145,28 +146,26 @@ void web::json::details::_Number::format(std::basic_string<char>& stream) const
145146
{
146147
if(m_number.m_type != number::type::double_type)
147148
{
148-
#ifdef _MS_WINDOWS
149149
// #digits + 1 to avoid loss + 1 for the sign + 1 for null terminator.
150150
const size_t tempSize = std::numeric_limits<uint64_t>::digits10 + 3;
151151
char tempBuffer[tempSize];
152152

153+
#ifdef _MS_WINDOWS
153154
// This can be improved performance-wise if we implement our own routine
154155
if (m_number.m_type == number::type::signed_type)
155156
_i64toa_s(m_number.m_intval, tempBuffer, tempSize, 10);
156157
else
157158
_ui64toa_s(m_number.m_uintval, tempBuffer, tempSize, 10);
158159

159160
const auto numChars = strnlen_s(tempBuffer, tempSize);
160-
stream.append(tempBuffer, numChars);
161161
#else
162-
std::stringstream ss; // TODO - also consider replacing with snprintf...
162+
int numChars;
163163
if (m_number.m_type == number::type::signed_type)
164-
ss << m_number.m_intval;
164+
numChars = snprintf(tempBuffer, tempSize, "%" PRId64, m_number.m_intval);
165165
else
166-
ss << m_number.m_uintval;
167-
168-
stream.append(ss.str());
166+
numChars = snprintf(tempBuffer, tempSize, "%" PRIu64, m_number.m_uintval);
169167
#endif
168+
stream.append(tempBuffer, numChars);
170169
}
171170
else
172171
{

0 commit comments

Comments
 (0)