Exemplo n.º 1
0
  public JsonSerializer<?> modifyCollectionSerializer(
      SerializationConfig config,
      CollectionType valueType,
      BeanDescription beanDesc,
      JsonSerializer<?> serializer) {

    Class<?> klass = beanDesc.getBeanClass();

    if (ClassUtil.isExtendedBy(klass, Set.class)) {
      return new SetSerializer(serializer);
    } else {
      return new ListSerializer(serializer);
    }
  }
  protected void addObjectIdReader(
      DeserializationContext ctxt, BeanDescription beanDesc, BeanDeserializerBuilder builder)
      throws JsonMappingException {
    ObjectIdInfo objectIdInfo = beanDesc.getObjectIdInfo();
    if (objectIdInfo == null) {
      return;
    }
    Class<?> implClass = objectIdInfo.getGeneratorType();
    JavaType idType;
    SettableBeanProperty idProp;
    ObjectIdGenerator<?> gen;

    // Just one special case: Property-based generator is trickier
    if (implClass
        == ObjectIdGenerators.PropertyGenerator.class) { // most special one, needs extra work
      String propName = objectIdInfo.getPropertyName();
      idProp = builder.findProperty(propName);
      if (idProp == null) {
        throw new IllegalArgumentException(
            "Invalid Object Id definition for "
                + beanDesc.getBeanClass().getName()
                + ": can not find property with name '"
                + propName
                + "'");
      }
      idType = idProp.getType();
      gen = new PropertyBasedObjectIdGenerator(objectIdInfo.getScope());
    } else {
      JavaType type = ctxt.constructType(implClass);
      idType = ctxt.getTypeFactory().findTypeParameters(type, ObjectIdGenerator.class)[0];
      idProp = null;
      gen = ctxt.objectIdGeneratorInstance(beanDesc.getClassInfo(), objectIdInfo);
    }
    // also: unlike with value deserializers, let's just resolve one we need here
    JsonDeserializer<?> deser = ctxt.findRootValueDeserializer(idType);
    builder.setObjectIdReader(
        ObjectIdReader.construct(idType, objectIdInfo.getPropertyName(), gen, deser, idProp));
  }
Exemplo n.º 3
0
  @Override
  public JsonSerializer<?> modifySerializer(
      SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {

    Class<?> klass = beanDesc.getBeanClass();

    if (klass == Boolean.class) {
      return new BooleanSerializer(serializer);
    } else if (klass == Integer.class) {
      return new IntegerSerializer(serializer);
    } else if (klass == Long.class) {
      return new LongSerializer(serializer);
    } else if (klass == Float.class) {
      return new FloatSerializer(serializer);
    } else if (klass == Double.class) {
      return new DoubleSerializer(serializer);
    } else if (klass == BigDecimal.class) {
      return new BigDecimalSerializer(serializer);
    } else if (ClassUtil.isExtendedBy(klass, Date.class)) {
      return new DateSerializer(serializer);
    }

    return serializer;
  }