Ejemplo n.º 1
0
size_t DocumentAccessor::length () const {
  if (! isArray()) {
    return 0;
  }
  // ok, we have confirmed this is an array
  TRI_ASSERT(_current != nullptr);
  if (_current != nullptr) {
    return TRI_LengthArrayJson(_current);
  }
  return 0;
}
Ejemplo n.º 2
0
bool TRI_DeleteArrayJson (TRI_memory_zone_t* zone, TRI_json_t* array, size_t index) {
  TRI_ASSERT(TRI_IsArrayJson(array));

  size_t const n = TRI_LengthArrayJson(array);

  if (index >= n) {
    return false;
  }
  
  TRI_json_t* element = static_cast<TRI_json_t*>(TRI_AtVector(&array->_value._objects, index));
  TRI_ASSERT(element != nullptr);
  TRI_DestroyJson(zone, element);
  TRI_RemoveVector(&array->_value._objects, index);

  return true;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
int64_t TRI_ToInt64Json (TRI_json_t const* json) {
  TRI_ASSERT(json != nullptr);
  switch (json->_type) {
    case TRI_JSON_UNUSED:
    case TRI_JSON_NULL:
      return 0;
    case TRI_JSON_BOOLEAN:
      return (json->_value._boolean ? 1 : 0);
    case TRI_JSON_NUMBER:
      return static_cast<int64_t>(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 static_cast<int64_t>(v);
      }
      catch (...) {
        // conversion failed
      }
      break;
    case TRI_JSON_ARRAY: {
      size_t const n = TRI_LengthArrayJson(json);

      if (n == 0) {
        return 0;
      }
      else if (n == 1) {
        return TRI_ToInt64Json(TRI_LookupArrayJson(json, 0));
      }
      break;
    }
    case TRI_JSON_OBJECT:
      break;
  }
  
  // TODO: must convert to null here
  return 0;
}
Ejemplo n.º 5
0
DocumentAccessor& DocumentAccessor::at (int64_t index) {
  if (isArray()) {
    size_t length = TRI_LengthArrayJson(_current);

    if (index < 0) {
      // a negative position is allowed
      index = static_cast<int64_t>(length) + index; 
    }

    if (index >= 0 && index < static_cast<int64_t>(length)) {
      // only look up the value if it is within array bounds
      TRI_json_t const* found = TRI_LookupArrayJson(_current, static_cast<size_t>(index));

      if (found != nullptr) {
        _current = found;
        return *this;
      }
    }
    // fall-through intentional
  }

  setToNull();
  return *this;
}