// BlockyFroggy
// Copyright © 2017 John Ryland.
// All rights reserved.
#include "Json.h"
const JsonValue ConstJsonValueRef::s_null;
ConstJsonValueRef::ConstJsonValueRef(const JsonValue& refVal)
: m_val(refVal)
{
}
const JsonValue& ConstJsonValueRef::value()
{
return m_val;
}
void ConstJsonValueRef::setName(const char* name)
{
m_name = name;
}
std::string ConstJsonValueRef::getName()
{
return m_name;
}
int ConstJsonValueRef::operator()(int def)
{
if (m_val.IsInt())
return m_val.GetInt();
return def;
}
const char* ConstJsonValueRef::operator()(const char* def)
{
if (m_val.IsString())
return m_val.GetString();
return def;
}
ConstJsonValueRef ConstJsonValueRef::operator[](const char* n)
{
if (m_val.HasMember(n))
return m_val[n];
return s_null;
}
ConstJsonValueRef::ConstJsonValueRefIterator ConstJsonValueRef::begin()
{
ConstJsonValueRefIterator i(m_val.GetType());
if (i.m_type == rapidjson::kArrayType)
i.m_arrayIterator = m_val.Begin();
if (i.m_type == rapidjson::kObjectType)
i.m_objectIterator = m_val.MemberBegin();
return i;
}
ConstJsonValueRef::ConstJsonValueRefIterator ConstJsonValueRef::end()
{
ConstJsonValueRefIterator i(m_val.GetType());
if (i.m_type == rapidjson::kArrayType)
i.m_arrayIterator = m_val.End();
if (i.m_type == rapidjson::kObjectType)
i.m_objectIterator = m_val.MemberEnd();
return i;
}
ConstJsonValueRef::ConstJsonValueRefIterator::ConstJsonValueRefIterator(rapidjson::Type t)
: m_type(t)
, m_index(0)
{
}
std::string ConstJsonValueRef::ConstJsonValueRefIterator::GetName()
{
if (m_type == rapidjson::kArrayType)
return "[" + std::to_string(m_index) + "]";
if (m_type == rapidjson::kObjectType)
return m_objectIterator->name.GetString();
return "invalid";
}
ConstJsonValueRef ConstJsonValueRef::ConstJsonValueRefIterator::operator*()
{
if (m_type == rapidjson::kArrayType)
{
ConstJsonValueRef ret(*m_arrayIterator);
ret.setName(GetName().c_str());
return ret;
}
if (m_type == rapidjson::kObjectType)
{
ConstJsonValueRef ret(m_objectIterator->value);
ret.setName(GetName().c_str());
return ret;
}
return s_null;
}
void ConstJsonValueRef::ConstJsonValueRefIterator::operator++()
{
m_index++;
if (m_type == rapidjson::kArrayType)
++m_arrayIterator;
if (m_type == rapidjson::kObjectType)
++m_objectIterator;
}
bool ConstJsonValueRef::ConstJsonValueRefIterator::operator!=(ConstJsonValueRef::ConstJsonValueRefIterator other)
{
if (m_type == rapidjson::kArrayType)
return m_arrayIterator != other.m_arrayIterator;
if (m_type == rapidjson::kObjectType)
return m_objectIterator != other.m_objectIterator;
return false;
}