0

I want to encode and decode a circular JSON object with circular pointers.

It's not duplicate because I want to encode with circular and decode it. I need circulars objects after decode!

This is just a simple object but I have a complicated object.

var obj = { parent1: { child1: {} } }; obj.parent1.child1.parent = obj.parent1; console.log(JSON.stringify(obj)); // ERROR: Converting circular structure to JSON

I try to change pointers to address like:

obj.parent1.child1.parent = '$P$root.parent1$'; 

and when decode correct it.

But how can I detect which object is a pointer. And how can i get object address?

5
  • For parsing back to an object, you'll want to use a reviver function in JSON.parse() to restore whatever reference you used during serialisation. If that's not possible, you'll have to iterate through the parsed object and replace references Commented Sep 11, 2018 at 12:23
  • @Phil How can I detect which object is a pointer when serialize it. And how can i get pointer address? Commented Sep 11, 2018 at 12:26
  • @Phil Is this question duplicate? And why? Commented Sep 11, 2018 at 12:27
  • Yes, it is. Did you have a look at some of the answers? Many offer examples and links showing encoding and decoding options. Commented Sep 11, 2018 at 12:29
  • I found it. Thanks a lot. I thought after the answer with -11 vote is badder and don't look they. Commented Sep 11, 2018 at 12:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.