Пример #1
0
  @Override
  public void writeValue(JSONSerializer serializer, Object propertyValue) throws Exception {
    if (format != null) {
      serializer.writeWithFormat(propertyValue, format);
      return;
    }

    if (fieldSerializer == null) {

      if (propertyValue == null) {
        runtimeFieldClass = this.fieldInfo.getFieldClass();
      } else {
        runtimeFieldClass = propertyValue.getClass();
      }

      fieldSerializer = serializer.getObjectWriter(runtimeFieldClass);
    }

    if (propertyValue == null) {
      if (writeNumberAsZero && Number.class.isAssignableFrom(runtimeFieldClass)) {
        serializer.getWriter().write('0');
        return;
      } else if (writeNullStringAsEmpty && String.class == runtimeFieldClass) {
        serializer.getWriter().write("\"\"");
        return;
      } else if (writeNullBooleanAsFalse && Boolean.class == runtimeFieldClass) {
        serializer.getWriter().write("false");
        return;
      } else if (writeNullListAsEmpty && Collection.class.isAssignableFrom(runtimeFieldClass)) {
        serializer.getWriter().write("[]");
        return;
      }

      fieldSerializer.write(serializer, null, fieldInfo.getName(), null);
      return;
    }

    if (writeEnumUsingToString == true && runtimeFieldClass.isEnum()) {
      serializer.getWriter().writeString(((Enum<?>) propertyValue).name());
      return;
    }

    Class<?> valueClass = propertyValue.getClass();
    if (valueClass == runtimeFieldClass) {
      fieldSerializer.write(
          serializer, propertyValue, fieldInfo.getName(), fieldInfo.getFieldType());
      return;
    }

    ObjectSerializer valueSerializer = serializer.getObjectWriter(valueClass);
    valueSerializer.write(serializer, propertyValue, fieldInfo.getName(), fieldInfo.getFieldType());
  }
  public void write(JSONSerializer serializer, Object object) throws IOException {
    SerializeWriter out = serializer.getWrier();
    DataVO vo = (DataVO) object;
    if (vo == null) {
      out.append("null");
      return;
    }
    try {
      String[] fields = vo.getFieldNames();
      if (fields.length == 0) {
        out.append("{}");
        return;
      }

      out.append('{');

      boolean commaFlag = false;
      for (int i = 0; i < fields.length; ++i) {

        Object propertyValue = vo.getValue(fields[i]);

        if (propertyValue == null && (!serializer.isEnabled(SerializerFeature.WriteMapNullValue))) {
          continue;
        }

        List<PropertyFilter> propertyFilters = serializer.getPropertyFiltersDirect();
        if (propertyFilters != null) {
          boolean apply = true;
          for (PropertyFilter propertyFilter : propertyFilters) {
            if (!propertyFilter.apply(object, fields[i], propertyValue)) {
              apply = false;
              break;
            }
          }

          if (!apply) {
            continue;
          }
        }

        if (commaFlag) {
          out.append(',');
        }

        commaFlag = true;
        if (propertyValue == null) {
          out.write(fields[i] + " : null ");
          continue;
        }
        ObjectSerializer objserializer = serializer.getObjectWriter(propertyValue.getClass());
        out.write("\"".concat(fields[i]).concat("\":"));
        objserializer.write(serializer, propertyValue);
      }

      out.append('}');
    } catch (Exception e) {
      throw new JSONException("write javaBean error", e);
    }
  }
Пример #3
0
  public void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

    if (object == null) {
      if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
        out.write("[]");
      } else {
        out.writeNull();
      }
      return;
    }

    Type elementType = null;
    if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
      if (fieldType instanceof ParameterizedType) {
        ParameterizedType param = (ParameterizedType) fieldType;
        elementType = param.getActualTypeArguments()[0];
      }
    }

    Enumeration<?> e = (Enumeration<?>) object;

    SerialContext context = serializer.getContext();
    serializer.setContext(context, object, fieldName, 0);

    try {
      int i = 0;
      out.append('[');
      while (e.hasMoreElements()) {
        Object item = e.nextElement();
        if (i++ != 0) {
          out.append(',');
        }

        if (item == null) {
          out.writeNull();
          continue;
        }

        Class<?> clazz = item.getClass();

        ObjectSerializer itemSerializer = serializer.getObjectWriter(clazz);
        itemSerializer.write(serializer, item, i - 1, elementType, 0);
      }
      out.append(']');
    } finally {
      serializer.setContext(context);
    }
  }
Пример #4
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);
    }
  }
  public final void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

    Object[] array = (Object[]) object;

    if (object == null) {
      if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
        out.write("[]");
      } else {
        out.writeNull();
      }
      return;
    }

    int size = array.length;

    int end = size - 1;

    if (end == -1) {
      out.append("[]");
      return;
    }

    SerialContext context = serializer.getContext();
    serializer.setContext(context, object, fieldName, 0);

    try {
      Class<?> preClazz = null;
      ObjectSerializer preWriter = null;
      out.append('[');

      if (out.isEnabled(SerializerFeature.PrettyFormat)) {
        serializer.incrementIndent();
        serializer.println();
        for (int i = 0; i < size; ++i) {
          if (i != 0) {
            out.write(',');
            serializer.println();
          }
          serializer.write(array[i]);
        }
        serializer.decrementIdent();
        serializer.println();
        out.write(']');
        return;
      }

      for (int i = 0; i < end; ++i) {
        Object item = array[i];

        if (item == null) {
          out.append("null,");
        } else {
          if (serializer.containsReference(item)) {
            serializer.writeReference(item);
          } else {
            Class<?> clazz = item.getClass();

            if (clazz == preClazz) {
              preWriter.write(serializer, item, null, null);
            } else {
              preClazz = clazz;
              preWriter = serializer.getObjectWriter(clazz);

              preWriter.write(serializer, item, null, null);
            }
          }
          out.append(',');
        }
      }

      Object item = array[end];

      if (item == null) {
        out.append("null]");
      } else {
        if (serializer.containsReference(item)) {
          serializer.writeReference(item);
        } else {
          serializer.writeWithFieldName(item, end);
        }
        out.append(']');
      }
    } finally {
      serializer.setContext(context);
    }
  }