webhook / webhook-generate

Grunt Static File Generator
MIT License
21 stars 28 forks source link

json filter fails to serialize any content with a relationship #31

Open snide opened 9 years ago

snide commented 9 years ago

Originally reported by asprouse in https://github.com/webhook/webhook/issues/47

According to the documentation using Webhook it should be possible generate quick static JSON endpoints. This pattern works like a charm until a relationship is added and then the filter will cause the following error:

TypeError: Converting circular structure to JSON

This problem could be avoided by creating a de-cycle filter or option on the existing json filter. There is an existing public domain de-cycle algorithm written by Crockford which could be used. This algorithm replaces already seen objects with a refrence object containing a JSONPath which can be used by the consumer to reestablish the circular references after deserialization.

The issue with the decycle filter is that it will leave in other potentially unwanted objects in the reverse relation. A the solution to this would be to not serialize reverse relations. Figuring this out in the template layer might be a bit tricky without attaching metadata to the object to indicate the reverse relationship. A potentially simpler solution would be to assume that any property with a circular reference is indeed a reverse relation. Limiting the depth of recursion while serializing might be another option.

Maybe I am missing something, since this functionality is a listed in the "common pattern" section of the documentation, but if I am not this should be supported without having to resort to manually serializing in swig.