Outputing data in JSON format in response to a JSONP request : JSON « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » JSONScreenshots 
Outputing data in JSON format in response to a JSONP request
 

/*
 * This file is part of the AusStage Utilities Package
 *
 * The AusStage Utilities Package is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * The AusStage Utilities Package is distributed in the hope that it will 
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the AusStage Utilities Package.  
 * If not, see <http://www.gnu.org/licenses/>.
*/

//package au.edu.ausstage.utils;

/**
 * A class of methods useful when outputing data in JSON format in response to a JSONP request
 */
public class JSONPManager {

  /**
   * A convenience method to wrap a JSON string in a callback function name 
   * used to support JSONP requests.
   * More information: http://en.wikipedia.org/wiki/JSON#JSONP
   *
   @param json     the json string
   @param callback the name of the callback method
   */
  public static String wrapJSON(String json, String callback) {
    // could do other more interesting things here, but for the moment let's keep it simple  
    return callback + "(" + json + ");";  
  
  // end the wrapJSON method
  
  /**
   * A convenience method to wrap a JSON string in a callback function name
   * used to support JSONP requests.
   * In this version the wrapped JSON is sent directly to the user via the PrintWriter
   * More information: http://en.wikipedia.org/wiki/JSON#JSONP
   *
   @param json     the json string
   @param callback the name of the callback method
   @param writer   the PrintWriter to use to output the JSONP
   */
  public static void wrapJSON(String json, String callback, java.io.PrintWriter writer) {
    // could do other more interesting things here, but for the moment let's keep it simple  
    writer.print(callback + "(" + json + ");");  
  
  // end the wrapJSON method
  
// end class definition

   
  
Related examples in the same category
1.JSON Parser
2.String node for JSON
3.Create Object from JSON string
4.JSON tokenizer.It makes tokens for parsing json data.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.