Discussion:
Patch for ticket #329
Niek Kouwenberg
2010-02-22 13:00:42 UTC
Permalink
MochiKit.Base.serializeJSON fails for objects with a 'length' property

This method used an 'is array like'-like check, instead of a strict 'is
array' check. The provided patch replaces the if-statement with the isArray
checks as implemented in Prototype.js and jQuery. This does not break
existing functionality, but prevents the errors for objects with a lenght
property. These are now correctly added to the JSON string as objects.
--
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to mochikit+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en.
Per Cederberg
2010-02-22 13:36:09 UTC
Permalink
I think the proper type check would be better written as "o instanceof Array".

Also, previously we've been very cautious with breaking backwards
compatibility. The existing code supports JSON-serialization of
array-like structures, something that wouldn't be supported by the new
code. Perhaps we could just recommended native JSON.stringify() for
most cases:

https://developer.mozilla.org/En/Using_native_JSON

So, the question is if we should switch to strict JSON in the
serializeJSON() method? Or just deprecate our version now that all
major browers support JSON.stringify(). Any opinions on the list?

Cheers,

/Per

On Mon, Feb 22, 2010 at 14:00, Niek Kouwenberg
Post by Niek Kouwenberg
MochiKit.Base.serializeJSON fails for objects with a 'length' property
This method used an 'is array like'-like check, instead of a strict 'is
array' check. The provided patch replaces the if-statement with the isArray
checks as implemented in Prototype.js and jQuery. This does not break
existing functionality, but prevents the errors for objects with a lenght
property. These are now correctly added to the JSON string as objects.
--
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/mochikit?hl=en.
--
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to mochikit+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en.
Niek Kouwenberg
2010-02-22 14:47:56 UTC
Permalink
I see.

In that case we can just try to serialize the JSON string for an array-like
object, and on failure (incorrect length property) fallback to the
object-like serialization.

A simple try-catch will fix this. I've attached a different patch which is
backward compatible and fixes this bug.
--
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to mochikit+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en.
Loading...