Strings unify

This commit is contained in:
Mikhail Lobotskiy
2024-03-15 17:56:17 +04:00
parent 69a71d78ed
commit be0c6b13e7
5 changed files with 31 additions and 39 deletions

View File

@ -66,16 +66,13 @@ namespace NSJSON
static_cast<CPrimitive*>(m_internal->m_value.get())->isDouble());
}
bool IValue::IsStringA() const
bool IValue::IsString() const
{
return (m_internal->m_type == CTypedValue::vtPrimitive &&
static_cast<CPrimitive*>(m_internal->m_value.get())->isStringA());
}
if (m_internal->m_type != CTypedValue::vtPrimitive)
return false;
bool IValue::IsStringW() const
{
return (m_internal->m_type == CTypedValue::vtPrimitive &&
static_cast<CPrimitive*>(m_internal->m_value.get())->isStringW());
CPrimitive* pPrimitive = static_cast<CPrimitive*>(m_internal->m_value.get());
return (pPrimitive->isStringA() || pPrimitive->isStringW());
}
bool IValue::IsArray() const

View File

@ -73,11 +73,7 @@ namespace NSJSON
/**
* Returns true if the value is a string.
*/
bool IsStringA() const;
/**
* Returns true if the value is a wstring.
*/
bool IsStringW() const;
bool IsString() const;
/**
* Returns true if the value is an array.
*/

View File

@ -1,5 +1,8 @@
#include "json_values.h"
#include <locale>
#include <codecvt>
namespace NSJSON
{
IBaseValue::IBaseValue()
@ -113,6 +116,13 @@ namespace NSJSON
{
if (m_type == ptStringA)
return m_string;
if (m_type == ptStringW)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return std::string(converter.to_bytes(m_wstring));
}
#ifdef JSON_DEBUG
throw std::bad_cast();
#endif
@ -123,6 +133,13 @@ namespace NSJSON
{
if (m_type == ptStringW)
return m_wstring;
if (m_type == ptStringA)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return std::wstring(converter.from_bytes(m_string));
}
#ifdef JSON_DEBUG
throw std::bad_cast();
#endif

View File

@ -29,13 +29,9 @@ namespace NSJSON
{
ret = NSJSBase::CJSContext::createDouble((double)value);
}
else if (value.IsStringA())
else if (value.IsString())
{
ret = NSJSBase::CJSContext::createString((std::string)value);
}
else if (value.IsStringW())
{
ret = NSJSBase::CJSContext::createString((std::wstring)value);
ret = NSJSBase::CJSContext::createString(value.ToStringA());
}
// arrays
else if (value.IsArray())