private static JsonCodec<ViewDefinition> createTestingViewCodec() { ObjectMapperProvider provider = new ObjectMapperProvider(); provider.setJsonDeserializers(ImmutableMap.of(Type.class, new TypeDeserializer(new TypeRegistry()))); return new JsonCodecFactory(provider).jsonCodec(ViewDefinition.class); }
private static <T> JsonCodec<T> getJsonCodecForType(Type type) { if (VarcharType.VARCHAR.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(String.class); } if (BooleanType.BOOLEAN.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Boolean.class); } if (BigintType.BIGINT.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Long.class); } if (IntegerType.INTEGER.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Integer.class); } if (DoubleType.DOUBLE.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Double.class); } if (type instanceof ArrayType) { Type elementType = ((ArrayType) type).getElementType(); return (JsonCodec<T>) JSON_CODEC_FACTORY.listJsonCodec(getJsonCodecForType(elementType)); } if (type instanceof MapType) { Type keyType = ((MapType) type).getKeyType(); Type valueType = ((MapType) type).getValueType(); return (JsonCodec<T>) JSON_CODEC_FACTORY.mapJsonCodec(getMapKeyType(keyType), getJsonCodecForType(valueType)); } throw new PrestoException(INVALID_SESSION_PROPERTY, format("Session property type %s is not supported", type)); }
@Override public JsonCodec<T> get() { return codecFactory.jsonCodec(clazz); }
public CodecSupplier(Class<T> clazz, Metadata metadata) { this.clazz = requireNonNull(clazz, "clazz is null"); this.metadata = requireNonNull(metadata, "metadata is null"); ObjectMapperProvider objectMapperProvider = new ObjectMapperProvider(); objectMapperProvider.setJsonDeserializers(ImmutableMap.of(Type.class, new TypeDeserializer())); this.codecFactory = new JsonCodecFactory(objectMapperProvider); }
@Test public void testMapJsonCodecFromJsonCodec() throws Exception { JsonCodec<Map<String, Person>> jsonCodec = jsonCodecFactory.mapJsonCodec(String.class, jsonCodecFactory.jsonCodec(Person.class)); validateMapCodec(jsonCodec); }
@Test public void testListJsonCodecFromJsonCodec() throws Exception { JsonCodec<List<Person>> jsonCodec = jsonCodecFactory.listJsonCodec(jsonCodecFactory.jsonCodec(Person.class)); validateListCodec(jsonCodec); }
public <T> JsonCodec<T> jsonCodec(Class<T> type) { requireNonNull(type, "type is null"); return new JsonCodec<>(createObjectMapper(), type); }
@Test public void testMapJsonCodec() throws Exception { JsonCodec<Map<String, Person>> jsonCodec = jsonCodecFactory.mapJsonCodec(String.class, Person.class); validateMapCodec(jsonCodec); }
@Test public void testListJsonCodec() throws Exception { JsonCodec<List<Person>> jsonCodec = jsonCodecFactory.listJsonCodec(Person.class); validateListCodec(jsonCodec); }
@Override public JsonCodec<T> get() { return codecFactory.jsonCodec(clazz); }
public CodecSupplier(Class<T> clazz, Metadata metadata) { this.clazz = clazz; ObjectMapperProvider objectMapperProvider = new ObjectMapperProvider(); objectMapperProvider.setJsonDeserializers(ImmutableMap.of(Type.class, new TypeDeserializer(metadata))); this.codecFactory = new JsonCodecFactory(objectMapperProvider); }
@Test public void testMapJsonCodecFromJsonCodec() throws Exception { JsonCodec<Map<String, Person>> jsonCodec = jsonCodecFactory.mapJsonCodec(String.class, jsonCodecFactory.jsonCodec(Person.class)); validateMapCodec(jsonCodec); }
@Test public void testListJsonCodecFromJsonCodec() throws Exception { JsonCodec<List<Person>> jsonCodec = jsonCodecFactory.listJsonCodec(jsonCodecFactory.jsonCodec(Person.class)); validateListCodec(jsonCodec); }
public <T> JsonCodec<T> jsonCodec(Type type) { Preconditions.checkNotNull(type, "type is null"); return new JsonCodec<>(createObjectMapper(), type); }
@Test public void testMapJsonCodec() throws Exception { JsonCodec<Map<String, Person>> jsonCodec = jsonCodecFactory.mapJsonCodec(String.class, Person.class); validateMapCodec(jsonCodec); }
@Test public void testListJsonCodec() throws Exception { JsonCodec<List<Person>> jsonCodec = jsonCodecFactory.listJsonCodec(Person.class); validateListCodec(jsonCodec); }
@Test public void testSerializationRoundTrip() { ObjectMapperProvider objectMapperProvider = new ObjectMapperProvider(); objectMapperProvider.setJsonDeserializers(ImmutableMap.of(Type.class, new TypeDeserializer(new TypeRegistry()))); JsonCodec<Signature> codec = new JsonCodecFactory(objectMapperProvider, true).jsonCodec(Signature.class); Signature expected = new Signature( "function", SCALAR, parseTypeSignature(StandardTypes.BIGINT), ImmutableList.of(parseTypeSignature(StandardTypes.BOOLEAN), parseTypeSignature(StandardTypes.DOUBLE), parseTypeSignature(StandardTypes.VARCHAR))); String json = codec.toJson(expected); Signature actual = codec.fromJson(json); assertEquals(actual.getName(), expected.getName()); assertEquals(actual.getKind(), expected.getKind()); assertEquals(actual.getReturnType(), expected.getReturnType()); assertEquals(actual.getArgumentTypes(), expected.getArgumentTypes()); } }
private static <T> JsonCodec<T> getJsonCodecForType(Type type) { if (VarcharType.VARCHAR.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(String.class); } if (BooleanType.BOOLEAN.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Boolean.class); } if (BigintType.BIGINT.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Long.class); } if (IntegerType.INTEGER.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Integer.class); } if (DoubleType.DOUBLE.equals(type)) { return (JsonCodec<T>) JSON_CODEC_FACTORY.jsonCodec(Double.class); } if (type instanceof ArrayType) { Type elementType = ((ArrayType) type).getElementType(); return (JsonCodec<T>) JSON_CODEC_FACTORY.listJsonCodec(getJsonCodecForType(elementType)); } if (type instanceof MapType) { Type keyType = ((MapType) type).getKeyType(); Type valueType = ((MapType) type).getValueType(); return (JsonCodec<T>) JSON_CODEC_FACTORY.mapJsonCodec(getMapKeyType(keyType), getJsonCodecForType(valueType)); } throw new PrestoException(INVALID_SESSION_PROPERTY, format("Session property type %s is not supported", type)); }
@Override public JsonCodec<T> get() { return codecFactory.jsonCodec(clazz); }
public CodecSupplier(Class<T> clazz, Metadata metadata) { this.clazz = clazz; this.metadata = metadata; ObjectMapperProvider objectMapperProvider = new ObjectMapperProvider(); objectMapperProvider.setJsonDeserializers(ImmutableMap.of(Type.class, new TypeDeserializer())); this.codecFactory = new JsonCodecFactory(objectMapperProvider); }