@Override public JsonCodec<T> get() { return codecFactory.jsonCodec(clazz); }
@Override public JsonCodec<T> get() { return codecFactory.jsonCodec(clazz); }
@Override public JsonCodec<T> get() { return codecFactory.jsonCodec(clazz); }
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)); }
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); }
@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()); } }
@Override public JsonCodec<?> get() { return jsonCodecFactory.jsonCodec(type); }
@Override public JsonCodec<?> get() { return jsonCodecFactory.jsonCodec(type); }
@Test public void testTypeLiteralList() throws Exception { JsonCodec<List<Person>> jsonCodec = jsonCodecFactory.jsonCodec(new TypeToken<List<Person>>() {}); validateListCodec(jsonCodec); }
@Test public void testTypeLiteralMap() throws Exception { JsonCodec<Map<String, Person>> jsonCodec = jsonCodecFactory.jsonCodec(new TypeToken<Map<String, Person>>() {}); validateMapCodec(jsonCodec); }
@Test public void testTypeLiteralMap() throws Exception { JsonCodec<Map<String, Person>> jsonCodec = jsonCodecFactory.jsonCodec(new TypeToken<Map<String, Person>>() {}); validateMapCodec(jsonCodec); }
@Test public void testTypeLiteralList() throws Exception { JsonCodec<List<Person>> jsonCodec = jsonCodecFactory.jsonCodec(new TypeToken<List<Person>>() {}); validateListCodec(jsonCodec); }
@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); }
@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); }
@Test public void testJsonCodec() throws Exception { JsonCodec<Person> jsonCodec = jsonCodecFactory.jsonCodec(Person.class); Person expected = new Person().setName("dain").setRocks(true); String json = jsonCodec.toJson(expected); Person actual = jsonCodec.fromJson(json); assertEquals(actual, expected); }
@Test public void testJsonCodec() throws Exception { JsonCodec<Person> jsonCodec = jsonCodecFactory.jsonCodec(Person.class); Person expected = new Person().setName("dain").setRocks(true); String json = jsonCodec.toJson(expected); Person actual = jsonCodec.fromJson(json); assertEquals(actual, expected); }
@Test public void testJsonCodecFactoryBinding() throws Exception { Injector injector = Guice.createInjector(new JsonModule()); JsonCodecFactory codecFactory = injector.getInstance(JsonCodecFactory.class); Person.validatePersonJsonCodec(codecFactory.jsonCodec(Person.class)); Person.validatePersonListJsonCodec(codecFactory.listJsonCodec(Person.class)); Person.validatePersonMapJsonCodec(codecFactory.mapJsonCodec(String.class, Person.class)); }
@Test public void testJsonCodecFactoryBinding() throws Exception { Injector injector = Guice.createInjector(new JsonModule()); JsonCodecFactory codecFactory = injector.getInstance(JsonCodecFactory.class); Person.validatePersonJsonCodec(codecFactory.jsonCodec(Person.class)); Person.validatePersonListJsonCodec(codecFactory.listJsonCodec(Person.class)); Person.validatePersonMapJsonCodec(codecFactory.mapJsonCodec(String.class, Person.class)); }