public void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

    AtomicLong val = (AtomicLong) object;
    out.writeLong(val.get());
  }
Beispiel #2
0
  public void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
      throws IOException {
    SerializeWriter out = serializer.out;

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

    float floatValue = ((Float) object).floatValue();
    out.writeFloat(floatValue, true);
  }
Beispiel #3
0
  public void write(JSONSerializer serializer, String value) {
    SerializeWriter out = serializer.getWriter();

    if (value == null) {
      if (out.isEnabled(SerializerFeature.WriteNullStringAsEmpty)) {
        out.writeString("");
      } else {
        out.writeNull();
      }
      return;
    }

    out.writeString(value);
  }
  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);
    }
  }
  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());
    }
  }
  public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

    if (object == null) {
      if (out.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
        out.write('0');
      } else {
        out.writeNull();
      }
      return;
    }

    BigInteger val = (BigInteger) object;
    out.write(val.toString());
  }
Beispiel #7
0
  public final void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

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

    byte[] array = (byte[]) object;
    out.writeByteArray(array);
  }
Beispiel #8
0
  public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

    Character value = (Character) object;
    if (value == null) {
      out.writeString("");
      return;
    }

    char c = value.charValue();
    if (c == 0) {
      out.writeString("\u0000");
    } else {
      out.writeString(value.toString());
    }
  }
  public final void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

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

    Object[] array = (Object[]) object;
    int size = array.length;

    int end = size - 1;

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

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

      if (item == null) {
        out.append("null,");
      } else {
        compObjectSerializer.write(serializer, item, null, null);
        out.append(',');
      }
    }

    Object item = array[end];

    if (item == null) {
      out.append("null]");
    } else {
      compObjectSerializer.write(serializer, item, null, null);
      out.append(']');
    }
  }
Beispiel #10
0
  public void writePrefix(JSONSerializer serializer) throws IOException {
    SerializeWriter out = serializer.out;

    if (out.quoteFieldNames) {
      if (out.useSingleQuotes) {
        if (single_quoted_fieldPrefix == null) {
          single_quoted_fieldPrefix = '\'' + fieldInfo.name + "\':";
        }
        out.write(single_quoted_fieldPrefix);
      } else {
        out.write(double_quoted_fieldPrefix);
      }
    } else {
      if (un_quoted_fieldPrefix == null) {
        this.un_quoted_fieldPrefix = fieldInfo.name + ":";
      }
      out.write(un_quoted_fieldPrefix);
    }
  }
  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);
    }
  }
Beispiel #12
0
  public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();
    Font font = (Font) object;
    if (font == null) {
      out.writeNull();
      return;
    }

    char sep = '{';
    if (out.isEnabled(SerializerFeature.WriteClassName)) {
      out.write('{');
      out.writeFieldName(JSON.DEFAULT_TYPE_KEY);
      out.writeString(Font.class.getName());
      sep = ',';
    }

    out.writeFieldValue(sep, "name", font.getName());
    out.writeFieldValue(',', "style", font.getStyle());
    out.writeFieldValue(',', "size", font.getSize());
    out.write('}');
  }
Beispiel #13
0
  public final void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

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

    short[] array = (short[]) object;
    out.write('[');
    for (int i = 0; i < array.length; ++i) {
      if (i != 0) {
        out.write(',');
      }
      out.writeInt(array[i]);
    }
    out.write(']');
  }
Beispiel #14
0
  public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

    if (object == null) {
      if (out.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
        out.write('0');
      } else {
        out.writeNull();
      }
      return;
    }

    BigDecimal val = (BigDecimal) object;
    out.write(val.toString());

    if (out.isEnabled(SerializerFeature.WriteClassName)
        && fieldType != BigDecimal.class
        && val.scale() == 0) {
      out.write('.');
    }
  }
Beispiel #15
0
  public void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

    Boolean value = (Boolean) object;
    if (value == null) {
      if (out.isEnabled(SerializerFeature.WriteNullBooleanAsFalse)) {
        out.write("false");
      } else {
        out.writeNull();
      }
      return;
    }

    if (value.booleanValue()) {
      out.write("true");
    } else {
      out.write("false");
    }
  }
Beispiel #16
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, int features)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

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

    float[] array = (float[]) object;
    int size = array.length;

    int end = size - 1;

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

    out.append('[');
    for (int i = 0; i < end; ++i) {
      float item = array[i];

      if (Float.isNaN(item)) {
        out.writeNull();
      } else {
        out.append(Float.toString(item));
      }

      out.append(',');
    }

    float item = array[end];

    if (Float.isNaN(item)) {
      out.writeNull();
    } else {
      out.append(Float.toString(item));
    }

    out.append(']');
  }
Beispiel #18
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);
    }
  }
Beispiel #19
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  public void write(PBSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();

    if (fieldName == null) {
      if (object instanceof ConcurrentHashMap) {
        out.getCodedOutputStream().writeRawByte(com.jd.glowworm.asm.Type.MAP_ConcurrentHashMap);
      } else if (object instanceof LinkedHashMap) {
        out.getCodedOutputStream().writeRawByte(com.jd.glowworm.asm.Type.MAP_LinkedHash);
      } else if (object instanceof HashMap) {
        out.getCodedOutputStream().writeRawByte(com.jd.glowworm.asm.Type.MAP_HASH);
      } else {
        out.getCodedOutputStream().writeRawByte(com.jd.glowworm.asm.Type.Unknown);
      }
    }

    Map<?, ?> map = (Map<?, ?>) object;

    try {
      int tmpMapSize = map.size();
      out.writeInt(tmpMapSize);

      Class<?> preClazz = null;
      ObjectSerializer preWriter = null;

      for (Map.Entry entry : map.entrySet()) {
        Object entryKey = entry.getKey();
        Object value = entry.getValue();

        // if (entryKey == null || entryKey instanceof String)
        if (entryKey instanceof String) {
          String key = (String) entryKey;
          StringSerializer.instance.write(serializer, key, null, null);
        } else {
          serializer.write(entryKey);
        }

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

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

        if (clazz == preClazz) {
          preWriter.write(serializer, value, null, null);
        } else {
          preClazz = clazz;
          preWriter = serializer.getObjectWriter(clazz);
          String tmpClassName = preWriter.getClass().getName();
          if (tmpClassName.startsWith(ASMSerializerFactory.GenClassName_prefix)) {
            out.getCodedOutputStream().writeRawByte(com.jd.glowworm.asm.Type.OBJECT);
            out.writeString(clazz.getName());
          }

          preWriter.write(serializer, value, null, clazz);
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  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);
    }
  }