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);
    }
  }
예제 #2
0
 public void writeValue(JSONSerializer paramJSONSerializer, Object paramObject) throws Exception {
   paramJSONSerializer = paramJSONSerializer.getWriter();
   if (paramObject == null) {
     if (paramJSONSerializer.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
       paramJSONSerializer.write('0');
       return;
     }
     paramJSONSerializer.writeNull();
     return;
   }
   paramJSONSerializer.append(paramObject.toString());
 }
예제 #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 write(
     JSONSerializer paramJSONSerializer, Object paramObject1, Object paramObject2, Type paramType)
     throws IOException {
   paramObject2 = paramJSONSerializer.getWriter();
   if (paramObject1 == null) {
     paramJSONSerializer.getWriter().writeNull();
     return;
   }
   if (paramJSONSerializer.isEnabled(SerializerFeature.WriteEnumUsingToString)) {
     paramJSONSerializer.write(((Enum) paramObject1).name());
     return;
   }
   paramObject2.writeInt(((Enum) paramObject1).ordinal());
 }
예제 #5
0
  public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();
    if (object == null) {
      serializer.getWriter().writeNull();
      return;
    }

    if (serializer.isEnabled(SerializerFeature.WriteEnumUsingToString)) {
      Enum<?> e = (Enum<?>) object;
      serializer.write(e.name());
    } else {
      Enum<?> e = (Enum<?>) object;
      out.writeInt(e.ordinal());
    }
  }
예제 #6
0
 public void write(
     JSONSerializer paramJSONSerializer, Object paramObject1, Object paramObject2, Type paramType)
     throws IOException {
   paramJSONSerializer = paramJSONSerializer.getWriter();
   paramObject1 = (Point) paramObject1;
   if (paramObject1 == null) {
     paramJSONSerializer.writeNull();
     return;
   }
   char c = '{';
   if (paramJSONSerializer.isEnabled(SerializerFeature.WriteClassName)) {
     paramJSONSerializer.write('{');
     paramJSONSerializer.writeFieldName(JSON.DEFAULT_TYPE_KEY);
     paramJSONSerializer.writeString(Point.class.getName());
     c = ',';
   }
   paramJSONSerializer.writeFieldValue(c, "x", paramObject1.getX());
   paramJSONSerializer.writeFieldValue(',', "y", paramObject1.getY());
   paramJSONSerializer.write('}');
 }
예제 #7
0
  public void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

    if (object == null) {
      out.writeNull();
      return;
    }

    Date date = (Date) object;

    if (out.isEnabled(SerializerFeature.WriteDateUseDateFormat)) {
      DateFormat format = serializer.getDateFormat();
      if (format == null) {
        format = new SimpleDateFormat(JSON.DEFFAULT_DATE_FORMAT);
      }
      String text = format.format(date);
      out.writeString(text);
      return;
    }

    if (out.isEnabled(SerializerFeature.WriteClassName)) {
      if (object.getClass() != fieldType) {
        if (object.getClass() == java.util.Date.class) {
          out.write("new Date(");
          out.writeLongAndChar(((Date) object).getTime(), ')');
        } else {
          out.write('{');
          out.writeFieldName(JSON.DEFAULT_TYPE_KEY);
          serializer.write(object.getClass().getName());
          out.writeFieldValue(',', "val", ((Date) object).getTime());
          out.write('}');
        }
        return;
      }
    }

    long time = date.getTime();
    if (serializer.isEnabled(SerializerFeature.UseISO8601DateFormat)) {
      if (serializer.isEnabled(SerializerFeature.UseSingleQuotes)) {
        out.append('\'');
      } else {
        out.append('\"');
      }

      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(time);

      int year = calendar.get(Calendar.YEAR);
      int month = calendar.get(Calendar.MONTH) + 1;
      int day = calendar.get(Calendar.DAY_OF_MONTH);
      int hour = calendar.get(Calendar.HOUR_OF_DAY);
      int minute = calendar.get(Calendar.MINUTE);
      int second = calendar.get(Calendar.SECOND);
      int millis = calendar.get(Calendar.MILLISECOND);

      char[] buf;
      if (millis != 0) {
        buf = "0000-00-00T00:00:00.000".toCharArray();
        IOUtils.getChars(millis, 23, buf);
        IOUtils.getChars(second, 19, buf);
        IOUtils.getChars(minute, 16, buf);
        IOUtils.getChars(hour, 13, buf);
        IOUtils.getChars(day, 10, buf);
        IOUtils.getChars(month, 7, buf);
        IOUtils.getChars(year, 4, buf);

      } else {
        if (second == 0 && minute == 0 && hour == 0) {
          buf = "0000-00-00".toCharArray();
          IOUtils.getChars(day, 10, buf);
          IOUtils.getChars(month, 7, buf);
          IOUtils.getChars(year, 4, buf);
        } else {
          buf = "0000-00-00T00:00:00".toCharArray();
          IOUtils.getChars(second, 19, buf);
          IOUtils.getChars(minute, 16, buf);
          IOUtils.getChars(hour, 13, buf);
          IOUtils.getChars(day, 10, buf);
          IOUtils.getChars(month, 7, buf);
          IOUtils.getChars(year, 4, buf);
        }
      }

      out.write(buf);

      int timeZone = calendar.getTimeZone().getRawOffset() / (3600 * 1000);
      if (timeZone == 0) {
        out.append("Z");
      } else if (timeZone > 0) {
        out.append("+").append(String.format("%02d", timeZone)).append(":00");
      } else {
        out.append("-").append(String.format("%02d", -timeZone)).append(":00");
      }

      if (serializer.isEnabled(SerializerFeature.UseSingleQuotes)) {
        out.append('\'');
      } else {
        out.append('\"');
      }
    } else {
      out.writeLong(time);
    }
  }