text: /***
  	MIT License
  
  	Copyright (c) 2018 NUMERARE
  
  	Permission is hereby granted, free of charge, to any person obtaining a copy
  	of this software and associated documentation files (the "Software"), to deal
  	in the Software without restriction, including without limitation the rights
  	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  	copies of the Software, and to permit persons to whom the Software is
  	furnished to do so, subject to the following conditions:
  
  	The above copyright notice and this permission notice shall be included in all
  	copies or substantial portions of the Software.
  
  	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  	SOFTWARE.
  
  	Parts of this file are originally Copyright (c) 2012-2017 The CryptoNote developers, The Bytecoin developers
  ***/
  
  #include "JsonInputValueSerializer.h"
  
  #include <cassert>
  #include <stdexcept>
  
  #include "Common/StringTools.h"
  
  using Common::JsonValue;
  using namespace CryptoNote;
  
  JsonInputValueSerializer::JsonInputValueSerializer(const Common::JsonValue& value) {
    if (!value.isObject()) {
      throw std::runtime_error("Serializer doesn't support this type of serialization: Object expected.");
    }
  
    chain.push_back(&value);
  }
  
  JsonInputValueSerializer::JsonInputValueSerializer(Common::JsonValue&& value) : value(std::move(value)) {
    if (!this->value.isObject()) {
      throw std::runtime_error("Serializer doesn't support this type of serialization: Object expected.");
    }
  
    chain.push_back(&this->value);
  }
  
  JsonInputValueSerializer::~JsonInputValueSerializer() {
  }
  
  ISerializer::SerializerType JsonInputValueSerializer::type() const {
    return ISerializer::INPUT;
  }
  
  bool JsonInputValueSerializer::beginObject(Common::StringView name) {
    const JsonValue* parent = chain.back();
  
    if (parent->isArray()) {
      const JsonValue& v = (*parent)[idxs.back()++];
      chain.push_back(&v);
      return true;
    }
  
    if (parent->contains(std::string(name))) {
      const JsonValue& v = (*parent)(std::string(name));
      chain.push_back(&v);
      return true;
    }
  
    return false;
  }
  
  void JsonInputValueSerializer::endObject() {
    assert(!chain.empty());
    chain.pop_back();
  }
  
  bool JsonInputValueSerializer::beginArray(size_t& size, Common::StringView name) {
    const JsonValue* parent = chain.back();
    std::string strName(name);
  
    if (parent->contains(strName)) {
      const JsonValue& arr = (*parent)(strName);
      size = arr.size();
      chain.push_back(&arr);
      idxs.push_back(0);
      return true;
    }
   
    size = 0;
    return false;
  }
  
  void JsonInputValueSerializer::endArray() {
    assert(!chain.empty());
    assert(!idxs.empty());
  
    chain.pop_back();
    idxs.pop_back();
  }
  
  bool JsonInputValueSerializer::operator()(uint16_t& value, Common::StringView name) {
    return getNumber(name, value);
  }
  
  bool JsonInputValueSerializer::operator()(int16_t& value, Common::StringView name) {
    return getNumber(name, value);
  }
  
  bool JsonInputValueSerializer::operator()(uint32_t& value, Common::StringView name) {
    return getNumber(name, value);
  }
  
  bool JsonInputValueSerializer::operator()(int32_t& value, Common::StringView name) {
    return getNumber(name, value);
  }
  
  bool JsonInputValueSerializer::operator()(int64_t& value, Common::StringView name) {
    return getNumber(name, value);
  }
  
  bool JsonInputValueSerializer::operator()(uint64_t& value, Common::StringView name) {
    return getNumber(name, value);
  }
  
  bool JsonInputValueSerializer::operator()(double& value, Common::StringView name) {
    return getNumber(name, value);
  }
  
  bool JsonInputValueSerializer::operator()(uint8_t& value, Common::StringView name) {
    return getNumber(name, value);
  }
  
  bool JsonInputValueSerializer::operator()(std::string& value, Common::StringView name) {
    auto ptr = getValue(name);
    if (ptr == nullptr) {
      return false;
    }
    value = ptr->getString();
    return true;
  }
  
  bool JsonInputValueSerializer::operator()(bool& value, Common::StringView name) {
    auto ptr = getValue(name);
    if (ptr == nullptr) {
      return false;
    }
    value = ptr->getBool();
    return true;
  }
  
  bool JsonInputValueSerializer::binary(void* value, size_t size, Common::StringView name) {
    auto ptr = getValue(name);
    if (ptr == nullptr) {
      return false;
    }
  
    Common::fromHex(ptr->getString(), value, size);
    return true;
  }
  
  bool JsonInputValueSerializer::binary(std::string& value, Common::StringView name) {
    auto ptr = getValue(name);
    if (ptr == nullptr) {
      return false;
    }
  
    std::string valueHex = ptr->getString();
    value = Common::asString(Common::fromHex(valueHex));
  
    return true;
  }
  
  const JsonValue* JsonInputValueSerializer::getValue(Common::StringView name) {
    const JsonValue& val = *chain.back();
    if (val.isArray()) {
      return &val[idxs.back()++];
    }
  
    std::string strName(name);
    return val.contains(strName) ? &val(strName) : nullptr;
  }
  