org.apache.kafka.connect.data.Values Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kafka.connect.data;
import org.apache.kafka.common.utils.Utils;
import org.apache.kafka.connect.data.Schema.Type;
import org.apache.kafka.connect.errors.DataException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.text.CharacterIterator;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TimeZone;
import java.util.regex.Pattern;
/**
* Utility for converting from one Connect value to a different form. This is useful when the caller expects a value of a particular type
* but is uncertain whether the actual value is one that isn't directly that type but can be converted into that type.
*
* For example, a caller might expects a particular {@link org.apache.kafka.connect.header.Header} to contain an {@link Type#INT64}
* value, when in fact that header contains a string representation of a 32-bit integer. Here, the caller can use the methods in this
* class to convert the value to the desired type:
*
* Header header = ...
* long value = Values.convertToLong(header.schema(), header.value());
*
*
* This class is able to convert any value to a string representation as well as parse those string representations back into most of
* the types. The only exception is {@link Struct} values that require a schema and thus cannot be parsed from a simple string.
*/
public class Values {
private static final Logger LOG = LoggerFactory.getLogger(Values.class);
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
private static final SchemaAndValue NULL_SCHEMA_AND_VALUE = new SchemaAndValue(null, null);
private static final SchemaAndValue TRUE_SCHEMA_AND_VALUE = new SchemaAndValue(Schema.BOOLEAN_SCHEMA, Boolean.TRUE);
private static final SchemaAndValue FALSE_SCHEMA_AND_VALUE = new SchemaAndValue(Schema.BOOLEAN_SCHEMA, Boolean.FALSE);
private static final Schema ARRAY_SELECTOR_SCHEMA = SchemaBuilder.array(Schema.STRING_SCHEMA).build();
private static final Schema MAP_SELECTOR_SCHEMA = SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.STRING_SCHEMA).build();
private static final Schema STRUCT_SELECTOR_SCHEMA = SchemaBuilder.struct().build();
private static final String TRUE_LITERAL = Boolean.TRUE.toString();
private static final String FALSE_LITERAL = Boolean.FALSE.toString();
private static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
private static final String NULL_VALUE = "null";
static final String ISO_8601_DATE_FORMAT_PATTERN = "yyyy-MM-dd";
static final String ISO_8601_TIME_FORMAT_PATTERN = "HH:mm:ss.SSS'Z'";
static final String ISO_8601_TIMESTAMP_FORMAT_PATTERN = ISO_8601_DATE_FORMAT_PATTERN + "'T'" + ISO_8601_TIME_FORMAT_PATTERN;
private static final Set TEMPORAL_LOGICAL_TYPE_NAMES =
Collections.unmodifiableSet(
new HashSet<>(
Arrays.asList(Time.LOGICAL_NAME,
Timestamp.LOGICAL_NAME,
Date.LOGICAL_NAME
)
)
);
private static final String QUOTE_DELIMITER = "\"";
private static final String COMMA_DELIMITER = ",";
private static final String ENTRY_DELIMITER = ":";
private static final String ARRAY_BEGIN_DELIMITER = "[";
private static final String ARRAY_END_DELIMITER = "]";
private static final String MAP_BEGIN_DELIMITER = "{";
private static final String MAP_END_DELIMITER = "}";
private static final int ISO_8601_DATE_LENGTH = ISO_8601_DATE_FORMAT_PATTERN.length();
private static final int ISO_8601_TIME_LENGTH = ISO_8601_TIME_FORMAT_PATTERN.length() - 2; // subtract single quotes
private static final int ISO_8601_TIMESTAMP_LENGTH = ISO_8601_TIMESTAMP_FORMAT_PATTERN.length() - 4; // subtract single quotes
private static final Pattern TWO_BACKSLASHES = Pattern.compile("\\\\");
private static final Pattern DOUBLEQOUTE = Pattern.compile("\"");
/**
* 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#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#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#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 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#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 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);
}
/**
* Convert the specified value to an {@link Type#ARRAY} value. If the value is a string representation of an array, this method
* will parse the string and its elements to infer the schemas for those elements. Thus, this method supports
* arrays of other primitives and structured types. If the value is already an array (or list), this method simply casts and
* returns it.
*
* This method currently does not use the schema, though it may be used in the future.
*
* @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 list, or null if the supplied value was null
* @throws DataException if the value cannot be converted to a list value
*/
public static List> convertToList(Schema schema, Object value) {
return (List>) convertTo(ARRAY_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.
*
* This method currently does not use the schema, though it may be used in the future.
*
* @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);
}
/**
* 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.
*
* This method currently does not use the schema, though it may be used in the future.
*
* @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 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 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 Decimal decimal} 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 decimal, or null if the supplied value was null
* @throws DataException if the value cannot be converted to a decimal value
*/
public static BigDecimal convertToDecimal(Schema schema, Object value, int scale) {
return (BigDecimal) convertTo(Decimal.schema(scale), schema, value);
}
/**
* If possible infer a schema for the given value.
*
* @param value the value whose schema is to be inferred; may be null
* @return the inferred schema, or null if the value is null or no schema could be inferred
*/
public static Schema inferSchema(Object value) {
if (value instanceof String) {
return Schema.STRING_SCHEMA;
}
if (value instanceof Boolean) {
return Schema.BOOLEAN_SCHEMA;
}
if (value instanceof Byte) {
return Schema.INT8_SCHEMA;
}
if (value instanceof Short) {
return Schema.INT16_SCHEMA;
}
if (value instanceof Integer) {
return Schema.INT32_SCHEMA;
}
if (value instanceof Long) {
return Schema.INT64_SCHEMA;
}
if (value instanceof Float) {
return Schema.FLOAT32_SCHEMA;
}
if (value instanceof Double) {
return Schema.FLOAT64_SCHEMA;
}
if (value instanceof byte[] || value instanceof ByteBuffer) {
return Schema.BYTES_SCHEMA;
}
if (value instanceof List) {
List> list = (List>) value;
if (list.isEmpty()) {
return null;
}
SchemaDetector detector = new SchemaDetector();
for (Object element : list) {
if (!detector.canDetect(element)) {
return null;
}
}
return SchemaBuilder.array(detector.schema()).build();
}
if (value instanceof Map) {
Map, ?> map = (Map, ?>) value;
if (map.isEmpty()) {
return null;
}
SchemaDetector keyDetector = new SchemaDetector();
SchemaDetector valueDetector = new SchemaDetector();
for (Map.Entry, ?> entry : map.entrySet()) {
if (!keyDetector.canDetect(entry.getKey()) || !valueDetector.canDetect(entry.getValue())) {
return null;
}
}
return SchemaBuilder.map(keyDetector.schema(), valueDetector.schema()).build();
}
if (value instanceof Struct) {
return ((Struct) value).schema();
}
return null;
}
/**
* Parse the specified string representation of a value into its schema and value.
*
* @param value the string form of the value
* @return the schema and value; never null, but whose schema and value may be null
* @see #convertToString
*/
public static SchemaAndValue parseString(String value) {
if (value == null) {
return NULL_SCHEMA_AND_VALUE;
}
if (value.isEmpty()) {
return new SchemaAndValue(Schema.STRING_SCHEMA, value);
}
Parser parser = new Parser(value);
return parse(parser, false);
}
/**
* Convert the value to the desired type.
*
* @param toSchema the schema for the desired type; may not be null
* @param fromSchema the schema for the supplied value; may be null if not known
* @return the converted value; never null
* @throws DataException if the value could not be converted to the desired type
*/
protected static Object convertTo(Schema toSchema, Schema fromSchema, Object value) throws DataException {
if (value == null) {
if (toSchema.isOptional()) {
return null;
}
throw new DataException("Unable to convert a null value to a schema that requires a value");
}
switch (toSchema.type()) {
case BYTES:
if (Decimal.LOGICAL_NAME.equals(toSchema.name())) {
if (value instanceof ByteBuffer) {
value = Utils.toArray((ByteBuffer) value);
}
if (value instanceof byte[]) {
return Decimal.toLogical(toSchema, (byte[]) value);
}
if (value instanceof BigDecimal) {
return value;
}
if (value instanceof Number) {
// Not already a decimal, so treat it as a double ...
double converted = ((Number) value).doubleValue();
return BigDecimal.valueOf(converted);
}
if (value instanceof String) {
return new BigDecimal(value.toString()).doubleValue();
}
}
if (value instanceof ByteBuffer) {
return Utils.toArray((ByteBuffer) value);
}
if (value instanceof byte[]) {
return value;
}
if (value instanceof BigDecimal) {
return Decimal.fromLogical(toSchema, (BigDecimal) value);
}
break;
case STRING:
StringBuilder sb = new StringBuilder();
append(sb, value, false);
return sb.toString();
case BOOLEAN:
if (value instanceof Boolean) {
return value;
}
if (value instanceof String) {
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();
}
if (value instanceof java.util.Date) {
if (fromSchema != null) {
String fromSchemaName = fromSchema.name();
if (Date.LOGICAL_NAME.equals(fromSchemaName)) {
return value;
}
if (Timestamp.LOGICAL_NAME.equals(fromSchemaName)) {
// Just get the number of days from this timestamp
long millis = ((java.util.Date) value).getTime();
int days = (int) (millis / MILLIS_PER_DAY); // truncates
return Date.toLogical(toSchema, days);
}
} else {
// There is no fromSchema, so no conversion is needed
return value;
}
}
long numeric = asLong(value, fromSchema, null);
return Date.toLogical(toSchema, (int) numeric);
}
if (Time.LOGICAL_NAME.equals(toSchema.name())) {
if (value instanceof String) {
SchemaAndValue parsed = parseString(value.toString());
value = parsed.value();
}
if (value instanceof java.util.Date) {
if (fromSchema != null) {
String fromSchemaName = fromSchema.name();
if (Time.LOGICAL_NAME.equals(fromSchemaName)) {
return value;
}
if (Timestamp.LOGICAL_NAME.equals(fromSchemaName)) {
// Just get the time portion of this timestamp
Calendar calendar = Calendar.getInstance(UTC);
calendar.setTime((java.util.Date) value);
calendar.set(Calendar.YEAR, 1970);
calendar.set(Calendar.MONTH, 0); // Months are zero-based
calendar.set(Calendar.DAY_OF_MONTH, 1);
return Time.toLogical(toSchema, (int) calendar.getTimeInMillis());
}
} else {
// There is no fromSchema, so no conversion is needed
return value;
}
}
long numeric = asLong(value, fromSchema, null);
return Time.toLogical(toSchema, (int) numeric);
}
if (value instanceof Integer) {
return value;
}
return (int) asLong(value, fromSchema, null);
case INT64:
if (Timestamp.LOGICAL_NAME.equals(toSchema.name())) {
if (value instanceof String) {
SchemaAndValue parsed = parseString(value.toString());
value = parsed.value();
}
if (value instanceof java.util.Date) {
java.util.Date date = (java.util.Date) value;
if (fromSchema != null) {
String fromSchemaName = fromSchema.name();
if (Date.LOGICAL_NAME.equals(fromSchemaName)) {
int days = Date.fromLogical(fromSchema, date);
long millis = days * MILLIS_PER_DAY;
return Timestamp.toLogical(toSchema, millis);
}
if (Time.LOGICAL_NAME.equals(fromSchemaName)) {
long millis = Time.fromLogical(fromSchema, date);
return Timestamp.toLogical(toSchema, millis);
}
if (Timestamp.LOGICAL_NAME.equals(fromSchemaName)) {
return value;
}
} else {
// There is no fromSchema, so no conversion is needed
return value;
}
}
long numeric = asLong(value, fromSchema, null);
return Timestamp.toLogical(toSchema, numeric);
}
if (value instanceof Long) {
return value;
}
return asLong(value, fromSchema, null);
case FLOAT32:
if (value instanceof Float) {
return value;
}
return (float) asDouble(value, fromSchema, null);
case FLOAT64:
if (value instanceof Double) {
return value;
}
return asDouble(value, fromSchema, null);
case ARRAY:
if (value instanceof String) {
SchemaAndValue schemaAndValue = parseString(value.toString());
value = schemaAndValue.value();
}
if (value instanceof List) {
return value;
}
break;
case MAP:
if (value instanceof String) {
SchemaAndValue schemaAndValue = parseString(value.toString());
value = schemaAndValue.value();
}
if (value instanceof Map) {
return value;
}
break;
case STRUCT:
if (value instanceof Struct) {
Struct struct = (Struct) value;
return struct;
}
}
throw new DataException("Unable to convert " + value + " (" + value.getClass() + ") to " + toSchema);
}
/**
* Convert the specified value to the desired scalar value type.
*
* @param value the value to be converted; may not be null
* @param fromSchema 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 long value after conversion; never null
* @throws DataException if the value could not be converted to a long
*/
protected static long asLong(Object value, Schema fromSchema, Throwable error) {
try {
if (value instanceof Number) {
Number number = (Number) value;
return number.longValue();
}
if (value instanceof String) {
return new BigDecimal(value.toString()).longValue();
}
} catch (NumberFormatException e) {
error = e;
// fall through
}
if (fromSchema != null) {
String schemaName = fromSchema.name();
if (value instanceof java.util.Date) {
if (Date.LOGICAL_NAME.equals(schemaName)) {
return Date.fromLogical(fromSchema, (java.util.Date) value);
}
if (Time.LOGICAL_NAME.equals(schemaName)) {
return Time.fromLogical(fromSchema, (java.util.Date) value);
}
if (Timestamp.LOGICAL_NAME.equals(schemaName)) {
return Timestamp.fromLogical(fromSchema, (java.util.Date) value);
}
}
throw new DataException("Unable to convert " + value + " (" + value.getClass() + ") to " + fromSchema, error);
}
throw new DataException("Unable to convert " + value + " (" + value.getClass() + ") to a number", error);
}
/**
* 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);
}
protected static void append(StringBuilder sb, Object value, boolean embedded) {
if (value == null) {
sb.append(NULL_VALUE);
} else if (value instanceof Number) {
sb.append(value);
} else if (value instanceof Boolean) {
sb.append(value);
} else if (value instanceof String) {
if (embedded) {
String escaped = escape((String) value);
sb.append('"').append(escaped).append('"');
} else {
sb.append(value);
}
} else if (value instanceof byte[]) {
value = Base64.getEncoder().encodeToString((byte[]) value);
if (embedded) {
sb.append('"').append(value).append('"');
} else {
sb.append(value);
}
} else if (value instanceof ByteBuffer) {
byte[] bytes = Utils.readBytes((ByteBuffer) value);
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) {
Struct struct = (Struct) value;
Schema schema = struct.schema();
boolean first = true;
sb.append('{');
for (Field field : schema.fields()) {
if (first) {
first = false;
} else {
sb.append(',');
}
append(sb, field.name(), true);
sb.append(':');
append(sb, struct.get(field), true);
}
sb.append('}');
} else if (value instanceof Map.Entry) {
Map.Entry, ?> entry = (Map.Entry, ?>) value;
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);
sb.append(formatted);
} else {
throw new DataException("Failed to serialize unexpected value type " + value.getClass().getName() + ": " + value);
}
}
protected static void appendIterable(StringBuilder sb, Iterator> iter) {
if (iter.hasNext()) {
append(sb, iter.next(), true);
while (iter.hasNext()) {
sb.append(',');
append(sb, iter.next(), true);
}
}
}
protected static String escape(String value) {
String replace1 = TWO_BACKSLASHES.matcher(value).replaceAll("\\\\\\\\");
return DOUBLEQOUTE.matcher(replace1).replaceAll("\\\\\"");
}
public static DateFormat dateFormatFor(java.util.Date value) {
if (value.getTime() < MILLIS_PER_DAY) {
return new SimpleDateFormat(ISO_8601_TIME_FORMAT_PATTERN);
}
if (value.getTime() % MILLIS_PER_DAY == 0) {
return new SimpleDateFormat(ISO_8601_DATE_FORMAT_PATTERN);
}
return new SimpleDateFormat(ISO_8601_TIMESTAMP_FORMAT_PATTERN);
}
protected static boolean canParseSingleTokenLiteral(Parser parser, boolean embedded, String tokenLiteral) {
int startPosition = parser.mark();
// If the next token is what we expect, then either...
if (parser.canConsume(tokenLiteral)) {
// ...we're reading an embedded value, in which case the next token will be handled appropriately
// by the caller if it's something like an end delimiter for a map or array, or a comma to
// separate multiple embedded values...
// ...or it's being parsed as part of a top-level string, in which case, any other tokens should
// cause use to stop parsing this single-token literal as such and instead just treat it like
// a string. For example, the top-level string "true}" will be tokenized as the tokens "true" and
// "}", but should ultimately be parsed as just the string "true}" instead of the boolean true.
if (embedded || !parser.hasNext()) {
return true;
}
}
parser.rewindTo(startPosition);
return false;
}
protected static SchemaAndValue parse(Parser parser, boolean embedded) throws NoSuchElementException {
if (!parser.hasNext()) {
return null;
}
if (embedded) {
if (parser.canConsume(QUOTE_DELIMITER)) {
StringBuilder sb = new StringBuilder();
while (parser.hasNext()) {
if (parser.canConsume(QUOTE_DELIMITER)) {
break;
}
sb.append(parser.next());
}
String content = sb.toString();
// We can parse string literals as temporal logical types, but all others
// are treated as strings
SchemaAndValue parsed = parseString(content);
if (parsed != null && TEMPORAL_LOGICAL_TYPE_NAMES.contains(parsed.schema().name())) {
return parsed;
}
return new SchemaAndValue(Schema.STRING_SCHEMA, content);
}
}
if (canParseSingleTokenLiteral(parser, embedded, NULL_VALUE)) {
return null;
}
if (canParseSingleTokenLiteral(parser, embedded, TRUE_LITERAL)) {
return TRUE_SCHEMA_AND_VALUE;
}
if (canParseSingleTokenLiteral(parser, embedded, FALSE_LITERAL)) {
return FALSE_SCHEMA_AND_VALUE;
}
int startPosition = parser.mark();
try {
if (parser.canConsume(ARRAY_BEGIN_DELIMITER)) {
List