@Test void convertDateToString() { final ZonedDateTime dateTime = ZonedDateTime.of(2017, 7, 17, 9, 0, 0, 0, ZoneId.of("GMT")); final String stringValue = dateTime.format(ISO_ZONED_DATE_TIME); new RecordConverters().coerce(ZonedDateTime.class, stringValue, "foo"); final ZonedDateTime asDate = new RecordConverters().coerce(ZonedDateTime.class, stringValue, "foo"); assertEquals(dateTime, asDate); final String asString = new RecordConverters().coerce(String.class, stringValue, "foo"); assertEquals(stringValue, asString); }
public <T> T coerce(final Class<T> expectedType, final Object value, final String name) { if (value == null) { return null; } if (Long.class.isInstance(value) && expectedType != Long.class) { if (expectedType == ZonedDateTime.class) { final long epochMilli = Number.class.cast(value).longValue(); if (epochMilli == -1L) { return null; } return expectedType.cast(ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), UTC)); } if (expectedType == Date.class) { return expectedType.cast(new Date(Number.class.cast(value).longValue())); } } if (!expectedType.isInstance(value)) { if (Number.class.isInstance(value) && Number.class.isAssignableFrom(expectedType)) { return mapNumber(expectedType, Number.class.cast(value)); } if (String.class.isInstance(value)) { if (ZonedDateTime.class == expectedType) { return expectedType.cast(ZonedDateTime.parse(String.valueOf(value))); } if (byte[].class == expectedType) { return expectedType.cast(Base64.getDecoder().decode(String.valueOf(value))); } } throw new IllegalArgumentException(name + " can't be converted to " + expectedType); } return expectedType.cast(value); }
RecordConverters implements Serializable { public <T> T coerce(final Class<T> expectedType, final Object value, final String name) { if (value == null) { return null; } if (Long.class.isInstance(value) && expectedType != Long.class) { if (expectedType == ZonedDateTime.class) { final long epochMilli = Number.class.cast(value).longValue(); if (epochMilli == -1L) { return null; } return expectedType.cast(ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), UTC)); } if (expectedType == Date.class) { return expectedType.cast(new Date(Number.class.cast(value).longValue())); } } if (!expectedType.isInstance(value)) { if (Number.class.isInstance(value) && Number.class.isAssignableFrom(expectedType)) { return mapNumber(expectedType, Number.class.cast(value)); } if (String.class.isInstance(value)) { if (ZonedDateTime.class == expectedType) { return expectedType.cast(ZonedDateTime.parse(String.valueOf(value))); } if (byte[].class == expectedType) { return expectedType.cast(Base64.getDecoder().decode(String.valueOf(value))); } } throw new IllegalArgumentException(name + " can't be converted to " + expectedType); } return expectedType.cast(value); } }
RecordConverters implements Serializable { public <T> T coerce(final Class<T> expectedType, final Object value, final String name) { if (value == null) { return null; } if (Long.class.isInstance(value) && expectedType != Long.class) { if (expectedType == ZonedDateTime.class) { final long epochMilli = Number.class.cast(value).longValue(); if (epochMilli == -1L) { return null; } return expectedType.cast(ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), UTC)); } if (expectedType == Date.class) { return expectedType.cast(new Date(Number.class.cast(value).longValue())); } } if (!expectedType.isInstance(value)) { if (Number.class.isInstance(value) && Number.class.isAssignableFrom(expectedType)) { return mapNumber(expectedType, Number.class.cast(value)); } if (String.class.isInstance(value)) { if (ZonedDateTime.class == expectedType) { return expectedType.cast(ZonedDateTime.parse(String.valueOf(value))); } if (byte[].class == expectedType) { return expectedType.cast(Base64.getDecoder().decode(String.valueOf(value))); } } throw new IllegalArgumentException(name + " can't be converted to " + expectedType); } return expectedType.cast(value); }  }
RecordConverters implements Serializable { public <T> T coerce(final Class<T> expectedType, final Object value, final String name) { if (value == null) { return null; } if (Long.class.isInstance(value) && expectedType != Long.class) { if (expectedType == ZonedDateTime.class) { final long epochMilli = Number.class.cast(value).longValue(); if (epochMilli == -1L) { return null; } return expectedType.cast(ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), UTC)); } if (expectedType == Date.class) { return expectedType.cast(new Date(Number.class.cast(value).longValue())); } } if (!expectedType.isInstance(value)) { if (Number.class.isInstance(value) && Number.class.isAssignableFrom(expectedType)) { return mapNumber(expectedType, Number.class.cast(value)); } if (String.class.isInstance(value)) { if (ZonedDateTime.class == expectedType) { return expectedType.cast(ZonedDateTime.parse(String.valueOf(value))); } if (byte[].class == expectedType) { return expectedType.cast(Base64.getDecoder().decode(String.valueOf(value))); } } throw new IllegalArgumentException(name + " can't be converted to " + expectedType); } return expectedType.cast(value); }  T mapNumber(final Class<T> expected, final Number from); Record toRecord(final MappingMetaRegistry registry, final T data, final Supplier<Jsonb> jsonbProvider,
            final Supplier<RecordBuilderFactory> recordBuilderProvider); static Schema toSchema(final RecordBuilderFactory factory, final Object next); Object toType(final MappingMetaRegistry registry, final Object data, final Class<?> parameterType,
            final Supplier<JsonBuilderFactory> factorySupplier, final Supplier<JsonProvider> providerSupplier,
            final Supplier<Jsonb> jsonbProvider, final Supplier<RecordBuilderFactory> recordBuilderProvider); Object toType(final MappingMetaRegistry registry, final Object data, final Class<?> parameterType,
            final Supplier<JsonBuilderFactory> factorySupplier, final Supplier<JsonProvider> providerSupplier,
            final Supplier<Jsonb> jsonbProvider, final Supplier<RecordBuilderFactory> recordBuilderProvider,
            final java.util.Map<String, String> metadata); T coerce(final Class<T> expectedType, final Object value, final String name); }
RecordConverters implements Serializable { public <T> T coerce(final Class<T> expectedType, final Object value, final String name) { if (value == null) { return null; } if (Long.class.isInstance(value) && expectedType != Long.class) { if (expectedType == ZonedDateTime.class) { final long epochMilli = Number.class.cast(value).longValue(); if (epochMilli == -1L) { return null; } return expectedType.cast(ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), UTC)); } if (expectedType == Date.class) { return expectedType.cast(new Date(Number.class.cast(value).longValue())); } } if (!expectedType.isInstance(value)) { if (Number.class.isInstance(value) && Number.class.isAssignableFrom(expectedType)) { return mapNumber(expectedType, Number.class.cast(value)); } if (String.class.isInstance(value)) { if (ZonedDateTime.class == expectedType) { return expectedType.cast(ZonedDateTime.parse(String.valueOf(value))); } if (byte[].class == expectedType) { return expectedType.cast(Base64.getDecoder().decode(String.valueOf(value))); } } throw new IllegalArgumentException(name + " can't be converted to " + expectedType); } return expectedType.cast(value); }  T mapNumber(final Class<T> expected, final Number from); Record toRecord(final MappingMetaRegistry registry, final T data, final Supplier<Jsonb> jsonbProvider,
            final Supplier<RecordBuilderFactory> recordBuilderProvider); static Schema toSchema(final RecordBuilderFactory factory, final Object next); Object toType(final MappingMetaRegistry registry, final Object data, final Class<?> parameterType,
            final Supplier<JsonBuilderFactory> factorySupplier, final Supplier<JsonProvider> providerSupplier,
            final Supplier<Jsonb> jsonbProvider, final Supplier<RecordBuilderFactory> recordBuilderProvider); Object toType(final MappingMetaRegistry registry, final Object data, final Class<?> parameterType,
            final Supplier<JsonBuilderFactory> factorySupplier, final Supplier<JsonProvider> providerSupplier,
            final Supplier<Jsonb> jsonbProvider, final Supplier<RecordBuilderFactory> recordBuilderProvider,
            final java.util.Map<String, String> metadata); T coerce(final Class<T> expectedType, final Object value, final String name);  }