@Test public void testFilter() { TestBean2 bean = new TestBean2(); bean.enumField2 = IdentifiableEnum.value1; bean.setStrField("str1"); bean.setInts(new int[]{ 1, 2 }); bean.testBean = new TestBean(); bean.testBean.setStrField("str2"); bean.testBean.publicStrField = "publicStr"; JSONCoderOption opt = withIndentFactor(2); opt.addFilterFor(TestBean2.class, SimpleFilter.include("enumField2", "testBean")); opt.getSimpleFilterFor(TestBean2.class).addProperties("ints"); opt.addFilterFor(TestBean.class, SimpleFilter.exclude("publicStrField")); opt.addFilterFor(Object.class, SimpleFilter.exclude("fieldInAnyClass")); String result = JSONCoder.encode(bean, opt); log("result(filtered)=" + result); assertTrue("shouldn't contain 'str1'", !result.contains("str1")); assertTrue("should include 'testBean'", result.contains("testBean")); assertTrue("should include 'ints'", result.contains("ints")); assertTrue("should contain 'enumField2'", result.contains("enumField2")); assertTrue("should 'str2'", result.contains("str2")); assertTrue("shouldn't contain 'publicStrField'", !result.contains("publicStrField")); opt.addFilterFor(TestBean2.class, MaskFilterByName.of().add("strField", str -> "hash:" + str.hashCode())); result = JSONCoder.encode(bean, opt); log("result(masked) =" + result); assertTrue("should include str1 hashCode'", result.contains("\"strField\":\"hash:3541024\"")); opt.addSkippedClasses(TestBean.class); result = JSONCoder.encode(bean, opt); log("result=(skipClasses)" + result); assertTrue("shouldn't contain class TestBean", !result.contains("testBean")); }
public static String encode(EncodeReq req, JSONCoderOption opt) { try { StringBuilder sWriter = null; Appendable writer = req.writer; if (writer == null) { writer = sWriter = new StringBuilder(); } TDNode jsonNode = BeanCoder.get().encode(req.object, new BeanCoderContext(opt), req.type); TDJSONWriter.get().write(writer, jsonNode, opt.getJsonOption()); return sWriter == null ? null : sWriter.toString(); } catch (Exception e) { throw new BeanCoderException(e); } }
JSONCoder { public static String encode(EncodeReq req, JSONCoderOption opt) { try { StringBuilder sWriter = null; Appendable writer = req.writer; if (writer == null) { writer = sWriter = new StringBuilder(); } TDNode jsonNode = BeanCoder.get().encode(req.object, new BeanCoderContext(opt), req.type); TDJSONWriter.get().write(writer, jsonNode, opt.getJsonOption()); return sWriter == null ? null : sWriter.toString(); } catch (Exception e) { throw new BeanCoderException(e); } } }
JSONCoder { public static String encode(EncodeReq req, JSONCoderOption opt) { try { StringBuilder sWriter = null; Appendable writer = req.writer; if (writer == null) { writer = sWriter = new StringBuilder(); } TDNode jsonNode = BeanCoder.get().encode(req.object, new BeanCoderContext(opt), req.type); TDJSONWriter.get().write(writer, jsonNode, opt.getJsonOption()); return sWriter == null ? null : sWriter.toString(); } catch (Exception e) { throw new BeanCoderException(e); } } JSONCoder(JSONCoderOption option); }
JSONCoder { public static String encode(EncodeReq req, JSONCoderOption opt) { try { StringBuilder sWriter = null; Appendable writer = req.writer; if (writer == null) { writer = sWriter = new StringBuilder(); } TDNode jsonNode = BeanCoder.get().encode(req.object, new BeanCoderContext(opt), req.type); TDJSONWriter.get().write(writer, jsonNode, opt.getJsonOption()); return sWriter == null ? null : sWriter.toString(); } catch (Exception e) { throw new BeanCoderException(e); } } JSONCoder(JSONCoderOption option); @SuppressWarnings("unchecked") static T decode(DecodeReq<T> req, JSONCoderOption opt); @SuppressWarnings({"unchecked", "WeakerAccess"}) static T decode(String jsonStr, Class<T> type, JSONCoderOption opt); @SuppressWarnings("unchecked") static T decode(Reader reader, Class<T> type, JSONCoderOption opt); static T decode(CharSource source, Class<T> type, JSONCoderOption opt); @SuppressWarnings("unchecked") static T decode(TDNode treeDocNode, Class<T> type, JSONCoderOption opt); T decode(DecodeReq<T> req); T decodeTo(String str, T target); T decode(String str, Class<T> type); @SuppressWarnings("unchecked") T decode(Reader reader, Class<T> type); T decode(CharSource source, Class<T> type); @SuppressWarnings("unchecked") T decode(TDNode treeDocNode, Class<T> type); static String encode(EncodeReq req, JSONCoderOption opt); static String encode(Object obj, JSONCoderOption opt); static void encode(Object obj, Writer writer, JSONCoderOption opt); String encode(EncodeReq req); String encode(Object obj); void encode(Object obj, Writer writer); }
JSONCoder { public static String encode(EncodeReq req, JSONCoderOption opt) { try { StringBuilder sWriter = null; Appendable writer = req.writer; if (writer == null) { writer = sWriter = new StringBuilder(); } TDNode jsonNode = BeanCoder.get().encode(req.object, new BeanCoderContext(opt), req.type); TDJSONWriter.get().write(writer, jsonNode, opt.getJsonOption()); return sWriter == null ? null : sWriter.toString(); } catch (Exception e) { throw new BeanCoderException(e); } } JSONCoder(JSONCoderOption option); @SuppressWarnings("unchecked") static T decode(DecodeReq<T> req, JSONCoderOption opt); @SuppressWarnings({"unchecked", "WeakerAccess"}) static T decode(String jsonStr, Class<T> type, JSONCoderOption opt); @SuppressWarnings("unchecked") static T decode(Reader reader, Class<T> type, JSONCoderOption opt); static T decode(CharSource source, Class<T> type, JSONCoderOption opt); @SuppressWarnings("unchecked") static T decode(TDNode treeDocNode, Class<T> type, JSONCoderOption opt); T decode(DecodeReq<T> req); T decodeTo(String str, T target); T decode(String str, Class<T> type); @SuppressWarnings("unchecked") T decode(Reader reader, Class<T> type); T decode(CharSource source, Class<T> type); @SuppressWarnings("unchecked") T decode(TDNode treeDocNode, Class<T> type); static String encode(EncodeReq req, JSONCoderOption opt); static String encode(Object obj, JSONCoderOption opt); static void encode(Object obj, Writer writer, JSONCoderOption opt); String encode(EncodeReq req); String encode(Object obj); void encode(Object obj, Writer writer);  }