Tech Tip: What is a Cyclic Structure in JSON and why is it unable to be stringified?
PRODUCT: 4D | VERSION: 15.x | PLATFORM: Mac & Win
Published On: August 26, 2016
A Cyclic Structure is a structure that references itself. For example:
C_OBJECT($obj) C_TEXT($json) OB SET($obj;"o";1) OB SET($obj;"this is cyclic";$obj) |
It is important to note that this type of structure cannot be stringified. If you attempt to stringify this data structure you can expect to see an error.
For exampple, the following code ran against the structure built above would produce an error:
$json:=JSON Stringify($obj) |
The runtime error produced by the code above:
The same error can be seen when attempting to view the Cyclic Structure in the 4D Debugger:
As you can see in the screenshot above, the Cyclic Structure can be expanded infinitely. Another term for this is recursion. Meaning that if it were stringified (written out as text), the text would go on forever and eventually fill the hard drive.