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);
  }
Exemplo n.º 2
0
  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);
  }
Exemplo n.º 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);
  }
Exemplo n.º 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());
  }
Exemplo n.º 5
0
 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;
 }
Exemplo n.º 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));
  }
Exemplo n.º 7
0
 static {
   mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
 }
Exemplo n.º 8
0
 public JSONPath(String path) {
   this(path, SerializeConfig.getGlobalInstance(), ParserConfig.getGlobalInstance());
 }