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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » JSON 




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.