public void test_namefilter() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (name.equals("id")) { Assert.assertTrue(value instanceof Character); 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); }
public void test_namefilter_1() 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); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", '0'); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"ID\":\"0\"}", text); }
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.0}", text); }
public void test_namefilter_1() 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); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", 0); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"ID\":0}", text); }
public void test_namefilter_1() 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); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", (byte) 0); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"ID\":0}", text); }
public void test_0() throws Exception { Object[] array = { new ModelA(), new ModelB() }; SerializeConfig config = new SerializeConfig(); config.addFilter(ModelA.class, // new PascalNameFilter()); config.addFilter(ModelB.class, // new NameFilter() { @Override public String process(Object object, String name, Object value) { return name; } }); String text2 = JSON.toJSONString(array, config); Assert.assertEquals("[{\"Id\":1001},{\"id\":1002}]", text2); String text = JSON.toJSONString(array); Assert.assertEquals("[{\"id\":1001},{\"id\":1002}]", text); }
public void test_namefilter_1() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (name.equals("id")) { Assert.assertTrue(value instanceof Short); return "ID"; } return name; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getNameFilters().add(filter); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", (short) 0); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"ID\":0}", text); }
public void test_namefilter() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (name.equals("1001")) { return "ID"; } return name; } }; SerializeWriter out = new SerializeWriter(); JSONSerializer serializer = new JSONSerializer(out); serializer.getNameFilters().add(filter); Map map = new HashMap(); map.put(1001, 0); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"ID\":0}", text); }
public void test_namefilter_1() 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); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", true); serializer.write(map); String text = out.toString(); Assert.assertEquals("{\"ID\":true}", text); }
public void test_namefilter() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (name.equals("id")) { Assert.assertTrue(value instanceof Long); 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); }
public void test_namefilter() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (name.equals("id")) { Assert.assertTrue(value instanceof Integer); 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); }
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); }
private static void setFilter(JSONSerializer serializer, SerializeFilter filter) { if (filter != null) { 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); } } }
public void test_for_issue() throws Exception { User user = new User(); user.setId(1); user.setName("name"); NameFilter filter = new NameFilter() { public String process(Object object, String name, Object value) { System.out.println("name="+name+",value="+value); if(name.equals("name")){ return "nt"; } return name; } }; // test for JSON.toJSONString(user,filter); String jsonString = JSON.toJSONString(user,filter); System.out.println(jsonString); assertEquals("{\"id\":1,\"nt\":\"name\"}", jsonString); // test for jsonSerializer.getNameFilters().add(filter); JSONSerializer jsonSerializer = new JSONSerializer(); jsonSerializer.getNameFilters().add(filter); jsonSerializer.write(user); jsonString = jsonSerializer.toString(); System.out.println(jsonString); assertEquals("{\"id\":1,\"nt\":\"name\"}",jsonString); }
public void test_for_issue() throws Exception { Model model = new Model(); model.oldValue = new Value(); String json = JSON.toJSONString(model, new NameFilter() { public String process(Object object, String name, Object value) { if ("oldValue".equals(name)) { return "old_value"; } return name; } }); System.out.println(json); }
public void test_filter() throws Exception { NameFilter upcaseNameFilter = new NameFilter() { @Override public String process(Object object, String name, Object value) { return name.toUpperCase(); } }; SerializeConfig.getGlobalInstance() // .addFilter(A.class, upcaseNameFilter); Assert.assertEquals("{\"ID\":0}", JSON.toJSONString(new A())); Assert.assertEquals("{\"id\":0}", JSON.toJSONString(new B())); }
public void test_namefilter() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (value != null) { Assert.assertTrue(value instanceof Byte); } 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); }
public void test_namefilter() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (value != null) { Assert.assertTrue(value instanceof Short); } 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); }
public void test_namefilter() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (value != null) { Assert.assertTrue(value instanceof Boolean); } 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\":false}", text); }
public static void test_toJSONString() throws Exception { NameFilter filter = new NameFilter() { public String process(Object source, String name, Object value) { if (name.equals("id")) { return "ID"; } return name; } }; Assert.assertEquals("{\"ID\":0}", JSON.toJSONString(Collections.singletonMap("id", 0), filter)); }