Bug Tracker

Modify

Ticket #6651 (closed feature: wontfix)

Opened 3 years ago

Last modified 3 years ago

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

comment:1 Changed 3 years ago by dmethvin

  • Component changed from unfiled to ajax

comment:2 Changed 3 years ago by rwaldron

  • Priority set to low
  • Status changed from new to closed
  • Resolution set to wontfix

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

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.