示例#1
0
 public String toJSONString() {
   SerializeWriter out = new SerializeWriter();
   try {
     new JSONSerializer(out).write(this);
     return out.toString();
   } finally {
     out.close();
   }
 }
示例#2
0
 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
  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
  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
  public static String toJSONString(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.toString();
    } finally {
      out.close();
    }
  }
示例#6
0
  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);
  }
示例#7
0
  /** @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();
    }
  }