diff options
-rw-r--r-- | xbmc/utils/JSONVariantWriter.cpp | 100 | ||||
-rw-r--r-- | xbmc/utils/JSONVariantWriter.h | 32 | ||||
-rw-r--r-- | xbmc/utils/Makefile | 1 |
3 files changed, 133 insertions, 0 deletions
diff --git a/xbmc/utils/JSONVariantWriter.cpp b/xbmc/utils/JSONVariantWriter.cpp new file mode 100644 index 0000000000..fb02dfcffe --- /dev/null +++ b/xbmc/utils/JSONVariantWriter.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2005-2011 Team XBMC + * http://www.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, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "JSONVariantWriter.h" + +using namespace std; + +string CJSONVariantWriter::Write(const CVariant &value, bool compact) +{ + string output; + + yajl_gen_config conf = { compact ? 0 : 1, "\t" }; + yajl_gen g = yajl_gen_alloc(&conf, NULL); + + if (InternalWrite(g, value)) + { + const unsigned char * buffer; + unsigned int length; + + yajl_gen_get_buf(g, &buffer, &length); + output = string((const char *)buffer, length); + } + + yajl_gen_clear(g); + + return output; +} + +bool CJSONVariantWriter::InternalWrite(yajl_gen g, const CVariant &value) +{ + bool success = false; + + switch (value.type()) + { + case CVariant::VariantTypeInteger: + success = yajl_gen_status_ok == yajl_gen_integer(g, (long int)value.asInteger()); + break; + case CVariant::VariantTypeUnsignedInteger: + success = yajl_gen_status_ok == yajl_gen_integer(g, (long int)value.asUnsignedInteger()); + break; + case CVariant::VariantTypeDouble: + success = yajl_gen_status_ok == yajl_gen_double(g, value.asDouble()); + break; + case CVariant::VariantTypeBoolean: + success = yajl_gen_status_ok == yajl_gen_bool(g, value.asBoolean() ? 1 : 0); + break; + case CVariant::VariantTypeString: + success = yajl_gen_status_ok == yajl_gen_string(g, (const unsigned char*)value.c_str(), value.size()); + break; + case CVariant::VariantTypeArray: + success = yajl_gen_status_ok == yajl_gen_array_open(g); + + for (CVariant::const_iterator_array itr = value.begin_array(); itr != value.end_array() && success; itr++) + success &= InternalWrite(g, *itr); + + if (success) + success = yajl_gen_status_ok == yajl_gen_array_close(g); + + break; + case CVariant::VariantTypeObject: + success = yajl_gen_status_ok == yajl_gen_map_open(g); + + for (CVariant::const_iterator_map itr = value.begin_map(); itr != value.end_map() && success; itr++) + { + success &= yajl_gen_status_ok == yajl_gen_string(g, (const unsigned char*)itr->first.c_str(), itr->first.length()); + if (success) + success &= InternalWrite(g, itr->second); + } + + if (success) + success &= yajl_gen_status_ok == yajl_gen_map_close(g); + + break; + case CVariant::VariantTypeConstNull: + case CVariant::VariantTypeNull: + default: + success = yajl_gen_status_ok == yajl_gen_null(g); + break; + } + + return success; +} diff --git a/xbmc/utils/JSONVariantWriter.h b/xbmc/utils/JSONVariantWriter.h new file mode 100644 index 0000000000..3f3c304e07 --- /dev/null +++ b/xbmc/utils/JSONVariantWriter.h @@ -0,0 +1,32 @@ +#pragma once +/* + * Copyright (C) 2005-2011 Team XBMC + * http://www.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, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "Variant.h" +#include <yajl/yajl_gen.h> + +class CJSONVariantWriter +{ +public: + static std::string Write(const CVariant &value, bool compact); +private: + static bool InternalWrite(yajl_gen g, const CVariant &value); +}; diff --git a/xbmc/utils/Makefile b/xbmc/utils/Makefile index 44e7502816..2eb81d555b 100644 --- a/xbmc/utils/Makefile +++ b/xbmc/utils/Makefile @@ -23,6 +23,7 @@ SRCS=AlarmClock.cpp \ InfoLoader.cpp \ JobManager.cpp \ JSONVariantParser.cpp \ + JSONVariantWriter.cpp \ LabelFormatter.cpp \ LangCodeExpander.cpp \ LCD.cpp \ |