Class JSONPObject

java.lang.Object
com.alibaba.fastjson2.JSONPObject

public class JSONPObject extends Object
JSONPObject is used to represent JSONP (JSON with Padding) data structure.

JSONP is a technique for safely requesting data from another domain. It wraps JSON data in a function call to bypass the same-origin policy that prevents direct access to resources from different domains.

Example usage:


 // Create a JSONP object
 JSONPObject jsonp = new JSONPObject("callback");
 jsonp.addParameter(new JSONObject().fluentPut("id", 1).fluentPut("name", "test"));

 // Serialize to JSONP string
 String jsonpString = jsonp.toString(); // "callback({\"id\":1,\"name\":\"test\"})"

 // Parse from JSONP string
 JSONPObject parsed = JSON.parseObject(jsonpString, JSONPObject.class);
 
See Also:
  • Constructor Details

    • JSONPObject

      public JSONPObject()
      Default constructor for JSONPObject
    • JSONPObject

      public JSONPObject(String function)
      Constructor with function name
      Parameters:
      function - the JSONP callback function name
  • Method Details

    • getFunction

      public String getFunction()
      Gets the function name of this JSONP object
      Returns:
      the function name
    • setFunction

      public void setFunction(String function)
      Sets the function name of this JSONP object
      Parameters:
      function - the function name to set
    • getParameters

      public List<Object> getParameters()
      Gets the parameters list of this JSONP object
      Returns:
      the parameters list
    • addParameter

      public void addParameter(Object parameter)
      Adds a parameter to this JSONP object
      Parameters:
      parameter - the parameter to add
    • toString

      public String toString()
      Serializes this JSONP object to JSONP string format
      Overrides:
      toString in class Object
      Returns:
      the JSONP string representation