Back to index

unity  6.0.0
Public Member Functions | Private Member Functions | Private Attributes
unity::json::Parser Class Reference

#include <JSONParser.h>

Collaboration diagram for unity::json::Parser:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Parser ()
bool Open (std::string const &filename)
void ReadInt (std::string const &node_name, std::string const &member_name, int &value) const
void ReadInts (std::string const &node_name, std::string const &member_name, std::vector< int > &values) const
void ReadDouble (std::string const &node_name, std::string const &member_name, double &value) const
void ReadDoubles (std::string const &node_name, std::string const &member_name, std::vector< double > &values) const
void ReadColor (std::string const &node_name, std::string const &member_name, std::string const &opacity_name, nux::Color &color) const
void ReadColors (std::string const &node_name, std::string const &member_name, std::string const &opacity_name, std::vector< nux::Color > &colors) const
template<typename T >
void ReadMappedString (std::string const &node_name, std::string const &member_name, std::map< std::string, T > const &mapping, T &value) const
template<typename T >
void ReadMappedStrings (std::string const &node_name, std::string const &member_name, std::map< std::string, T > const &mapping, std::vector< T > &values) const

Private Member Functions

JsonObject * GetNodeObject (std::string const &node_name) const
JsonArray * GetArray (std::string const &node_name, std::string const &member_name) const

Private Attributes

glib::Object< JsonParser > parser_
JsonNode * root_

Detailed Description

Definition at line 40 of file JSONParser.h.


Constructor & Destructor Documentation

Definition at line 47 of file JSONParser.cpp.

  : root_(nullptr)
{
}

Member Function Documentation

JsonArray * unity::json::Parser::GetArray ( std::string const &  node_name,
std::string const &  member_name 
) const [private]

Definition at line 84 of file JSONParser.cpp.

{
  JsonObject* object = GetNodeObject(node_name);
  if (object)
    return json_object_get_array_member(object, member_name.c_str());
  return nullptr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JsonObject * unity::json::Parser::GetNodeObject ( std::string const &  node_name) const [private]

Definition at line 74 of file JSONParser.cpp.

{
  if (!root_)
    return nullptr;

  JsonObject* object = json_node_get_object(root_);
  JsonNode* node = json_object_get_member(object, node_name.c_str());
  return json_node_get_object(node);
}

Here is the caller graph for this function:

bool unity::json::Parser::Open ( std::string const &  filename)

Definition at line 52 of file JSONParser.cpp.

{
  glib::Error error;
  parser_ = json_parser_new();
  gboolean result = json_parser_load_from_file(parser_, filename.c_str(), &error);
  if (!result)
  {
    LOG_WARN(logger) << "Failure: " << error;
    return false;
  }

  root_ = json_parser_get_root(parser_); // not ref'ed

  if (JSON_NODE_TYPE(root_) != JSON_NODE_OBJECT)
  {
    LOG_WARN(logger) << "Root node is not an object, fail.  It's an: "
                     << json_node_type_name(root_);
    return false;
  }
  return true;
}

Here is the caller graph for this function:

void unity::json::Parser::ReadColor ( std::string const &  node_name,
std::string const &  member_name,
std::string const &  opacity_name,
nux::Color &  color 
) const

Definition at line 148 of file JSONParser.cpp.

{
  JsonObject* object = GetNodeObject(node_name);

  if (!object)
    return;

  color = ColorFromPango(json_object_get_string_member(object, member_name.c_str()));
  color.alpha = json_object_get_double_member(object, opacity_name.c_str());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void unity::json::Parser::ReadColors ( std::string const &  node_name,
std::string const &  member_name,
std::string const &  opacity_name,
std::vector< nux::Color > &  colors 
) const

Definition at line 162 of file JSONParser.cpp.

{
  JsonArray* array = GetArray(node_name, member_name);
  if (!array)
    return;

  std::size_t size = std::min<size_t>(json_array_get_length(array),
                                      colors.size());
  for (std::size_t i = 0; i < size; ++i)
  {
    colors[i] = ColorFromPango(json_array_get_string_element(array, i));
  }

  array = GetArray(node_name, opacity_name);
  if (!array)
    return;
  size = std::min<size_t>(json_array_get_length(array),
                          colors.size());
  for (std::size_t i = 0; i < size; ++i)
    colors[i].alpha = json_array_get_double_element(array, i);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void unity::json::Parser::ReadDouble ( std::string const &  node_name,
std::string const &  member_name,
double &  value 
) const

Definition at line 121 of file JSONParser.cpp.

{
  JsonObject* object = GetNodeObject(node_name);

  if (!object)
    return;

  value = json_object_get_double_member(object, member_name.c_str());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void unity::json::Parser::ReadDoubles ( std::string const &  node_name,
std::string const &  member_name,
std::vector< double > &  values 
) const

Definition at line 133 of file JSONParser.cpp.

{
  JsonArray* array = GetArray(node_name, member_name);

  if (!array)
    return;

  std::size_t size = std::min<size_t>(json_array_get_length(array),
                                      values.size());
  for (std::size_t i = 0; i < size; ++i)
    values[i] = json_array_get_double_element(array, i);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void unity::json::Parser::ReadInt ( std::string const &  node_name,
std::string const &  member_name,
int &  value 
) const

Definition at line 94 of file JSONParser.cpp.

{
  JsonObject* object = GetNodeObject(node_name);

  if (!object)
    return;

  value = json_object_get_int_member(object, member_name.c_str());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void unity::json::Parser::ReadInts ( std::string const &  node_name,
std::string const &  member_name,
std::vector< int > &  values 
) const

Definition at line 106 of file JSONParser.cpp.

{
  JsonArray* array = GetArray(node_name, member_name);

  if (!array)
    return;

  std::size_t size = std::min<size_t>(json_array_get_length(array),
                                      values.size());
  for (std::size_t i = 0; i < size; ++i)
    values[i] = json_array_get_int_element(array, i);
}

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
void unity::json::Parser::ReadMappedString ( std::string const &  node_name,
std::string const &  member_name,
std::map< std::string, T > const &  mapping,
T &  value 
) const

Definition at line 115 of file JSONParser.h.

{
  JsonObject* object = GetNodeObject(node_name);
  if (!object)
    return;

  std::string key(json_object_get_string_member(object, member_name.c_str()));
  boost::to_lower(key);
  auto it = mapping.find(key);
  if (it != mapping.end())
    value = it->second;
}

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
void unity::json::Parser::ReadMappedStrings ( std::string const &  node_name,
std::string const &  member_name,
std::map< std::string, T > const &  mapping,
std::vector< T > &  values 
) const

Definition at line 93 of file JSONParser.h.

{
  JsonArray* array = GetArray(node_name, member_name);
  if (!array)
    return;

  std::size_t size = std::min<size_t>(json_array_get_length(array),
                                      values.size());
  for (std::size_t i = 0; i < size; ++i)
  {
    std::string key(json_array_get_string_element(array, i));
    boost::to_lower(key);
    auto it = mapping.find(key);
    if (it != mapping.end())
      values[i] = it->second;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 87 of file JSONParser.h.

JsonNode* unity::json::Parser::root_ [private]

Definition at line 88 of file JSONParser.h.


The documentation for this class was generated from the following files: