コード例 #1
0
ファイル: JSON.java プロジェクト: xunmeng2015/bibitaxi
 public String toJSONString() {
   SerializeWriter out = new SerializeWriter();
   try {
     new JSONSerializer(out).write(this);
     return out.toString();
   } finally {
     out.close();
   }
 }
コード例 #2
0
ファイル: JSON.java プロジェクト: xunmeng2015/bibitaxi
 public void writeJSONString(Appendable appendable) {
   SerializeWriter out = new SerializeWriter();
   try {
     new JSONSerializer(out).write(this);
     appendable.append(out.toString());
   } catch (IOException e) {
     throw new JSONException(e.getMessage(), e);
   } finally {
     out.close();
   }
 }
コード例 #3
0
ファイル: JSON.java プロジェクト: xunmeng2015/bibitaxi
  public static String toJSONStringZ(
      Object object, SerializeConfig mapping, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter(features);

    try {
      JSONSerializer serializer = new JSONSerializer(out, mapping);

      serializer.write(object);

      return out.toString();
    } finally {
      out.close();
    }
  }
コード例 #4
0
ファイル: FastjsonCodec.java プロジェクト: xzoth/fastjson
  public String encode(Object object) throws Exception {
    SerializeWriter out = new SerializeWriter();
    out.config(SerializerFeature.DisableCircularReferenceDetect, true);
    //        out.config(SerializerFeature.DisableCheckSpecialChar, true);

    JSONSerializer serializer = new JSONSerializer(out);
    serializer.write(object);

    String text = out.toString();

    out.close();

    return text;
  }
コード例 #5
0
ファイル: JSON.java プロジェクト: xunmeng2015/bibitaxi
  public static void writeJSONStringTo(
      Object object, Writer writer, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter(writer);

    try {
      JSONSerializer serializer = new JSONSerializer(out);
      for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
        serializer.config(feature, true);
      }

      serializer.write(object);
    } finally {
      out.close();
    }
  }
コード例 #6
0
ファイル: JSON.java プロジェクト: xunmeng2015/bibitaxi
  public static byte[] toJSONBytes(Object object, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter();

    try {
      JSONSerializer serializer = new JSONSerializer(out);
      for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
        serializer.config(feature, true);
      }

      serializer.write(object);

      return out.toBytes("UTF-8");
    } finally {
      out.close();
    }
  }
コード例 #7
0
ファイル: JSON.java プロジェクト: xunmeng2015/bibitaxi
  public static String toJSONString(
      Object object, SerializeFilter[] filters, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter();

    try {
      JSONSerializer serializer = new JSONSerializer(out);
      for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) {
        serializer.config(feature, true);
      }

      serializer.config(SerializerFeature.WriteDateUseDateFormat, true);

      setFilter(serializer, filters);

      serializer.write(object);

      return out.toString();
    } finally {
      out.close();
    }
  }
コード例 #8
0
ファイル: NameFilterTest.java プロジェクト: heydaies/fastjson
  public void test_namefilter() throws Exception {
    NameFilter filter =
        new NameFilter() {

          public String process(Object source, String name, Object value) {
            if (name.equals("id")) {
              return "ID";
            }

            return name;
          }
        };

    SerializeWriter out = new SerializeWriter();
    JSONSerializer serializer = new JSONSerializer(out);
    serializer.getNameFilters().add(filter);

    Bean a = new Bean();
    serializer.write(a);

    String text = out.toString();
    Assert.assertEquals("{\"ID\":0}", text);
  }
コード例 #9
0
ファイル: JSON.java プロジェクト: xunmeng2015/bibitaxi
  /** @since 1.1.14 */
  public static String toJSONStringWithDateFormat(
      Object object, String dateFormat, SerializerFeature... features) {
    SerializeWriter out = new SerializeWriter();

    try {
      JSONSerializer serializer = new JSONSerializer(out);
      for (SerializerFeature feature : features) {
        serializer.config(feature, true);
      }

      serializer.config(SerializerFeature.WriteDateUseDateFormat, true);

      if (dateFormat != null) {
        serializer.setDateFormat(dateFormat);
      }

      serializer.write(object);

      return out.toString();
    } finally {
      out.close();
    }
  }
コード例 #10
0
  public void test_0() throws Exception {
    StringWriter out = new StringWriter();

    SerializeWriter writer = new SerializeWriter(out, 10);
    Assert.assertEquals(10, writer.getBufferLength());

    writer.write("abcde");
    writer.writeInt(12345678);
    writer.write(',');
    writer.close();

    String text = out.toString();
    Assert.assertEquals("abcde12345678,", text);
  }
コード例 #11
0
  public final void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
      throws IOException {
    boolean writeClassName = serializer.isEnabled(SerializerFeature.WriteClassName);

    SerializeWriter out = serializer.getWriter();

    Type elementType = null;
    if (writeClassName) {
      if (fieldType instanceof ParameterizedType) {
        ParameterizedType param = (ParameterizedType) fieldType;
        elementType = param.getActualTypeArguments()[0];
      }
    }

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

    List<?> list = (List<?>) object;

    if (list.size() == 0) {
      out.append("[]");
      return;
    }

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

    ObjectSerializer itemSerializer = null;
    try {
      if (out.isEnabled(SerializerFeature.PrettyFormat)) {
        out.append('[');
        serializer.incrementIndent();

        handle(serializer, itemSerializer, context, object, 0, elementType, list.get(0));
        for (int i = 1, size = list.size(); i < size; ++i) {
          out.append(',');
          handle(serializer, itemSerializer, context, object, i, elementType, list.get(i));
        }

        serializer.decrementIdent();
        serializer.println();
        out.append(']');
        return;
      }

      out.append('[');
      int i = 0;
      for (Object item : list) {
        if (i != 0) {
          out.append(',');
        }

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

          if (clazz == Integer.class) {
            out.writeInt(((Integer) item).intValue());
          } else if (clazz == Long.class) {
            long val = ((Long) item).longValue();
            if (writeClassName) {
              out.writeLongAndChar(val, 'L');
            } else {
              out.writeLong(val);
            }
          } else {
            SerialContext itemContext = new SerialContext(context, object, fieldName, 0, 0);
            serializer.setContext(itemContext);

            // if (serializer.containsReference(item)) {
            // serializer.writeReference(item);
            // } else {
            // itemSerializer =
            // serializer.getObjectWriter(item.getClass());
            // itemSerializer.write(serializer, item, i,
            // elementType, 0);
            // }
            itemSerializer = serializer.getObjectWriter(item.getClass());
            // TODO
            itemSerializer.write(serializer, item, i, elementType, 0);
          }
        }
        i++;
      }
      out.append(']');
    } finally {
      serializer.setContext(context);
    }
  }