Пример #1
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;
  }
Пример #2
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();
    }
  }
Пример #3
0
  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();
    }
  }
Пример #4
0
  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();
    }
  }
Пример #5
0
  public void write(
      JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
      throws IOException {

    Optional<?> optional = (Optional<?>) object;
    Object value = optional.get();
    serializer.write(value);
  }
Пример #6
0
  private static void setFilter(JSONSerializer serializer, SerializeFilter filter) {
    if (filter == null) {
      return;
    }

    if (filter instanceof PropertyPreFilter) {
      serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
    }

    if (filter instanceof NameFilter) {
      serializer.getNameFilters().add((NameFilter) filter);
    }

    if (filter instanceof ValueFilter) {
      serializer.getValueFilters().add((ValueFilter) filter);
    }

    if (filter instanceof PropertyFilter) {
      serializer.getPropertyFilters().add((PropertyFilter) filter);
    }

    if (filter instanceof BeforeFilter) {
      serializer.getBeforeFilters().add((BeforeFilter) filter);
    }

    if (filter instanceof AfterFilter) {
      serializer.getAfterFilters().add((AfterFilter) filter);
    }

    if (filter instanceof LabelFilter) {
      serializer.getLabelFilters().add((LabelFilter) filter);
    }
  }
Пример #7
0
 private void handle(
     JSONSerializer serializer,
     ObjectSerializer itemSerializer,
     SerialContext context,
     Object object,
     Object fieldName,
     Type elementType,
     Object item)
     throws IOException {
   serializer.println();
   if (item != null) {
     itemSerializer = serializer.getObjectWriter(item.getClass());
     SerialContext itemContext = new SerialContext(context, object, fieldName, 0, 0);
     serializer.setContext(itemContext);
     itemSerializer.write(serializer, item, fieldName, elementType, 0);
   } else {
     serializer.getWriter().writeNull();
   }
 }
Пример #8
0
  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();
    }
  }
Пример #9
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);
  }
Пример #10
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();
    }
  }
Пример #11
0
 public void write(
     JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
     throws IOException {
   serializer.write(path);
 }
Пример #12
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);
    }
  }