Back to index

unity  6.0.0
JSONParser.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Tim Penhey <tim.penhey@canonical.com>
00018  *              Mirco Müller <mirco.mueller@canonical.com
00019  */
00020 
00021 #include "JSONParser.h"
00022 
00023 #include <pango/pango.h>
00024 
00025 #include <NuxCore/Logger.h>
00026 
00027 namespace unity
00028 {
00029 namespace json
00030 {
00031 namespace
00032 {
00033 nux::logging::Logger logger("unity.json");
00034 
00035 nux::Color ColorFromPango(const gchar* color_string)
00036 {
00037   static const float PANGO_MAX = 0xffff;
00038   PangoColor color = {0, 0, 0};
00039   ::pango_color_parse(&color, color_string);
00040   return nux::Color(color.red / PANGO_MAX,
00041                     color.green / PANGO_MAX,
00042                     color.blue  / PANGO_MAX);
00043 }
00044 
00045 }
00046 
00047 Parser::Parser()
00048   : root_(nullptr)
00049 {
00050 }
00051 
00052 bool Parser::Open(std::string const& filename)
00053 {
00054   glib::Error error;
00055   parser_ = json_parser_new();
00056   gboolean result = json_parser_load_from_file(parser_, filename.c_str(), &error);
00057   if (!result)
00058   {
00059     LOG_WARN(logger) << "Failure: " << error;
00060     return false;
00061   }
00062 
00063   root_ = json_parser_get_root(parser_); // not ref'ed
00064 
00065   if (JSON_NODE_TYPE(root_) != JSON_NODE_OBJECT)
00066   {
00067     LOG_WARN(logger) << "Root node is not an object, fail.  It's an: "
00068                      << json_node_type_name(root_);
00069     return false;
00070   }
00071   return true;
00072 }
00073 
00074 JsonObject* Parser::GetNodeObject(std::string const& node_name) const
00075 {
00076   if (!root_)
00077     return nullptr;
00078 
00079   JsonObject* object = json_node_get_object(root_);
00080   JsonNode* node = json_object_get_member(object, node_name.c_str());
00081   return json_node_get_object(node);
00082 }
00083 
00084 JsonArray* Parser::GetArray(std::string const& node_name,
00085                             std::string const& member_name) const
00086 {
00087   JsonObject* object = GetNodeObject(node_name);
00088   if (object)
00089     return json_object_get_array_member(object, member_name.c_str());
00090   return nullptr;
00091 }
00092 
00093 
00094 void Parser::ReadInt(std::string const& node_name,
00095                      std::string const& member_name,
00096                      int& value) const
00097 {
00098   JsonObject* object = GetNodeObject(node_name);
00099 
00100   if (!object)
00101     return;
00102 
00103   value = json_object_get_int_member(object, member_name.c_str());
00104 }
00105 
00106 void Parser::ReadInts(std::string const& node_name,
00107                       std::string const& member_name,
00108                       std::vector<int>& values) const
00109 {
00110   JsonArray* array = GetArray(node_name, member_name);
00111 
00112   if (!array)
00113     return;
00114 
00115   std::size_t size = std::min<size_t>(json_array_get_length(array),
00116                                       values.size());
00117   for (std::size_t i = 0; i < size; ++i)
00118     values[i] = json_array_get_int_element(array, i);
00119 }
00120 
00121 void Parser::ReadDouble(std::string const& node_name,
00122                         std::string const& member_name,
00123                         double& value) const
00124 {
00125   JsonObject* object = GetNodeObject(node_name);
00126 
00127   if (!object)
00128     return;
00129 
00130   value = json_object_get_double_member(object, member_name.c_str());
00131 }
00132 
00133 void Parser::ReadDoubles(std::string const& node_name,
00134                          std::string const& member_name,
00135                          std::vector<double>& values) const
00136 {
00137   JsonArray* array = GetArray(node_name, member_name);
00138 
00139   if (!array)
00140     return;
00141 
00142   std::size_t size = std::min<size_t>(json_array_get_length(array),
00143                                       values.size());
00144   for (std::size_t i = 0; i < size; ++i)
00145     values[i] = json_array_get_double_element(array, i);
00146 }
00147 
00148 void Parser::ReadColor(std::string const& node_name,
00149                        std::string const& member_name,
00150                        std::string const& opacity_name,
00151                        nux::Color& color) const
00152 {
00153   JsonObject* object = GetNodeObject(node_name);
00154 
00155   if (!object)
00156     return;
00157 
00158   color = ColorFromPango(json_object_get_string_member(object, member_name.c_str()));
00159   color.alpha = json_object_get_double_member(object, opacity_name.c_str());
00160 }
00161 
00162 void Parser::ReadColors(std::string const& node_name,
00163                         std::string const& member_name,
00164                         std::string const& opacity_name,
00165                         std::vector<nux::Color>& colors) const
00166 {
00167   JsonArray* array = GetArray(node_name, member_name);
00168   if (!array)
00169     return;
00170 
00171   std::size_t size = std::min<size_t>(json_array_get_length(array),
00172                                       colors.size());
00173   for (std::size_t i = 0; i < size; ++i)
00174   {
00175     colors[i] = ColorFromPango(json_array_get_string_element(array, i));
00176   }
00177 
00178   array = GetArray(node_name, opacity_name);
00179   if (!array)
00180     return;
00181   size = std::min<size_t>(json_array_get_length(array),
00182                           colors.size());
00183   for (std::size_t i = 0; i < size; ++i)
00184     colors[i].alpha = json_array_get_double_element(array, i);
00185 }
00186 
00187 
00188 
00189 } // namespace json
00190 } // namespace unity