Java 类com.alibaba.fastjson.serializer.JavaBeanSerializer 实例源码

项目:GitHub    文件:JSONPath.java   
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Collection<Object> getPropertyValues(final Object currentObject) {
    final Class<?> currentClass = currentObject.getClass();

    JavaBeanSerializer beanSerializer = getJavaBeanSerializer(currentClass);

    if (beanSerializer != null) {
        try {
            return beanSerializer.getFieldValues(currentObject);
        } catch (Exception e) {
            throw new JSONPathException("jsonpath error, path " + path, e);
        }
    }

    if (currentObject instanceof Map) {
        Map map = (Map) currentObject;
        return map.values();
    }

    throw new UnsupportedOperationException();
}
项目:GitHub    文件:TypeUtilsTest_castToJavaBean.java   
public void test_bean_2() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 123);
    PO vo = TypeUtils.castToJavaBean(map, PO.class);
    Assert.assertEquals(123, vo.id);

    SerializeWriter out = new SerializeWriter();

    try {
        SerializeConfig config = new SerializeConfig();
        JSONSerializer serializer = new JSONSerializer(out, config);
        config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", "ID")));

        serializer.write(vo);

        Assert.assertEquals("{\"ID\":123}", out.toString());
    } finally {
        out.close();
    }


}
项目:GitHub    文件:TypeUtilsTest_castToJavaBean.java   
public void test_bean_3() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", 123);
    PO vo = TypeUtils.castToJavaBean(map, PO.class);
    Assert.assertEquals(123, vo.id);

    SerializeWriter out = new SerializeWriter();

    try {
        SerializeConfig config = new SerializeConfig();
        JSONSerializer serializer = new JSONSerializer(out, config);
        config.put(PO.class, new JavaBeanSerializer(PO.class, Collections.singletonMap("id", (String) null)));

        serializer.write(vo);

        Assert.assertEquals("{}", out.toString());
    } finally {
        out.close();
    }


}
项目:uavstack    文件:JSONPath.java   
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Collection<Object> getPropertyValues(final Object currentObject) {
    final Class<?> currentClass = currentObject.getClass();

    JavaBeanSerializer beanSerializer = getJavaBeanSerializer(currentClass);

    if (beanSerializer != null) {
        try {
            return beanSerializer.getFieldValues(currentObject);
        } catch (Exception e) {
            throw new JSONPathException("jsonpath error, path " + path, e);
        }
    }

    if (currentObject instanceof Map) {
        Map map = (Map) currentObject;
        return map.values();
    }

    throw new UnsupportedOperationException();
}
项目:GitHub    文件:JSON_toJSONStringTest.java   
public void test_0() throws Exception {
    User user = new User();
    user.setId(123);
    user.setName("毛头");

    SerializeConfig mapping = new SerializeConfig();
    mapping.put(User.class, new JavaBeanSerializer(User.class, "id"));

    JSONSerializer serializer = new JSONSerializer(mapping);
    serializer.write(user);
    String jsonString = serializer.toString();

    Assert.assertEquals("{\"id\":123}", jsonString);
}
项目:GitHub    文件:JSON_toJSONStringTest.java   
public void test_1() throws Exception {
    User user = new User();
    user.setId(123);
    user.setName("毛头");

    SerializeConfig mapping = new SerializeConfig();
    mapping.put(User.class, new JavaBeanSerializer(User.class, Collections.singletonMap("id", "uid")));

    JSONSerializer serializer = new JSONSerializer(mapping);
    serializer.write(user);
    String jsonString = serializer.toString();

    Assert.assertEquals("{\"uid\":123}", jsonString);
}
项目:GitHub    文件:JavaBeanSerializerTest.java   
public void test_0_s() throws Exception {
    SerializeWriter out = new SerializeWriter();

    A a = new A();
    a.getL0().add("A");
    a.getL0().add("B");

    JavaBeanSerializer serializer = new JavaBeanSerializer(A.class);
    serializer.write(new JSONSerializer(out), a, null, null, 0);

    Assert.assertEquals("{\"l0\":[\"A\",\"B\"]}", out.toString());
}
项目:GitHub    文件:JavaBeanSerializerTest.java   
public void test_1_s() throws Exception {
    SerializeWriter out = new SerializeWriter();

    B a = new B();
    a.getL0().add("A");
    a.getL0().add("B");

    JavaBeanSerializer serializer = new JavaBeanSerializer(B.class);
    serializer.write(new JSONSerializer(out), a, null, null, 0);

    Assert.assertEquals("{\"l0\":[\"A\",\"B\"],\"l1\":[]}", out.toString());
}
项目:GitHub    文件:JavaBeanSerializerTest.java   
public void test_2_s() throws Exception {
    SerializeWriter out = new SerializeWriter();

    JavaBeanSerializer serializer = new JavaBeanSerializer(F.class);
    serializer.write(new JSONSerializer(out), new F(new E(123)), null, null, 0);

    Assert.assertEquals("{\"e\":{\"id\":123}}", out.toString());
}
项目:GitHub    文件:JavaBeanSerializerTest.java   
public void test_3_s() throws Exception {
    SerializeWriter out = new SerializeWriter();

    JavaBeanSerializer serializer = new JavaBeanSerializer(F.class);
    serializer.write(new JSONSerializer(out), new F(null), null, null, 0);

    Assert.assertEquals("{}", out.toString());
}
项目:GitHub    文件:JavaBeanSerializerTest.java   
public void test_error_s() throws Exception {
    JSONException error = null;
    try {
        SerializeWriter out = new SerializeWriter();
        JavaBeanSerializer serializer = new JavaBeanSerializer(C.class);
        serializer.write(new JSONSerializer(out), new C(), null, null, 0);
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JavaBeanSerializerTest.java   
public void test_error_1_s() throws Exception {
    JSONException error = null;
    try {
        SerializeWriter out = new SerializeWriter();
        JavaBeanSerializer serializer = new JavaBeanSerializer(D.class);
        serializer.write(new JSONSerializer(out), new D(), null, null, 0);
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:uavstack    文件:JSONPath.java   
@SuppressWarnings("rawtypes")
protected Object getPropertyValue(final Object currentObject, final String propertyName, boolean strictMode) {
    if (currentObject == null) {
        return null;
    }

    if (currentObject instanceof Map) {
        Map map = (Map) currentObject;
        return map.get(propertyName);
    }

    final Class<?> currentClass = currentObject.getClass();

    JavaBeanSerializer beanSerializer = getJavaBeanSerializer(currentClass);
    if (beanSerializer != null) {
        try {
            return beanSerializer.getFieldValue(currentObject, propertyName);
        } catch (Exception e) {
            throw new JSONPathException("jsonpath error, path " + path + ", segement " + propertyName, e);
        }
    }

    if (currentObject instanceof List) {
        List list = (List) currentObject;

        List<Object> fieldValues = new ArrayList<Object>(list.size());

        for (int i = 0; i < list.size(); ++i) {
            Object obj = list.get(i);
            Object itemValue = getPropertyValue(obj, propertyName, strictMode);
            fieldValues.add(itemValue);
        }

        return fieldValues;
    }
    throw new JSONPathException("jsonpath error, path " + path + ", segement " + propertyName);
}
项目:GitHub    文件:JSONPath.java   
@SuppressWarnings("rawtypes")
int evalSize(Object currentObject) {
    if (currentObject == null) {
        return -1;
    }

    if (currentObject instanceof Collection) {
        return ((Collection) currentObject).size();
    }

    if (currentObject instanceof Object[]) {
        return ((Object[]) currentObject).length;
    }

    if (currentObject.getClass().isArray()) {
        return Array.getLength(currentObject);
    }

    if (currentObject instanceof Map) {
        int count = 0;

        for (Object value : ((Map) currentObject).values()) {
            if (value != null) {
                count++;
            }
        }
        return count;
    }

    JavaBeanSerializer beanSerializer = getJavaBeanSerializer(currentObject.getClass());

    if (beanSerializer == null) {
        return -1;
    }

    try {
        return beanSerializer.getSize(currentObject);
    } catch (Exception e) {
        throw new JSONPathException("evalSize error : " + path, e);
    }
}
项目:GitHub    文件:JavaBeanSerializerTest2.java   
public void test_0() throws Exception {
    new JavaBeanSerializer(A.class, Collections.<String, String> emptyMap());
}