Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CJSONVariantWriter: use RapidJSON instead of yajl
- Loading branch information
1 parent
8b0085b
commit 00f83a7
Showing
8 changed files
with
252 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,104 @@ | ||
/* | ||
* Copyright (C) 2005-2013 Team XBMC | ||
* http://xbmc.org | ||
* | ||
* This Program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2, or (at your option) | ||
* any later version. | ||
* | ||
* This Program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with XBMC; see the file COPYING. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include <locale> | ||
* Copyright (C) 2015 Team XBMC | ||
* http://xbmc.org | ||
* | ||
* This Program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2, or (at your option) | ||
* any later version. | ||
* | ||
* This Program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with XBMC; see the file COPYING. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include "JSONVariantWriter.h" | ||
#include "utils/Variant.h" | ||
|
||
std::string CJSONVariantWriter::Write(const CVariant &value, bool compact) | ||
{ | ||
std::string output; | ||
|
||
yajl_gen g = yajl_gen_alloc(NULL); | ||
yajl_gen_config(g, yajl_gen_beautify, compact ? 0 : 1); | ||
yajl_gen_config(g, yajl_gen_indent_string, "\t"); | ||
#include <rapidjson/prettywriter.h> | ||
#include <rapidjson/stringbuffer.h> | ||
#include <rapidjson/writer.h> | ||
|
||
// Set locale to classic ("C") to ensure valid JSON numbers | ||
#ifndef TARGET_WINDOWS | ||
const char *currentLocale = setlocale(LC_NUMERIC, NULL); | ||
std::string backupLocale; | ||
if (currentLocale != NULL && (currentLocale[0] != 'C' || currentLocale[1] != 0)) | ||
{ | ||
backupLocale = currentLocale; | ||
setlocale(LC_NUMERIC, "C"); | ||
} | ||
#else // TARGET_WINDOWS | ||
const wchar_t* const currentLocale = _wsetlocale(LC_NUMERIC, NULL); | ||
std::wstring backupLocale; | ||
if (currentLocale != NULL && (currentLocale[0] != L'C' || currentLocale[1] != 0)) | ||
{ | ||
backupLocale = currentLocale; | ||
_wsetlocale(LC_NUMERIC, L"C"); | ||
} | ||
#endif // TARGET_WINDOWS | ||
|
||
if (InternalWrite(g, value)) | ||
{ | ||
const unsigned char * buffer; | ||
|
||
size_t length; | ||
yajl_gen_get_buf(g, &buffer, &length); | ||
output = std::string((const char *)buffer, length); | ||
} | ||
|
||
// Re-set locale to what it was before using yajl | ||
#ifndef TARGET_WINDOWS | ||
if (!backupLocale.empty()) | ||
setlocale(LC_NUMERIC, backupLocale.c_str()); | ||
#else // TARGET_WINDOWS | ||
if (!backupLocale.empty()) | ||
_wsetlocale(LC_NUMERIC, backupLocale.c_str()); | ||
#endif // TARGET_WINDOWS | ||
|
||
yajl_gen_clear(g); | ||
yajl_gen_free(g); | ||
|
||
return output; | ||
} | ||
#include "utils/Variant.h" | ||
|
||
bool CJSONVariantWriter::InternalWrite(yajl_gen g, const CVariant &value) | ||
template<class TWriter> | ||
bool InternalWrite(TWriter& writer, const CVariant &value) | ||
{ | ||
bool success = false; | ||
|
||
switch (value.type()) | ||
{ | ||
case CVariant::VariantTypeInteger: | ||
success = yajl_gen_status_ok == yajl_gen_integer(g, (long long int)value.asInteger()); | ||
break; | ||
return writer.Int64(value.asInteger()); | ||
|
||
case CVariant::VariantTypeUnsignedInteger: | ||
success = yajl_gen_status_ok == yajl_gen_integer(g, (long long int)value.asUnsignedInteger()); | ||
break; | ||
return writer.Uint64(value.asUnsignedInteger()); | ||
|
||
case CVariant::VariantTypeDouble: | ||
success = yajl_gen_status_ok == yajl_gen_double(g, value.asDouble()); | ||
break; | ||
return writer.Double(value.asDouble()); | ||
|
||
case CVariant::VariantTypeBoolean: | ||
success = yajl_gen_status_ok == yajl_gen_bool(g, value.asBoolean() ? 1 : 0); | ||
break; | ||
return writer.Bool(value.asBoolean()); | ||
|
||
case CVariant::VariantTypeString: | ||
success = yajl_gen_status_ok == yajl_gen_string(g, (const unsigned char*)value.c_str(), (size_t)value.size()); | ||
break; | ||
return writer.String(value.c_str(), value.size()); | ||
|
||
case CVariant::VariantTypeArray: | ||
success = yajl_gen_status_ok == yajl_gen_array_open(g); | ||
if (!writer.StartArray()) | ||
return false; | ||
|
||
for (CVariant::const_iterator_array itr = value.begin_array(); itr != value.end_array() && success; ++itr) | ||
success &= InternalWrite(g, *itr); | ||
for (CVariant::const_iterator_array itr = value.begin_array(); itr != value.end_array(); ++itr) | ||
{ | ||
if (!InternalWrite(writer, *itr)) | ||
return false; | ||
} | ||
|
||
if (success) | ||
success = yajl_gen_status_ok == yajl_gen_array_close(g); | ||
return writer.EndArray(value.size()); | ||
|
||
break; | ||
case CVariant::VariantTypeObject: | ||
success = yajl_gen_status_ok == yajl_gen_map_open(g); | ||
if (!writer.StartObject()) | ||
return false; | ||
|
||
for (CVariant::const_iterator_map itr = value.begin_map(); itr != value.end_map() && success; ++itr) | ||
for (CVariant::const_iterator_map itr = value.begin_map(); itr != value.end_map(); ++itr) | ||
{ | ||
success &= yajl_gen_status_ok == yajl_gen_string(g, (const unsigned char*)itr->first.c_str(), (size_t)itr->first.length()); | ||
if (success) | ||
success &= InternalWrite(g, itr->second); | ||
if (!writer.Key(itr->first.c_str()) || | ||
!InternalWrite(writer, itr->second)) | ||
return false; | ||
} | ||
|
||
if (success) | ||
success &= yajl_gen_status_ok == yajl_gen_map_close(g); | ||
return writer.EndObject(value.size()); | ||
|
||
break; | ||
case CVariant::VariantTypeConstNull: | ||
case CVariant::VariantTypeNull: | ||
default: | ||
success = yajl_gen_status_ok == yajl_gen_null(g); | ||
break; | ||
return writer.Null(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool CJSONVariantWriter::Write(const CVariant &value, std::string& output, bool compact) | ||
{ | ||
rapidjson::StringBuffer stringBuffer; | ||
if (compact) | ||
{ | ||
rapidjson::Writer<rapidjson::StringBuffer> writer(stringBuffer); | ||
|
||
if (!InternalWrite(writer, value) || !writer.IsComplete()) | ||
return false; | ||
} | ||
else | ||
{ | ||
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(stringBuffer); | ||
writer.SetIndent('\t', 1); | ||
|
||
if (!InternalWrite(writer, value) || !writer.IsComplete()) | ||
return false; | ||
} | ||
|
||
return success; | ||
output = stringBuffer.GetString(); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
#pragma once | ||
/* | ||
* Copyright (C) 2005-2013 Team XBMC | ||
* http://xbmc.org | ||
* | ||
* This Program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2, or (at your option) | ||
* any later version. | ||
* | ||
* This Program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with XBMC; see the file COPYING. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
* Copyright (C) 2015 Team XBMC | ||
* http://xbmc.org | ||
* | ||
* This Program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2, or (at your option) | ||
* any later version. | ||
* | ||
* This Program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with XBMC; see the file COPYING. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include <yajl/yajl_gen.h> | ||
#include <string> | ||
|
||
class CVariant; | ||
|
||
class CJSONVariantWriter | ||
{ | ||
public: | ||
static std::string Write(const CVariant &value, bool compact); | ||
private: | ||
static bool InternalWrite(yajl_gen g, const CVariant &value); | ||
CJSONVariantWriter() = delete; | ||
|
||
static bool Write(const CVariant &value, std::string& output, bool compact); | ||
}; |
Oops, something went wrong.