Ticket #6651 (closed feature: wontfix)
Add a function to pass an object, parsing JSON if needed
| Reported by: | Chainfire | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.4.3 |
| Component: | ajax | Version: | 1.4.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
Proposal function:
$.obj = function(obj) {
return (typeof obj === "string") ? $.parseJSON(obj) : obj;
}
Can be useful when using a lot of server-generated variables and such, outputted through for example PHP's json_encode. Implementing a function as follows:
$.test = function(obj) {
obj = $.obj(obj); alert(obj.foo); alert(obj.bar);
}
One simple extra call allows functions to take both obj's or JSON data directly. Especially useful if you are mixing normal and server-generated content. Using $.obj in a call directly:
$.test($.obj(...))
Now you don't need to keep track of what is JSON and what isn't.
This is of course an easy enough function for everybody to define themselves, but I think a standard API function for this would be beneficial.
Change History
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
