Exemplo n.º 1
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);
  }
Exemplo n.º 2
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());
    }
  }
Exemplo n.º 3
0
  public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType)
      throws IOException {
    SerializeWriter out = serializer.getWriter();
    Point font = (Point) object;
    if (font == null) {
      out.writeNull();
      return;
    }

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

    out.writeFieldValue(sep, "x", font.getX());
    out.writeFieldValue(',', "y", font.getY());
    out.write('}');
  }
Exemplo n.º 4
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();
    }
  }
Exemplo n.º 5
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);
    }
  }