Bug Tracker

Opened 9 years ago

Closed 9 years ago

#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:
Blocked by: Blocking:

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 (2)

comment:1 Changed 9 years ago by dmethvin

Component: unfiledajax

comment:2 Changed 9 years ago by Rick Waldron

Priority: low
Resolution: wontfix
Status: newclosed

Your proposed function $.obj appears to wrap $.parseJSON - which is just a sanitizer and fallback wrapper to JSON.parse()

Note: See TracTickets for help on using tickets.