@Override public JsonSerializer<?> modifySerializer(SerializationConfig config, BasicBeanDescription beanDesc, JsonSerializer<?> serializer) { if (serializer == null) { return new EmptyBeanSerializer(); } return new TypeInjectingSerializer((BeanSerializerBase) serializer); }
@Override public T decode(InputStream source) throws IOException { return (T) decodeJson(source); }
@Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonNode tree = jp.readValueAsTree(); if (tree instanceof ArrayNode) { return handleArray(jp, (ArrayNode) tree); } return handleObject(jp, tree); }
public EurekaJsonCodec(Set<Class<?>> acceptedTypes) { this.acceptedTypes = acceptedTypes; mapper = new ObjectMapper(); SimpleSerializers serializers = new SimpleSerializers(); serializers.addSerializer(Enum.class, new EnumSerializer()); SerializerFactory serializerFactory = BeanSerializerFactory .instance .withAdditionalSerializers(serializers) .withSerializerModifier(new TypeInjectingModifier()); DeserializerFactory deserializerFactory = BeanDeserializerFactory .instance .withDeserializerModifier(new TypeResolvingModifier(mapper)); mapper.setSerializerFactory(serializerFactory); mapper.setDeserializerProvider(new StdDeserializerProvider(deserializerFactory)); mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(Feature.FAIL_ON_EMPTY_BEANS, false); mapper.configure(Feature.AUTO_DETECT_GETTERS, false); }
@Override public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BasicBeanDescription beanDesc, BeanDeserializerBuilder builder) { Iterator<SettableBeanProperty> beanPropertyIterator = builder.getProperties(); while (beanPropertyIterator.hasNext()) { SettableBeanProperty settableBeanProperty = beanPropertyIterator.next(); Class<?> rawClass = settableBeanProperty.getType().getRawClass(); if (!isBasicType(rawClass)) { SettableBeanProperty newSettableBeanProperty = settableBeanProperty.withValueDeserializer(new PolymorphicDeserializer(rawClass, mapper)); builder.addOrReplaceProperty(newSettableBeanProperty, true); } } return builder; }
public static EurekaCodec<InstanceInfo> getInstanceInfoCodec(CodecType codecType) { switch (codecType) { case Json: return new EurekaJsonCodec<>((Set) Collections.singleton(InstanceInfo.class)); } throw new IllegalArgumentException(codecType + " not supported yet"); } }
private Object handleArray(JsonParser jp, ArrayNode arrayNode) throws IOException { Object[] arrayInstance = (Object[]) Array.newInstance(rawClass.getComponentType(), arrayNode.size()); for (int i = 0; i < arrayInstance.length; i++) { Object value = handleObject(jp, arrayNode.get(i)); arrayInstance[i] = value; } return arrayInstance; } }
private static Enum deserializeEnum(JsonParser jp, JsonNode tree) throws IOException { Class<Enum> enumType = (Class<Enum>) getObjectType(jp, tree); String enumValue = tree.get("value").asText(); for (Enum value : enumType.getEnumConstants()) { if (value.name().equals(enumValue)) { return value; } } throw new JsonMappingException(String.format("Unrecognized enum value for type %s for type %s", enumValue, enumType)); } }
@Override public void serialize(Object bean, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject(); serializeFields(bean, jgen, provider); jgen.writeStringField("_type", bean.getClass().getName()); jgen.writeEndObject(); } }
@Override public AbstractNettyCodec call(CodecType codec) { Map<Byte, AbstractNettyCodec> map = new HashMap<>(); map.put(CodecType.Avro.getVersion(), new EurekaCodecWrapper(new EurekaAvroCodec(REGISTRATION_PROTOCOL_MODEL_SET, REGISTRATION_AVRO_SCHEMA, new SchemaReflectData(REGISTRATION_AVRO_SCHEMA)))); map.put(CodecType.Json.getVersion(), new EurekaCodecWrapper(new EurekaJsonCodec(REGISTRATION_PROTOCOL_MODEL_SET))); return new DynamicNettyCodec(REGISTRATION_PROTOCOL_MODEL_SET, Collections.unmodifiableMap(map), codec.getVersion()); } };
public <C> C decodeContainer(Class<C> containerType, InputStream source) throws IOException { return (C) decodeJson(source); }
@Override public AbstractNettyCodec call(CodecType codec) { Map<Byte, AbstractNettyCodec> map = new HashMap<>(); map.put(CodecType.Avro.getVersion(), new EurekaCodecWrapper(new EurekaAvroCodec(REGISTRATION_PROTOCOL_MODEL_SET, REPLICATION_AVRO_SCHEMA, new SchemaReflectData(REPLICATION_AVRO_SCHEMA)))); map.put(CodecType.Json.getVersion(), new EurekaCodecWrapper(new EurekaJsonCodec(REGISTRATION_PROTOCOL_MODEL_SET))); return new DynamicNettyCodec(REPLICATION_PROTOCOL_MODEL_SET, Collections.unmodifiableMap(map), codec.getVersion()); } };
@Override public AbstractNettyCodec call(CodecType codec) { Map<Byte, AbstractNettyCodec> map = new HashMap<>(); map.put(CodecType.Avro.getVersion(), new EurekaCodecWrapper(new EurekaAvroCodec(REGISTRATION_PROTOCOL_MODEL_SET, INTEREST_AVRO_SCHEMA, new SchemaReflectData(INTEREST_AVRO_SCHEMA)))); map.put(CodecType.Json.getVersion(), new EurekaCodecWrapper(new EurekaJsonCodec(REGISTRATION_PROTOCOL_MODEL_SET))); return new DynamicNettyCodec(INTEREST_PROTOCOL_MODEL_SET, Collections.unmodifiableMap(map), codec.getVersion()); } };