Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 49 additions & 38 deletions src/wxSimpleJSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,37 @@ wxSimpleJSON &wxSimpleJSON::Add(const wxString &name, wxSimpleJSON::Ptr_t obj)
{
DeleteProperty(name);

cJSON_AddItemToObject(m_d, name.mb_str(wxConvUTF8).data(), obj->m_d);
cJSON_AddItemToObject(m_d, name.utf8_str().data(), obj->m_d);
return *this;
}

wxSimpleJSON &wxSimpleJSON::Add(const wxString &name, const wxString &value, const wxMBConv &conv)
{
DeleteProperty(name);

cJSON_AddStringToObject(m_d, name.mb_str(wxConvUTF8).data(), value.mb_str(conv).data());
cJSON_AddStringToObject(m_d, name.utf8_str().data(), value.mb_str(conv).data());
return *this;
}

wxSimpleJSON &wxSimpleJSON::Add(const wxString &name, double value)
{
DeleteProperty(name);

cJSON_AddNumberToObject(m_d, name.mb_str(wxConvUTF8).data(), value);
cJSON_AddNumberToObject(m_d, name.utf8_str().data(), value);
return *this;
}

wxSimpleJSON &wxSimpleJSON::AddNull(const wxString &name)
{
DeleteProperty(name);

cJSON_AddNullToObject(m_d, name.mb_str(wxConvUTF8).data());
cJSON_AddNullToObject(m_d, name.utf8_str().data());
return *this;
}

wxSimpleJSON &wxSimpleJSON::ArrayAdd(const wxArrayString &arr, const wxMBConv &conv)
{
wxSimpleJSON::Ptr_t parr = Create(wxSimpleJSON::IS_ARRAY);
wxSimpleJSON::Ptr_t parr = Create(wxSimpleJSON::JSONType::IS_ARRAY);
for(size_t i = 0; i < arr.size(); ++i) {
parr->ArrayAdd(arr.Item(i), conv);
}
Expand All @@ -107,7 +107,7 @@ wxSimpleJSON &wxSimpleJSON::Add(const wxString &name, const wxArrayString &arr,
{
DeleteProperty(name);

wxSimpleJSON::Ptr_t parr = Create(wxSimpleJSON::IS_ARRAY);
wxSimpleJSON::Ptr_t parr = Create(wxSimpleJSON::JSONType::IS_ARRAY);
for(size_t i = 0; i < arr.size(); ++i) {
parr->ArrayAdd(arr.Item(i), conv);
}
Expand All @@ -126,7 +126,7 @@ wxSimpleJSON::Ptr_t wxSimpleJSON::Item(size_t index) const
return Create(item);
}

wxString wxSimpleJSON::GetValueString(const wxString &defaultValue,
wxString wxSimpleJSON::AsString(const wxString &defaultValue,
const wxMBConv &conv) const
{
if(!m_d || (m_d->type != cJSON_String)) {
Expand All @@ -135,66 +135,75 @@ wxString wxSimpleJSON::GetValueString(const wxString &defaultValue,
return wxString(m_d->valuestring, conv);
}

wxArrayString wxSimpleJSON::GetValueArrayString(const wxMBConv &conv) const
{
wxArrayString wxSimpleJSON::AsArrayString(const wxMBConv &conv) const
{
if(!m_d || (m_d->type != cJSON_Array)) {
return wxArrayString();
}

wxArrayString arr;
wxSimpleJSON::Ptr_t parr = Create(m_d);
for(size_t i = 0; i < parr->ArraySize(); ++i) {
arr.Add(parr->Item(i)->GetValueString(wxEmptyString, conv));
if (parr->Item(i)->IsValueString())
{
arr.Add(parr->Item(i)->AsString(wxEmptyString, conv));
}
}
return arr;
}

std::vector<wxString> wxSimpleJSON::GetValueStringVector(const wxMBConv& conv) const
{
std::vector<wxString> wxSimpleJSON::AsStrings(const wxMBConv& conv) const
{
if (!m_d || (m_d->type != cJSON_Array)) {
return std::vector<wxString>();
}

std::vector<wxString> arr;
wxSimpleJSON::Ptr_t parr = Create(m_d);
for (size_t i = 0; i < parr->ArraySize(); ++i) {
arr.emplace_back(parr->Item(i)->GetValueString(wxEmptyString, conv));
if (parr->Item(i)->IsValueString())
{
arr.emplace_back(parr->Item(i)->AsString(wxEmptyString, conv));
}
}
return arr;
}

std::vector<wxSimpleJSON::Ptr_t> wxSimpleJSON::GetValueArrayObject() const
{
std::vector<wxSimpleJSON::Ptr_t> wxSimpleJSON::AsNodes() const
{
if(!m_d || (m_d->type != cJSON_Array)) {
return std::vector<wxSimpleJSON::Ptr_t>();
}

std::vector<wxSimpleJSON::Ptr_t> arr;
wxSimpleJSON::Ptr_t parr = Create(m_d);
for(size_t i = 0; i < parr->ArraySize(); ++i) {
arr.push_back(parr->Item(i));
arr.push_back(parr->Item(i));
}
return arr;
}

double wxSimpleJSON::GetValueNumber(double defaultValue) const
{
double wxSimpleJSON::AsDouble(double defaultValue) const
{
if(!m_d || (m_d->type != cJSON_Number)) {
return defaultValue;
}
return m_d->valuedouble;
}

std::vector<double> wxSimpleJSON::GetValueArrayNumber(double defaultValue) const
{
std::vector<double> wxSimpleJSON::AsDoubles(double defaultValue) const
{
if (!m_d || (m_d->type != cJSON_Array)) {
return std::vector<double>();
}

std::vector<double> arr;
wxSimpleJSON::Ptr_t parr = Create(m_d);
for (size_t i = 0; i < parr->ArraySize(); ++i) {
arr.emplace_back(parr->Item(i)->GetValueNumber(defaultValue));
if (parr->Item(i)->IsValueNumber())
{
arr.emplace_back(parr->Item(i)->AsDouble(defaultValue));
}
}
return arr;
}
Expand All @@ -204,7 +213,7 @@ wxSimpleJSON::Ptr_t wxSimpleJSON::GetProperty(const wxString &name) const
if(!m_d || (m_d->type != cJSON_Object)) {
return Create(nullptr);
}
return Create(cJSON_GetObjectItem(m_d, name.mb_str(wxConvUTF8).data()));
return Create(cJSON_GetObjectItem(m_d, name.utf8_str().data()));
}

wxSimpleJSON &wxSimpleJSON::ArrayAdd(bool value)
Expand All @@ -229,11 +238,11 @@ wxSimpleJSON &wxSimpleJSON::Add(const wxString &name, bool value)
if(!m_d || (m_d->type != cJSON_Object)) {
return *this;
}
cJSON_AddBoolToObject(m_d, name.mb_str(wxConvUTF8).data(), value);
cJSON_AddBoolToObject(m_d, name.utf8_str().data(), value);
return *this;
}

bool wxSimpleJSON::GetValueBool(bool defaultValue) const
bool wxSimpleJSON::AsBool(bool defaultValue) const
{
if(!m_d || (m_d->type != cJSON_True && m_d->type != cJSON_False)) {
return defaultValue;
Expand All @@ -242,16 +251,16 @@ bool wxSimpleJSON::GetValueBool(bool defaultValue) const
return m_d->type == cJSON_True;
}

std::vector<bool> wxSimpleJSON::GetValueArrayBool(bool defaultValue) const
{
std::vector<bool> wxSimpleJSON::AsBools(bool defaultValue) const
{
if (!m_d || (m_d->type != cJSON_Array)) {
return std::vector<bool>();
}

std::vector<bool> arr;
wxSimpleJSON::Ptr_t parr = Create(m_d);
for (size_t i = 0; i < parr->ArraySize(); ++i) {
arr.push_back(parr->Item(i)->GetValueBool(defaultValue));
arr.push_back(parr->Item(i)->AsBool(defaultValue));
}
return arr;
}
Expand Down Expand Up @@ -282,8 +291,8 @@ wxSimpleJSON::Ptr_t wxSimpleJSON::Create(const wxString &buffer, bool isRoot, co
}
}
// get the text where the error occurred
wxString errorLine(startOfErrorLine, conv, endOfErrorLine-startOfErrorLine);
wxString errorLineStartOfError(parseEnd, conv, endOfErrorLine-parseEnd);
const wxString errorLine(startOfErrorLine, conv, endOfErrorLine-startOfErrorLine);
const wxString errorLineStartOfError(parseEnd, conv, endOfErrorLine-parseEnd);
parsedNode->SetLastError(
wxString::Format(_(L"JSON parsing error at line %s, column %s.\n\n"
"full line:\n%s\n\n"
Expand All @@ -297,12 +306,12 @@ wxSimpleJSON::Ptr_t wxSimpleJSON::Create(const wxString &buffer, bool isRoot, co
return parsedNode;
}

wxSimpleJSON::Ptr_t wxSimpleJSON::LoadFile(const wxFileName &filename, const wxMBConv &conv)
wxSimpleJSON::Ptr_t wxSimpleJSON::LoadFile(const wxString &filename, const wxMBConv &conv)
{
if(!filename.Exists()) {
if (!wxFileName{ filename }.Exists()) {
return Create(nullptr);
}
wxFFile fp(filename.GetFullPath(), "rb");
wxFFile fp(filename, "rb");
wxString content;
if(fp.IsOpened() && fp.ReadAll(&content, conv)) {
fp.Close();
Expand All @@ -311,9 +320,9 @@ wxSimpleJSON::Ptr_t wxSimpleJSON::LoadFile(const wxFileName &filename, const wxM
return Create(nullptr);
}

bool wxSimpleJSON::Save(const wxFileName &filename, const wxMBConv &conv)
bool wxSimpleJSON::Save(const wxString &filename, const wxMBConv &conv) const
{
wxFFile fp(filename.GetFullPath(), "wb");
wxFFile fp(filename, "wb");
if(fp.IsOpened()) {
fp.Write(Print(true, conv), conv);
fp.Close();
Expand All @@ -327,19 +336,21 @@ bool wxSimpleJSON::DeleteProperty(const wxString &name)
if(!m_d || (m_d->type != cJSON_Object)) {
return false;
}
cJSON *p = cJSON_GetObjectItem(m_d, name.mb_str(wxConvUTF8).data());
cJSON *p = cJSON_GetObjectItem(m_d, name.utf8_str().data());
if(!p) {
return false;
}
cJSON_DeleteItemFromObject(m_d, name.mb_str(wxConvUTF8).data());
cJSON_DeleteItemFromObject(m_d, name.utf8_str().data());
return true;
}

bool wxSimpleJSON::DeleteProperty(int idx)
{
if(!m_d || (m_d->type != cJSON_Array)) {
if(!m_d || (m_d->type != cJSON_Array) ||
idx < 0 || idx >= cJSON_GetArraySize(m_d)) {
return false;
}

cJSON_DeleteItemFromArray(m_d, idx);
return true;
}
Expand All @@ -349,7 +360,7 @@ bool wxSimpleJSON::HasProperty(const wxString& name)
if(!m_d || (m_d->type != cJSON_Object)) {
return false;
}
cJSON *p = cJSON_GetObjectItem(m_d, name.mb_str(wxConvUTF8).data());
cJSON *p = cJSON_GetObjectItem(m_d, name.utf8_str().data());
if(!p) {
return false;
}
Expand Down
Loading