Esempio n. 1
0
void JsonVariant::writeTo(JsonWriter &writer) const {
  if (is<const JsonArray &>())
    as<const JsonArray &>().writeTo(writer);
  else if (is<const JsonObject &>())
    as<const JsonObject &>().writeTo(writer);
  else if (is<const char *>())
    writer.writeString(as<const char *>());
  else if (is<const String *>())
    writer.writeString(as<const String *>()->c_str());
  else if (is<long>())
    writer.writeLong(as<long>());
  else if (is<bool>())
    writer.writeBoolean(as<bool>());
  else if (is<double>()) {
    uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS);
    writer.writeDouble(as<double>(), decimals);
  }
}
void JsonVariant::writeTo(JsonWriter &writer) const {
  if (_type == JSON_ARRAY)
    _content.asArray->writeTo(writer);

  else if (_type == JSON_OBJECT)
    _content.asObject->writeTo(writer);

  else if (_type == JSON_STRING)
    writer.writeString(_content.asString);

  else if (_type == JSON_UNPARSED)
    writer.writeRaw(_content.asString);

  else if (_type == JSON_INTEGER)
    writer.writeInteger(_content.asInteger);

  else if (_type == JSON_BOOLEAN)
    writer.writeBoolean(_content.asInteger != 0);

  else if (_type >= JSON_FLOAT_0_DECIMALS) {
    uint8_t decimals = static_cast<uint8_t>(_type - JSON_FLOAT_0_DECIMALS);
    writer.writeFloat(_content.asFloat, decimals);
  }
}
void JsonVariant::writeTo(JsonWriter &writer) const {
  switch (_type) {
    case JSON_UNDEFINED:
      return;

    case JSON_ARRAY:
      _content.asArray->writeTo(writer);
      return;

    case JSON_OBJECT:
      _content.asObject->writeTo(writer);
      return;

    case JSON_STRING:
      writer.writeString(_content.asString);
      return;

    case JSON_UNPARSED:
      writer.writeRaw(_content.asString);
      return;

    case JSON_NEGATIVE_INTEGER:
      writer.writeRaw('-');
    case JSON_POSITIVE_INTEGER:
      writer.writeInteger(_content.asInteger);
      return;

    case JSON_BOOLEAN:
      writer.writeBoolean(_content.asInteger != 0);
      return;

    default:
      uint8_t decimals = static_cast<uint8_t>(_type - JSON_FLOAT_0_DECIMALS);
      writer.writeFloat(_content.asFloat, decimals);
  }
}