Example #1
0
bool TRI_ValueJavascriptAql (TRI_string_buffer_t* const buffer,
                             const TRI_aql_value_t* const value,
                             const TRI_aql_value_type_e type) {
  switch (type) {
    case TRI_AQL_TYPE_FAIL:
      return (TRI_AppendStringStringBuffer(buffer, "fail") == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_NULL:
      return (TRI_AppendStringStringBuffer(buffer, "null") == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_BOOL:
      return (TRI_AppendStringStringBuffer(buffer, value->_value._bool ? "true" : "false") == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_INT:
      return (TRI_AppendInt64StringBuffer(buffer, value->_value._int) == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_DOUBLE:
      return (TRI_AppendDoubleStringBuffer(buffer, value->_value._double) == TRI_ERROR_NO_ERROR);

    case TRI_AQL_TYPE_STRING: {
      char* escapedString;
      size_t outLength;

      if (TRI_AppendCharStringBuffer(buffer, '"') != TRI_ERROR_NO_ERROR) {
        return false;
      }

      escapedString = TRI_EscapeUtf8StringZ(TRI_UNKNOWN_MEM_ZONE,
                                            value->_value._string,
                                            strlen(value->_value._string),
                                            false,
                                            &outLength);
      if (escapedString == NULL) {
        return false;
      }

      if (TRI_AppendStringStringBuffer(buffer, escapedString) != TRI_ERROR_NO_ERROR) {
        TRI_Free(TRI_UNKNOWN_MEM_ZONE, escapedString);

        return false;
      }

      TRI_Free(TRI_UNKNOWN_MEM_ZONE, escapedString);

      return (TRI_AppendCharStringBuffer(buffer, '"') == TRI_ERROR_NO_ERROR);
    }
  }

  return false;
}
Example #2
0
static int StringifyJson (TRI_memory_zone_t* zone,
                          TRI_string_buffer_t* buffer, 
                          TRI_json_t const* object,
                          bool braces) {
  size_t n;
  size_t i;
  size_t outLength;
  char* ptr;
  int res;

  switch (object->_type) {
    case TRI_JSON_UNUSED:
      break;

    case TRI_JSON_NULL:
      res = TRI_AppendStringStringBuffer(buffer, "null");

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;

    case TRI_JSON_BOOLEAN:
      if (object->_value._boolean) {
        res = TRI_AppendStringStringBuffer(buffer, "true");
      }
      else {
        res = TRI_AppendStringStringBuffer(buffer, "false");
      }

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;

    case TRI_JSON_NUMBER:
      res = TRI_AppendDoubleStringBuffer(buffer, object->_value._number);

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;

    case TRI_JSON_STRING:
      res = TRI_AppendStringStringBuffer(buffer, "\"");

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      ptr = TRI_EscapeUtf8StringZ(zone, 
                                  object->_value._string.data,
                                  object->_value._string.length - 1, 
                                  false,
                                  &outLength);

      if (ptr == NULL) {
        return TRI_ERROR_OUT_OF_MEMORY;
      }

      res = TRI_AppendString2StringBuffer(buffer, ptr, outLength);

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      TRI_Free(zone, ptr);

      res = TRI_AppendStringStringBuffer(buffer, "\"");

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      break;

    case TRI_JSON_ARRAY:
      if (braces) {
        res = TRI_AppendStringStringBuffer(buffer, "{");

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      n = object->_value._objects._length;

      for (i = 0;  i < n;  i += 2) {
        if (0 < i) {
          res = TRI_AppendStringStringBuffer(buffer, ",");

          if (res != TRI_ERROR_NO_ERROR) {
            return res;
          }
        }

        res = StringifyJson(zone, buffer, TRI_AtVector(&object->_value._objects, i), true);

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }

        res = TRI_AppendCharStringBuffer(buffer, ':');

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }

        res = StringifyJson(zone, buffer, TRI_AtVector(&object->_value._objects, i + 1), true);

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      if (braces) {
        res = TRI_AppendStringStringBuffer(buffer, "}");

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      break;

    case TRI_JSON_LIST:
      if (braces) {
        res = TRI_AppendStringStringBuffer(buffer, "[");

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      n = object->_value._objects._length;

      for (i = 0;  i < n;  ++i) {
        if (0 < i) {
          res = TRI_AppendStringStringBuffer(buffer, ",");

          if (res != TRI_ERROR_NO_ERROR) {
            return res;
          }
        }

        res = StringifyJson(zone, buffer, TRI_AtVector(&object->_value._objects, i), true);

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      if (braces) {
        res = TRI_AppendStringStringBuffer(buffer, "]");

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      break;
  }

  return TRI_ERROR_NO_ERROR;
}