tags:

views:

412

answers:

3

In my web app, I have some complex objects written with JavaScript (ie nested arrays, objects within objects withing objects, etc) and the nature of my app relies on these. I need to send all the data to PHP so that I can save it to the database. What is an efficient, easy way to send my objects to PHP? I tried JSON, but got strange errors like 'too much recursion', which I'm guessing means that my objects are too complex for it. So is there a good library of script that will do this? Thanks in advance.

EDIT:

So JSON then. I tried updating the JSON library to no avail, and I am now looking for cyclic references.

+5  A: 

JSON is the right answer.

I suspect your JSON libraries are either broken or too restrictive. Check the JSON to see if it's well-formed, and if so, find a better library that can cope with your data structure.

Consider also simplifying the data structure. It may be too complex for your own good.

Warren Young
+1  A: 

I agree- JSON is the answer. I think the error you are encountering may be part of the library you are using...

http://markmail.org/message/2d5lvmdeg2qg55qr

Mentions the same error.

I've used many complex JSON objects and never encountered that error - I'd say something else is at play.

apocalypse9
I tried what your link said, and I still got the same error, sorry.
Dan
Any chance you could post a sample of the data that is causing the error?You may also want to run the data through a validator to see if it flags anything.http://www.jsonlint.com/
apocalypse9
+2  A: 

Pretty much any JSON library will have a too much recursion error if your object has circular references. The recursion limit of the javascript implementations I've tried is well in excess of ~100 levels deep, so your object would have to be really complicated.

You'll want to detect and eliminate circular references before trying to serialise your object using any sort of library.

edit: Just tested the firefox 3.5 and it tops out at 3000 levels of recursion.

Breton