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?

morteza ataiy
  • 541
  • 4
  • 12
  • For parsing back to an object, you'll want to use a _reviver_ function in [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Syntax) 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 – Phil Sep 11 '18 at 12:23
  • @Phil How can I detect which object is a pointer when serialize it. And how can i get pointer address? – morteza ataiy Sep 11 '18 at 12:26
  • @Phil Is this question duplicate? And why? – morteza ataiy Sep 11 '18 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. – Phil Sep 11 '18 at 12:29
  • I found it. Thanks a lot. I thought after the answer with -11 vote is badder and don't look they. – morteza ataiy Sep 11 '18 at 12:34

0 Answers0