Esempio n. 1
0
double TRI_ToDoubleJson (TRI_json_t const* json, bool& failed) {
  TRI_ASSERT(json != nullptr);
  failed = false;
  switch (json->_type) {
    case TRI_JSON_UNUSED:
    case TRI_JSON_NULL:
      return 0.0;
    case TRI_JSON_BOOLEAN:
      return (json->_value._boolean ? 1.0 : 0.0);
    case TRI_JSON_NUMBER:
      return json->_value._number;
    case TRI_JSON_STRING:
    case TRI_JSON_STRING_REFERENCE:
      try {
        // try converting string to number
        double v = std::stod(json->_value._string.data);
        return v;
      }
      catch (...) {
        if (strlen(json->_value._string.data) == 0) {
          return 0.0;
        }
        // conversion failed
      }
      break;
    case TRI_JSON_ARRAY: {
      size_t const n = TRI_LengthArrayJson(json);

      if (n == 0) {
        return 0.0;
      }
      else if (n == 1) {
        return TRI_ToDoubleJson(TRI_LookupArrayJson(json, 0), failed);
      }
      break;
    }
    case TRI_JSON_OBJECT:
      break;
  }

  failed = true;
  // TODO: must convert to null here
  return 0.0;
}
Esempio n. 2
0
double TRI_ToDoubleJson (TRI_json_t* json) {
  switch (json->_type) {
    case TRI_JSON_UNUSED:
    case TRI_JSON_NULL:
      return 0.0;
    case TRI_JSON_BOOLEAN:
      return (json->_value._boolean ? 1.0 : 0.0);
    case TRI_JSON_NUMBER:
      return json->_value._number;
    case TRI_JSON_STRING:
    case TRI_JSON_STRING_REFERENCE:
      try {
        // try converting string to number
        double v = std::stod(json->_value._string.data);
        return v;
      }
      catch (...) {
        // conversion failed
      }
      break;
    case TRI_JSON_LIST: {
      size_t const n = TRI_LengthListJson(json);
      if (n == 0) {
        return 0.0;
      }
      else if (n == 1) {
        return TRI_ToDoubleJson(TRI_LookupListJson(json, 0));
      }
      break;
    }
    case TRI_JSON_ARRAY:
      break;
  }

  // TODO: must convert to null here
  return 0.0;
}