Newer
Older
Import / projects / Gameloft / glwebtools / source / glwebtools / glwebtools_jsonbase.cpp
#include <glwebtools/serialization/glwebtools_jsonbase.h>
#include <algorithm>

namespace glwebtools
{
	// JSONValue
	JSONValue::JSONValue()
	{
	}

	JSONValue::JSONValue(const std::string& value)
	{
		m_value = value;
	}

	JSONValue::operator const std::string&() const
	{
		return m_value;
	}
} // glwebtools

namespace glwebtools
{
	JSONValue JSONObject::s_invalid_value;

	glwebtools::Error JSONObject::Set(JSONObject::key_const_ref_type key, const JSONValue& value)
	{
		JSONObject::list_type::iterator it = Find(key);
		if (it == m_values.end())
		{
			m_values.push_back(std::make_pair(key, value));
		}
		else
		{
			it->second = value;
		}

		return glwebtools::E_SUCCESS;
	}

	glwebtools::Error JSONObject::Get(JSONObject::key_const_ref_type key, JSONValue& value) const
	{
		JSONObject::list_type::const_iterator it = Find(key);
		if (it == m_values.end())
		{
			return glwebtools::E_INVALID_PARAMETER;
		}
		else
		{
			value = it->second;
		}

		return glwebtools::E_SUCCESS;
	}

	JSONValue& JSONObject::operator[](JSONObject::key_const_ref_type key)
	{
		JSONObject::list_type::iterator it = Find(key);
		if (it == m_values.end())
		{
			m_values.push_back(std::make_pair(key, JSONValue()));
			return m_values.back().second;
		}

		return it->second;
	}

	const JSONValue& JSONObject::operator[](JSONObject::key_const_ref_type key) const
	{
		JSONObject::list_type::const_iterator it = Find(key);
		if (it == m_values.end())
		{
			return s_invalid_value;
		}

		return it->second;
	}

	void JSONObject::Clear()
	{
		m_values.clear();
	}

	JSONObject::list_type::iterator JSONObject::Find(JSONObject::key_const_ref_type key)
	{
		MatchKey predicate(key);
		return std::find_if(m_values.begin(), m_values.end(), predicate);
	}

	JSONObject::list_type::const_iterator JSONObject::Find(JSONObject::key_const_ref_type key) const
	{
		MatchKey predicate(key);
		return std::find_if(m_values.begin(), m_values.end(), predicate);
	}

	JSONObject::list_type::iterator JSONObject::begin()
	{
		return m_values.begin();
	}

	JSONObject::list_type::const_iterator JSONObject::begin() const
	{
		return m_values.begin();
	}

	JSONObject::list_type::iterator JSONObject::end()
	{
		return m_values.end();
	}

	JSONObject::list_type::const_iterator JSONObject::end() const
	{
		return m_values.end();
	}


} // glwebtools

namespace glwebtools
{
	JSONValue JSONArray::s_invalid_value;

	glwebtools::Error JSONArray::Set(JSONArray::key_const_ref_type key, const JSONValue& value)
	{
		JSONArray::list_type::iterator it = Find(key);
		if (it == m_values.end())
		{
			m_values.push_back(std::make_pair(key, value));
		}
		else
		{
			it->second = value;
		}

		return glwebtools::E_SUCCESS;
	}

	glwebtools::Error JSONArray::Get(JSONArray::key_const_ref_type key, JSONValue& value) const
	{
		JSONArray::list_type::const_iterator it = Find(key);
		if (it == m_values.end())
		{
			return glwebtools::E_INVALID_PARAMETER;
		}
		else
		{
			value = it->second;
		}

		return glwebtools::E_SUCCESS;
	}

	JSONValue& JSONArray::operator[](JSONArray::key_const_ref_type key)
	{
		JSONArray::list_type::iterator it = Find(key);
		if (it == m_values.end())
		{
			m_values.push_back(std::make_pair(key, JSONValue()));
			return m_values.back().second;
		}

		return it->second;
	}

	const JSONValue& JSONArray::operator[](JSONArray::key_const_ref_type key) const
	{
		JSONArray::list_type::const_iterator it = Find(key);
		if (it == m_values.end())
		{
			return s_invalid_value;
		}

		return it->second;
	}

	void JSONArray::Clear()
	{
		m_values.clear();
	}

	JSONArray::list_type::iterator JSONArray::Find(JSONArray::key_const_ref_type key)
	{
		MatchKey predicate(key);
		return std::find_if(m_values.begin(), m_values.end(), predicate);
	}

	JSONArray::list_type::const_iterator JSONArray::Find(JSONArray::key_const_ref_type key) const
	{
		MatchKey predicate(key);
		return std::find_if(m_values.begin(), m_values.end(), predicate);
	}

	JSONArray::list_type::iterator JSONArray::begin()
	{
		return m_values.begin();
	}

	JSONArray::list_type::const_iterator JSONArray::begin() const
	{
		return m_values.begin();
	}

	JSONArray::list_type::iterator JSONArray::end()
	{
		return m_values.end();
	}

	JSONArray::list_type::const_iterator JSONArray::end() const
	{
		return m_values.end();
	}


} // glwebtools

namespace glwebtools
{
} // glwebtools