/** * Convert the specified value to an {@link Type#STRING} value. * Not supplying a schema may limit the ability to convert to the desired type. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a string, or null if the supplied value was null */ public static String convertToString(Schema schema, Object value) { return (String) convertTo(Schema.OPTIONAL_STRING_SCHEMA, schema, value); }
@Override public byte[] fromConnectHeader(String topic, String headerKey, Schema schema, Object value) { if (value == null) { return null; } return Values.convertToString(schema, value).getBytes(UTF_8); }
} else if (value instanceof String) { if (embedded) { String escaped = escape((String) value); sb.append('"').append(escaped).append('"'); } else { append(sb, bytes, embedded); } else if (value instanceof List) { List<?> list = (List<?>) value; sb.append('['); appendIterable(sb, list.iterator()); sb.append(']'); } else if (value instanceof Map) { Map<?, ?> map = (Map<?, ?>) value; sb.append('{'); appendIterable(sb, map.entrySet().iterator()); sb.append('}'); } else if (value instanceof Struct) { append(sb, field.name(), true); sb.append(':'); append(sb, struct.get(field), true); append(sb, entry.getKey(), true); sb.append(':'); append(sb, entry.getValue(), true); } else if (value instanceof java.util.Date) { java.util.Date dateValue = (java.util.Date) value; String formatted = dateFormatFor(dateValue).format(dateValue);
case STRING: StringBuilder sb = new StringBuilder(); append(sb, value, false); return sb.toString(); case BOOLEAN: SchemaAndValue parsed = parseString(value.toString()); if (parsed.value() instanceof Boolean) { return parsed.value(); return asLong(value, fromSchema, null) == 0L ? Boolean.FALSE : Boolean.TRUE; case INT8: if (value instanceof Byte) { return value; return (byte) asLong(value, fromSchema, null); case INT16: if (value instanceof Short) { return value; return (short) asLong(value, fromSchema, null); case INT32: if (Date.LOGICAL_NAME.equals(toSchema.name())) { if (value instanceof String) { SchemaAndValue parsed = parseString(value.toString()); value = parsed.value(); long numeric = asLong(value, fromSchema, null); return Date.toLogical(toSchema, (int) numeric);
listSchema = SchemaBuilder.array(elementSchema).schema(); result = alignListEntriesWithSchema(listSchema, result); return new SchemaAndValue(listSchema, result); throw new DataException("Unable to parse an empty array element: " + parser.original()); SchemaAndValue element = parse(parser, true); elementSchema = commonSchemaFor(elementSchema, element); result.add(element.value()); parser.canConsume(COMMA_DELIMITER); mapSchema = SchemaBuilder.map(keySchema, valueSchema).schema(); result = alignMapKeysAndValuesWithSchema(mapSchema, result); return new SchemaAndValue(mapSchema, result); SchemaAndValue key = parse(parser, true); if (key == null || key.value() == null) { throw new DataException("Map entry may not have a null key: " + parser.original()); SchemaAndValue value = parse(parser, true); Object entryValue = value != null ? value.value() : null; result.put(key.value(), entryValue); parser.canConsume(COMMA_DELIMITER); keySchema = commonSchemaFor(keySchema, key); valueSchema = commonSchemaFor(valueSchema, value);
private static String castToString(Object value) { if (value instanceof java.util.Date) { java.util.Date dateValue = (java.util.Date) value; return Values.dateFormatFor(dateValue).format(dateValue); } else { return value.toString(); } }
/** * Convert the specified value with the desired floating point type. * * @param value the value to be converted; may not be null * @param schema the schema for the current value type; may not be null * @param error any previous error that should be included in an exception message; may be null * @return the double value after conversion; never null * @throws DataException if the value could not be converted to a double */ protected static double asDouble(Object value, Schema schema, Throwable error) { try { if (value instanceof Number) { Number number = (Number) value; return number.doubleValue(); } if (value instanceof String) { return new BigDecimal(value.toString()).doubleValue(); } } catch (NumberFormatException e) { error = e; // fall through } return asLong(value, schema, error); }
/** * Convert the specified value to an {@link Type#BOOLEAN} value. The supplied schema is required if the value is a logical * type when the schema contains critical information that might be necessary for converting to a boolean. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a boolean, or null if the supplied value was null * @throws DataException if the value could not be converted to a boolean */ public static Boolean convertToBoolean(Schema schema, Object value) throws DataException { return (Boolean) convertTo(Schema.OPTIONAL_BOOLEAN_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#INT32} int value. The supplied schema is required if the value is a logical * type when the schema contains critical information that might be necessary for converting to an integer. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as an integer, or null if the supplied value was null * @throws DataException if the value could not be converted to an integer */ public static Integer convertToInteger(Schema schema, Object value) throws DataException { return (Integer) convertTo(Schema.OPTIONAL_INT32_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#INT64} long value. The supplied schema is required if the value is a logical * type when the schema contains critical information that might be necessary for converting to a long. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a long, or null if the supplied value was null * @throws DataException if the value could not be converted to a long */ public static Long convertToLong(Schema schema, Object value) throws DataException { return (Long) convertTo(Schema.OPTIONAL_INT64_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Time#SCHEMA time} value. * Not supplying a schema may limit the ability to convert to the desired type. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a time, or null if the supplied value was null * @throws DataException if the value cannot be converted to a time value */ public static java.util.Date convertToTime(Schema schema, Object value) { return (java.util.Date) convertTo(Time.SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Date#SCHEMA date} value. * Not supplying a schema may limit the ability to convert to the desired type. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a date, or null if the supplied value was null * @throws DataException if the value cannot be converted to a date value */ public static java.util.Date convertToDate(Schema schema, Object value) { return (java.util.Date) convertTo(Date.SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#INT16} short value. The supplied schema is required if the value is a logical * type when the schema contains critical information that might be necessary for converting to a short. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a short, or null if the supplied value was null * @throws DataException if the value could not be converted to a short */ public static Short convertToShort(Schema schema, Object value) throws DataException { return (Short) convertTo(Schema.OPTIONAL_INT16_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#FLOAT32} float value. The supplied schema is required if the value is a logical * type when the schema contains critical information that might be necessary for converting to a floating point number. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a float, or null if the supplied value was null * @throws DataException if the value could not be converted to a float */ public static Float convertToFloat(Schema schema, Object value) throws DataException { return (Float) convertTo(Schema.OPTIONAL_FLOAT32_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#INT8} byte value. The supplied schema is required if the value is a logical * type when the schema contains critical information that might be necessary for converting to a byte. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a byte, or null if the supplied value was null * @throws DataException if the value could not be converted to a byte */ public static Byte convertToByte(Schema schema, Object value) throws DataException { return (Byte) convertTo(Schema.OPTIONAL_INT8_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#FLOAT64} double value. The supplied schema is required if the value is a logical * type when the schema contains critical information that might be necessary for converting to a floating point number. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a double, or null if the supplied value was null * @throws DataException if the value could not be converted to a double */ public static Double convertToDouble(Schema schema, Object value) throws DataException { return (Double) convertTo(Schema.OPTIONAL_FLOAT64_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Timestamp#SCHEMA timestamp} value. * Not supplying a schema may limit the ability to convert to the desired type. * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a timestamp, or null if the supplied value was null * @throws DataException if the value cannot be converted to a timestamp value */ public static java.util.Date convertToTimestamp(Schema schema, Object value) { return (java.util.Date) convertTo(Timestamp.SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#STRUCT} value. Structs cannot be converted from other types, so this method returns * a struct only if the supplied value is a struct. If not a struct, this method throws an exception. * * <p>This method currently does not use the schema, though it may be used in the future.</p> * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a struct, or null if the supplied value was null * @throws DataException if the value is not a struct */ public static Struct convertToStruct(Schema schema, Object value) { return (Struct) convertTo(STRUCT_SELECTOR_SCHEMA, schema, value); }
/** * Convert the specified value to an {@link Type#MAP} value. If the value is a string representation of a map, this method * will parse the string and its entries to infer the schemas for those entries. Thus, this method supports * maps with primitives and structured keys and values. If the value is already a map, this method simply casts and returns it. * * <p>This method currently does not use the schema, though it may be used in the future.</p> * * @param schema the schema for the value; may be null * @param value the value to be converted; may be null * @return the representation as a map, or null if the supplied value was null * @throws DataException if the value cannot be converted to a map value */ public static Map<?, ?> convertToMap(Schema schema, Object value) { return (Map<?, ?>) convertTo(MAP_SELECTOR_SCHEMA, schema, value); }