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 Object getPropertyValue(Object object)
     throws InvocationTargetException, IllegalAccessException {
   return fieldInfo.get(object);
 }
Example #3
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);
    }
  }
Example #4
0
  @SuppressWarnings("unchecked")
  public static Object toJSON(Object javaObject, ParserConfig mapping) {
    if (javaObject == null) {
      return null;
    }

    if (javaObject instanceof JSON) {
      return javaObject;
    }

    if (javaObject instanceof Map) {
      Map<Object, Object> map = (Map<Object, Object>) javaObject;

      JSONObject json = new JSONObject(map.size());

      for (Map.Entry<Object, Object> entry : map.entrySet()) {
        Object key = entry.getKey();
        String jsonKey = TypeUtils.castToString(key);
        Object jsonValue = toJSON(entry.getValue());
        json.put(jsonKey, jsonValue);
      }

      return json;
    }

    if (javaObject instanceof Collection) {
      Collection<Object> collection = (Collection<Object>) javaObject;

      JSONArray array = new JSONArray(collection.size());

      for (Object item : collection) {
        Object jsonValue = toJSON(item);
        array.add(jsonValue);
      }

      return array;
    }

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

    if (clazz.isEnum()) {
      return ((Enum<?>) javaObject).name();
    }

    if (clazz.isArray()) {
      int len = Array.getLength(javaObject);

      JSONArray array = new JSONArray(len);

      for (int i = 0; i < len; ++i) {
        Object item = Array.get(javaObject, i);
        Object jsonValue = toJSON(item);
        array.add(jsonValue);
      }

      return array;
    }

    if (mapping.isPrimitive(clazz)) {
      return javaObject;
    }

    try {
      List<FieldInfo> getters = TypeUtils.computeGetters(clazz, null);

      JSONObject json = new JSONObject(getters.size());

      for (FieldInfo field : getters) {
        Object value = field.get(javaObject);
        Object jsonValue = toJSON(value);

        json.put(field.getName(), jsonValue);
      }

      return json;
    } catch (IllegalAccessException e) {
      throw new JSONException("toJSON error", e);
    } catch (InvocationTargetException e) {
      throw new JSONException("toJSON error", e);
    }
  }