コード例 #1
0
  public void test_0() throws Exception {
    Object[] array = {new ModelA(), new ModelB()};

    SerializeConfig config = new SerializeConfig();
    config.addFilter(
        ModelA.class, //
        new AfterFilter() {

          @Override
          public void writeAfter(Object object) {
            this.writeKeyValue("type", "A");
          }
        });
    config.addFilter(
        ModelB.class, //
        new AfterFilter() {

          @Override
          public void writeAfter(Object object) {
            this.writeKeyValue("type", "B");
          }
        });
    String text2 = JSON.toJSONString(array, config);
    Assert.assertEquals("[{\"id\":1001,\"type\":\"A\"},{\"id\":1002,\"type\":\"B\"}]", text2);

    String text = JSON.toJSONString(array);
    Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text);
  }
コード例 #2
0
ファイル: FeaturesTest3.java プロジェクト: judasn/fastjson
  public void test_1() throws Exception {
    SerializeConfig config = new SerializeConfig();
    config.setAsmEnable(true);

    String text = JSON.toJSONString(new Entity(), config);
    Assert.assertEquals("{\"value\":0}", text);
  }
コード例 #3
0
  public void test_codec_asm() throws Exception {
    V0 v = new V0();
    v.setValue(new Date());

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(true);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale);
    format.setTimeZone(JSON.defaultTimeZone);
    Assert.assertEquals("{\"value\":" + JSON.toJSONString(format.format(v.getValue())) + "}", text);
  }
コード例 #4
0
  public void test_codec_null() throws Exception {
    V0 v = new V0();

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);

    String text = JSON.toJSONString(v, mapping, SerializerFeature.WriteMapNullValue);
    Assert.assertEquals("{\"value\":null}", text);

    V0 v1 = JSON.parseObject(text, V0.class);

    Assert.assertEquals(v1.getValue(), v.getValue());
  }
コード例 #5
0
ファイル: JSONPath.java プロジェクト: sueking/fastjson
 protected JavaBeanSerializer getJavaBeanSerializer(final Class<?> currentClass) {
   JavaBeanSerializer beanSerializer = null;
   {
     ObjectSerializer serializer = serializeConfig.getObjectWriter(currentClass);
     if (serializer instanceof JavaBeanSerializer) {
       beanSerializer = (JavaBeanSerializer) serializer;
     } else if (serializer instanceof ASMJavaBeanSerializer) {
       beanSerializer = ((ASMJavaBeanSerializer) serializer).getJavaBeanSerializer();
     }
   }
   return beanSerializer;
 }
コード例 #6
0
  public void test_codec_null_1() throws Exception {
    V0 v = new V0();

    SerializeConfig mapping = new SerializeConfig();
    mapping.setAsmEnable(false);

    Assert.assertEquals(
        "{\"value\":null}",
        JSON.toJSONString(
            v,
            mapping,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty));
    Assert.assertEquals(
        "{value:null}",
        JSON.toJSONStringZ(
            v,
            mapping,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty));
    Assert.assertEquals(
        "{value:null}",
        JSON.toJSONStringZ(
            v,
            mapping,
            SerializerFeature.UseSingleQuotes,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty));
    Assert.assertEquals(
        "{'value':null}",
        JSON.toJSONStringZ(
            v,
            mapping,
            SerializerFeature.UseSingleQuotes,
            SerializerFeature.QuoteFieldNames,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty));
  }
コード例 #7
0
ファイル: JsonParser.java プロジェクト: X010/village-common
 static {
   mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
 }
コード例 #8
0
ファイル: JSONPath.java プロジェクト: sueking/fastjson
 public JSONPath(String path) {
   this(path, SerializeConfig.getGlobalInstance(), ParserConfig.getGlobalInstance());
 }