Example #1
0
  public FieldSerializer(Class<?> beanType, FieldInfo fieldInfo) {
    this.fieldInfo = fieldInfo;
    this.fieldContext = new BeanContext(beanType, fieldInfo);

    fieldInfo.setAccessible();

    this.double_quoted_fieldPrefix = '"' + fieldInfo.name + "\":";

    boolean writeNull = false;
    JSONField annotation = fieldInfo.getAnnotation();
    if (annotation != null) {
      for (SerializerFeature feature : annotation.serialzeFeatures()) {
        if ((feature.getMask() & SerializerFeature.WRITE_MAP_NULL_FEATURES) != 0) {
          writeNull = true;
          break;
        }
      }

      format = annotation.format();

      if (format.trim().length() == 0) {
        format = null;
      }

      for (SerializerFeature feature : annotation.serialzeFeatures()) {
        if (feature == SerializerFeature.WriteEnumUsingToString) {
          writeEnumUsingToString = true;
        } else if (feature == SerializerFeature.WriteEnumUsingName) {
          writeEnumUsingName = true;
        }
      }

      features = SerializerFeature.of(annotation.serialzeFeatures());
    }

    this.writeNull = writeNull;
  }
Example #2
0
  public void writeValue(JSONSerializer serializer, Object propertyValue) throws Exception {
    if (runtimeInfo == null) {

      Class<?> runtimeFieldClass;
      if (propertyValue == null) {
        runtimeFieldClass = this.fieldInfo.fieldClass;
      } else {
        runtimeFieldClass = propertyValue.getClass();
      }

      ObjectSerializer fieldSerializer;
      JSONField fieldAnnotation = fieldInfo.getAnnotation();
      if (fieldAnnotation != null && fieldAnnotation.serializeUsing() != Void.class) {
        fieldSerializer = (ObjectSerializer) fieldAnnotation.serializeUsing().newInstance();
      } else {
        fieldSerializer = serializer.getObjectWriter(runtimeFieldClass);
      }

      runtimeInfo = new RuntimeSerializerInfo(fieldSerializer, runtimeFieldClass);
    }

    final RuntimeSerializerInfo runtimeInfo = this.runtimeInfo;

    final int fieldFeatures = fieldInfo.serialzeFeatures;

    if (propertyValue == null) {
      Class<?> runtimeFieldClass = runtimeInfo.runtimeFieldClass;
      SerializeWriter out = serializer.out;
      if (Number.class.isAssignableFrom(runtimeFieldClass)) {
        out.writeNull(features, SerializerFeature.WriteNullNumberAsZero.mask);
        return;
      } else if (String.class == runtimeFieldClass) {
        out.writeNull(features, SerializerFeature.WriteNullStringAsEmpty.mask);
        return;
      } else if (Boolean.class == runtimeFieldClass) {
        out.writeNull(features, SerializerFeature.WriteNullBooleanAsFalse.mask);
        return;
      } else if (Collection.class.isAssignableFrom(runtimeFieldClass)) {
        out.writeNull(features, SerializerFeature.WriteNullListAsEmpty.mask);
        return;
      }

      ObjectSerializer fieldSerializer = runtimeInfo.fieldSerializer;

      if ((out.isEnabled(SerializerFeature.WRITE_MAP_NULL_FEATURES))
          && fieldSerializer instanceof JavaBeanSerializer) {
        out.writeNull();
        return;
      }

      fieldSerializer.write(serializer, null, fieldInfo.name, fieldInfo.fieldType, fieldFeatures);
      return;
    }

    if (fieldInfo.isEnum) {
      if (writeEnumUsingName) {
        serializer.out.writeString(((Enum<?>) propertyValue).name());
        return;
      }

      if (writeEnumUsingToString) {
        serializer.out.writeString(((Enum<?>) propertyValue).toString());
        return;
      }
    }

    Class<?> valueClass = propertyValue.getClass();
    ObjectSerializer valueSerializer;
    if (valueClass == runtimeInfo.runtimeFieldClass) {
      valueSerializer = runtimeInfo.fieldSerializer;
    } else {
      valueSerializer = serializer.getObjectWriter(valueClass);
    }

    if (format != null) {
      if (valueSerializer instanceof ContextObjectSerializer) {
        ((ContextObjectSerializer) valueSerializer)
            .write(serializer, propertyValue, this.fieldContext);
      } else {
        serializer.writeWithFormat(propertyValue, format);
      }
    } else {
      valueSerializer.write(
          serializer, propertyValue, fieldInfo.name, fieldInfo.fieldType, fieldFeatures);
    }
  }