Java Code Examples for io.vertx.core.json.JsonArray#getValue()

The following examples show how to use io.vertx.core.json.JsonArray#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractPostgresInsertReturningTest.java    From vertx-jooq with MIT License 6 votes vote down vote up
protected <R extends UpdatableRecord<R>, T> Function<Object, T> keyMapper(Table<R> table, Configuration configuration){
    return o -> {
        JsonArray j = (JsonArray) o;
        int pkLength = table.getPrimaryKey().getFieldsArray().length;
        if(pkLength == 1){
            return (T)j.getValue(0);
        }
        Object[] values = new Object[j.size()];
        for(int i=0;i<j.size();i++){
            values[i] = j.getValue(i);
        }
        TableField<R, Object>[] fields = (TableField<R, Object>[]) table.getPrimaryKey().getFieldsArray();
        Record result = DSL.using(configuration)
                .newRecord(fields);

        for (int i = 0; i < values.length; i++)
            result.set(fields[i], fields[i].getDataType().convert(values[i]));

        return (T) result;
    };
}
 
Example 2
Source File: AbstractAsyncVertxDAO.java    From vertx-jooq with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected AbstractAsyncVertxDAO(Table<R> table, Class<P> type, QueryExecutor<R, T, FIND_MANY, FIND_ONE, EXECUTE, INSERT_RETURNING> queryExecutor) {
    super(table, type, queryExecutor);
    Arguments.require(isMysql(queryExecutor.configuration()) || isPostgres(queryExecutor.configuration()),"Only Postgres and MySQL supported");
    if(isMysql(queryExecutor.configuration())){
        keyConverter = keyConverter();
    }else{
        keyConverter = o -> {
            JsonArray j = (JsonArray) o;
            int pkLength = getTable().getPrimaryKey().getFieldsArray().length;
            if(pkLength == 1){
                return (T)j.getValue(0);
            }
            Object[] values = new Object[j.size()];
            for(int i=0;i<j.size();i++){
                values[i] = j.getValue(i);
            }
            return compositeKeyRecord(values);
        };
    }
}
 
Example 3
Source File: JDBCStatementHelper.java    From vertx-jdbc-client with Apache License 2.0 6 votes vote down vote up
public void fillStatement(PreparedStatement statement, JsonArray in) throws SQLException {
  if (in == null) {
    in = EMPTY;
  }

  for (int i = 0; i < in.size(); i++) {
    Object value = in.getValue(i);

    if (value != null) {
      if (value instanceof String) {
        statement.setObject(i + 1, optimisticCast((String) value));
      } else {
        statement.setObject(i + 1, value);
      }
    } else {
      statement.setObject(i + 1, null);
    }
  }
}
 
Example 4
Source File: VertxExtensionModule.java    From vertx-lang-groovy with Apache License 2.0 5 votes vote down vote up
public static Object getAt(JsonArray json, int idx) {
  int size = json.size();
  if (idx < 0) {
    idx += size;
    if (idx < 0) {
      throw new ArrayIndexOutOfBoundsException("Invalid array index " + (idx - size));
    }
  }
  if (idx < size) {
    return json.getValue(idx);
  } else {
    return null;
  }
}
 
Example 5
Source File: JsonArrayValueResolver.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolve(Object context, String name) {
  if (context instanceof JsonArray) {
    JsonArray jsonArray = ((JsonArray) context);
    if ("length".equals(name) || "size".equals(name)) {
      return jsonArray.size();
    }
    // NumberFormatException will bubble up and cause a HandlebarsException with line, row info
    Object value = jsonArray.getValue(Integer.valueOf(name));
    if (value != null) {
      return value;
    }
  }
  return UNRESOLVED;
}
 
Example 6
Source File: JDBCStatementHelper.java    From vertx-jdbc-client with Apache License 2.0 4 votes vote down vote up
public void fillStatement(CallableStatement statement, JsonArray in, JsonArray out) throws SQLException {
  if (in == null) {
    in = EMPTY;
  }

  if (out == null) {
    out = EMPTY;
  }

  int max = Math.max(in.size(), out.size());

  for (int i = 0; i < max; i++) {
    Object value = null;
    boolean set = false;

    if (i < in.size()) {
      value = in.getValue(i);
    }

    // found a in value, use it as a input parameter
    if (value != null) {
      if (value instanceof String) {
        statement.setObject(i + 1, optimisticCast((String) value));
      } else {
        statement.setObject(i + 1, value);
      }
      set = true;
    }

    // reset
    value = null;

    if (i < out.size()) {
      value = out.getValue(i);
    }

    // found a out value, use it as a output parameter
    if (value != null) {
      // We're using the int from the enum instead of the enum itself to allow working with Drivers
      // that have not been upgraded to Java8 yet.
      if (value instanceof String) {
        statement.registerOutParameter(i + 1, JDBCType.valueOf((String) value).getVendorTypeNumber());
      } else if (value instanceof Number) {
        // for cases where vendors have special codes (e.g.: Oracle)
        statement.registerOutParameter(i + 1, ((Number) value).intValue());
      }
      set = true;
    }

    if (!set) {
      // assume null input
      statement.setNull(i + 1, Types.NULL);
    }
  }
}
 
Example 7
Source File: PebbleVertxAttributeResolver.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Override
public ResolvedAttribute resolve(Object instance, Object attributeNameValue, Object[] argumentValues,
    ArgumentsNode args, EvaluationContextImpl context, String filename, int lineNumber) {

  if (instance instanceof JsonObject) {
    ResolvedAttribute resolvedAttribute = new ResolvedAttribute(null);

    if (attributeNameValue instanceof String) {
      JsonObject jsonObject = (JsonObject) instance;
      resolvedAttribute = new ResolvedAttribute(jsonObject.getValue((String) attributeNameValue));
    }
    if (context.isStrictVariables() && resolvedAttribute.evaluatedValue == null) {
      throw new AttributeNotFoundException(null,
          String.format(
              "Attribute [%s] of [%s] does not exist or can not be accessed and strict variables is set to true.",
              attributeNameValue.toString(), instance.getClass().getName()),
          attributeNameValue.toString(), lineNumber, filename);
    }

    return resolvedAttribute;
  } else if (instance instanceof JsonArray) {
    JsonArray jsonArray = (JsonArray) instance;
    String attributeName = String.valueOf(attributeNameValue);
    int index;
    try {
      index = Integer.parseInt(attributeName);
    } catch (NumberFormatException e) {
      return null;
    }
    int length = jsonArray.size();

    if (index < 0 || index >= length) {
      if (context.isStrictVariables()) {
        throw new AttributeNotFoundException(null,
            "Index out of bounds while accessing JsonArray with strict variables on.", attributeName, lineNumber,
            filename);
      } else {
        return new ResolvedAttribute(null);
      }
    }

    return new ResolvedAttribute(jsonArray.getValue(index));
  }

  return null;
}