Back to index

unity  6.0.0
JSONParser.h
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 #ifndef UNITY_CORE_JSON_PARSER_H
00021 #define UNITY_CORE_JSON_PARSER_H
00022 
00023 #include <string>
00024 #include <vector>
00025 #include <map>
00026 
00027 #include <boost/algorithm/string.hpp>
00028 
00029 #include <NuxCore/Color.h>
00030 
00031 #include <json-glib/json-glib.h>
00032 
00033 #include "UnityCore/GLibWrapper.h"
00034 
00035 namespace unity
00036 {
00037 namespace json
00038 {
00039 
00040 class Parser
00041 {
00042 public:
00043   Parser();
00044 
00045   bool Open(std::string const& filename);
00046 
00047   void ReadInt(std::string const& node_name,
00048                std::string const& member_name,
00049                int& value) const;
00050   void ReadInts(std::string const& node_name,
00051                 std::string const& member_name,
00052                 std::vector<int>& values) const;
00053 
00054   void ReadDouble(std::string const& node_name,
00055                   std::string const& member_name,
00056                   double& value) const;
00057   void ReadDoubles(std::string const& node_name,
00058                    std::string const& member_name,
00059                    std::vector<double>& values) const;
00060 
00061   void ReadColor(std::string const& node_name,
00062                  std::string const& member_name,
00063                  std::string const& opacity_name,
00064                  nux::Color& color) const;
00065   void ReadColors(std::string const& node_name,
00066                   std::string const& member_name,
00067                   std::string const& opacity_name,
00068                   std::vector<nux::Color>& colors) const;
00069 
00070   template <typename T>
00071   void ReadMappedString(std::string const& node_name,
00072                         std::string const& member_name,
00073                         std::map<std::string, T> const& mapping,
00074                         T& value) const;
00075   template <typename T>
00076   void ReadMappedStrings(std::string const& node_name,
00077                          std::string const& member_name,
00078                          std::map<std::string, T> const& mapping,
00079                          std::vector<T>& values) const;
00080 
00081 private:
00082   JsonObject* GetNodeObject(std::string const& node_name) const;
00083   JsonArray* GetArray(std::string const& node_name,
00084                       std::string const& member_name) const;
00085 
00086 private:
00087   glib::Object<JsonParser> parser_;
00088   JsonNode* root_;
00089 };
00090 
00091 
00092 template <typename T>
00093 void Parser::ReadMappedStrings(std::string const& node_name,
00094                                std::string const& member_name,
00095                                std::map<std::string, T> const& mapping,
00096                                std::vector<T>& values) const
00097 {
00098   JsonArray* array = GetArray(node_name, member_name);
00099   if (!array)
00100     return;
00101 
00102   std::size_t size = std::min<size_t>(json_array_get_length(array),
00103                                       values.size());
00104   for (std::size_t i = 0; i < size; ++i)
00105   {
00106     std::string key(json_array_get_string_element(array, i));
00107     boost::to_lower(key);
00108     auto it = mapping.find(key);
00109     if (it != mapping.end())
00110       values[i] = it->second;
00111   }
00112 }
00113 
00114 template <typename T>
00115 void Parser::ReadMappedString(std::string const& node_name,
00116                               std::string const& member_name,
00117                               std::map<std::string, T> const& mapping,
00118                               T& value) const
00119 {
00120   JsonObject* object = GetNodeObject(node_name);
00121   if (!object)
00122     return;
00123 
00124   std::string key(json_object_get_string_member(object, member_name.c_str()));
00125   boost::to_lower(key);
00126   auto it = mapping.find(key);
00127   if (it != mapping.end())
00128     value = it->second;
00129 }
00130 
00131 
00132 }
00133 }
00134 
00135 
00136 #endif