@Test public void testDedupWithRef() { TestBean2 tb2 = new TestBean2(); TestBean tb = new TestBean().setBean2(tb2).setInts(new int[]{1,2,3}); tb.bean2List = Collections.singletonList(tb2); tb2.setInts(tb.getInts()); String str = toJSONString(tb, of().setDedupWithRef(true).setJsonOption(false, '"', 2)); log("JSONStr=" + str); assertTrue("Generate ref if dedupWithRef is set", str.contains("$ref")); TestBean tb1 = JSONCoder.global.decode(str, TestBean.class); assertSame(tb1.getBean2(), tb1.bean2List.get(0)); assertSame(tb1.getInts(), tb1.getBean2().getInts()); }
@SuppressWarnings("unchecked") public static <T> T decode(DecodeReq<T> req, JSONCoderOption opt) { try { TDNode tdNode = req.tdNode; if (tdNode == null && req.source != null) { tdNode = TDJSONParser.get().parse(req.source, new TDJSONParserOption()); } if (req.nodePath != null) tdNode = tdNode.getByPath(TDPath.parse(req.nodePath)); if (tdNode == null) return null; return (T) BeanCoder.get().decode(tdNode, req.getType(), req.target, "", new BeanCoderContext(opt)); } catch (Exception e) { if (e instanceof BeanCoderException) throw (BeanCoderException)e; throw new BeanCoderException(e); } }
JSONCoder { @SuppressWarnings("unchecked") public static <T> T decode(DecodeReq<T> req, JSONCoderOption opt) { try { TDNode tdNode = req.tdNode; if (tdNode == null && req.source != null) { tdNode = TDJSONParser.get().parse(req.source, new TDJSONParserOption()); } if (req.nodePath != null) tdNode = tdNode.getByPath(TDPath.parse(req.nodePath)); if (tdNode == null) return null; return (T) BeanCoder.get().decode(tdNode, req.getType(), req.target, "", new BeanCoderContext(opt)); } catch (Exception e) { if (e instanceof BeanCoderException) throw (BeanCoderException)e; throw new BeanCoderException(e); } } }
JSONCoder { @SuppressWarnings("unchecked") public static <T> T decode(DecodeReq<T> req, JSONCoderOption opt) { try { TDNode tdNode = req.tdNode; if (tdNode == null && req.source != null) { tdNode = TDJSONParser.get().parse(req.source, new TDJSONParserOption()); } if (req.nodePath != null) tdNode = tdNode.getByPath(TDPath.parse(req.nodePath)); if (tdNode == null) return null; return (T) BeanCoder.get().decode(tdNode, req.getType(), req.target, "", new BeanCoderContext(opt)); } catch (Exception e) { if (e instanceof BeanCoderException) throw (BeanCoderException)e; throw new BeanCoderException(e); } } JSONCoder(JSONCoderOption option); }
JSONCoder { @SuppressWarnings("unchecked") public static <T> T decode(DecodeReq<T> req, JSONCoderOption opt) { try { TDNode tdNode = req.tdNode; if (tdNode == null && req.source != null) { tdNode = TDJSONParser.get().parse(req.source, new TDJSONParserOption()); } if (req.nodePath != null) tdNode = tdNode.getByPath(TDPath.parse(req.nodePath)); if (tdNode == null) return null; return (T) BeanCoder.get().decode(tdNode, req.getType(), req.target, "", new BeanCoderContext(opt)); } catch (Exception e) { if (e instanceof BeanCoderException) throw (BeanCoderException)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 { @SuppressWarnings("unchecked") public static <T> T decode(DecodeReq<T> req, JSONCoderOption opt) { try { TDNode tdNode = req.tdNode; if (tdNode == null && req.source != null) { tdNode = TDJSONParser.get().parse(req.source, new TDJSONParserOption()); } if (req.nodePath != null) tdNode = tdNode.getByPath(TDPath.parse(req.nodePath)); if (tdNode == null) return null; return (T) BeanCoder.get().decode(tdNode, req.getType(), req.target, "", new BeanCoderContext(opt)); } catch (Exception e) { if (e instanceof BeanCoderException) throw (BeanCoderException)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);  }