org.stefaniuk.json.service
Class JsonServiceUtil

java.lang.Object
  extended by org.stefaniuk.json.service.JsonServiceUtil

public class JsonServiceUtil
extends Object

JSON service utility methods.

Since:
2010/09/20
Version:
1.0.0
Author:
Daniel Stefaniuk

Constructor Summary
JsonServiceUtil()
           
 
Method Summary
static
<T> Object
fromJson(FileReader fr, Class<T> pojoClass)
          Creates Java POJO object from JSON string.
static
<T> Object
fromJson(String jsonAsString, Class<T> pojoClass)
          Creates Java POJO object from JSON string.
static org.springframework.http.ResponseEntity<String> getResponseEntityForMethodCall(BufferedOutputStream bos)
          This method sets HTTP response headers adequate for JSON-RPC method call and returns ResponseEntity to be used by Spring Framework.
static org.springframework.http.ResponseEntity<String> getResponseEntityForMethodCall(BufferedOutputStream bos, org.springframework.http.HttpStatus status)
          This method sets HTTP response headers adequate for JSON-RPC method call and returns ResponseEntity to be used by Spring Framework.
static org.springframework.http.ResponseEntity<String> getResponseEntityForServiceMap(BufferedOutputStream bos)
          This method sets HTTP response headers adequate for Service Mapping Description and returns ResponseEntity to be used by Spring Framework.
static org.springframework.http.ResponseEntity<String> handle(JsonServiceRegistry service, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Class<?> clazz)
          Handles HTTP request.
static org.springframework.http.ResponseEntity<String> handle(JsonServiceRegistry service, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object obj)
          Handles HTTP request.
static void setHeadersForMethodCall(BufferedOutputStream bos, javax.servlet.http.HttpServletResponse response)
          This method sets HTTP response headers adequate for JSON-RPC method call.
static void setHeadersForMethodCall(BufferedOutputStream bos, javax.servlet.http.HttpServletResponse response, int status)
          This method sets HTTP response headers adequate for JSON-RPC method call.
static void setHeadersForServiceMap(BufferedOutputStream bos, javax.servlet.http.HttpServletResponse response)
          This method sets HTTP response headers adequate for Service Mapping Description.
static org.codehaus.jackson.node.ArrayNode toJson(List<?> list)
          Converts a list to JSON array.
static org.codehaus.jackson.node.ObjectNode toJson(Map<?,?> map)
          Converts a map to JSON object.
static String toJson(Object pojo, boolean prettyPrint)
          Serialises Java POJO object to JSON string.
static void toJson(Object pojo, FileWriter fw, boolean prettyPrint)
          Serialises Java POJO object to JSON string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonServiceUtil

public JsonServiceUtil()
Method Detail

setHeadersForServiceMap

public static void setHeadersForServiceMap(BufferedOutputStream bos,
                                           javax.servlet.http.HttpServletResponse response)
This method sets HTTP response headers adequate for Service Mapping Description.

Parameters:
bos - Output stream
response - HTTP response object

setHeadersForMethodCall

public static void setHeadersForMethodCall(BufferedOutputStream bos,
                                           javax.servlet.http.HttpServletResponse response)
This method sets HTTP response headers adequate for JSON-RPC method call.

Parameters:
bos - Output stream
response - HTTP response object

setHeadersForMethodCall

public static void setHeadersForMethodCall(BufferedOutputStream bos,
                                           javax.servlet.http.HttpServletResponse response,
                                           int status)
This method sets HTTP response headers adequate for JSON-RPC method call.

Parameters:
bos - Output stream
response - HTTP response object
status - HTTP response code

getResponseEntityForServiceMap

public static org.springframework.http.ResponseEntity<String> getResponseEntityForServiceMap(BufferedOutputStream bos)
This method sets HTTP response headers adequate for Service Mapping Description and returns ResponseEntity to be used by Spring Framework.

Parameters:
bos - Output stream
Returns:
Returns ResponseEntity to be used by Spring Framework.

getResponseEntityForMethodCall

public static org.springframework.http.ResponseEntity<String> getResponseEntityForMethodCall(BufferedOutputStream bos)
This method sets HTTP response headers adequate for JSON-RPC method call and returns ResponseEntity to be used by Spring Framework.

Parameters:
bos - Output stream
Returns:
Returns ResponseEntity to be used by Spring Framework.

getResponseEntityForMethodCall

public static org.springframework.http.ResponseEntity<String> getResponseEntityForMethodCall(BufferedOutputStream bos,
                                                                                             org.springframework.http.HttpStatus status)
This method sets HTTP response headers adequate for JSON-RPC method call and returns ResponseEntity to be used by Spring Framework.

Parameters:
bos - Output stream
status - HTTP response code
Returns:
Returns ResponseEntity to be used by Spring Framework.

handle

public static org.springframework.http.ResponseEntity<String> handle(JsonServiceRegistry service,
                                                                     javax.servlet.http.HttpServletRequest request,
                                                                     javax.servlet.http.HttpServletResponse response,
                                                                     Class<?> clazz)
                                                              throws IOException
Handles HTTP request.

Parameters:
service - Service registry object
request - HTTP request object
response - HTTP response object
clazz - Class
Returns:
Returns ResponseEntity to be used by Spring Framework.
Throws:
IOException

handle

public static org.springframework.http.ResponseEntity<String> handle(JsonServiceRegistry service,
                                                                     javax.servlet.http.HttpServletRequest request,
                                                                     javax.servlet.http.HttpServletResponse response,
                                                                     Object obj)
                                                              throws IOException
Handles HTTP request.

Parameters:
service - Service registry object
request - HTTP request object
response - HTTP response object
obj - Already instantiated object
Returns:
Returns ResponseEntity to be used by Spring Framework.
Throws:
IOException

fromJson

public static <T> Object fromJson(String jsonAsString,
                                  Class<T> pojoClass)
                       throws org.codehaus.jackson.map.JsonMappingException,
                              org.codehaus.jackson.JsonParseException,
                              IOException
Creates Java POJO object from JSON string.

Type Parameters:
T -
Parameters:
jsonAsString - JSON string
pojoClass - POJO class
Returns:
POJO object
Throws:
org.codehaus.jackson.map.JsonMappingException
org.codehaus.jackson.JsonParseException
IOException

fromJson

public static <T> Object fromJson(FileReader fr,
                                  Class<T> pojoClass)
                       throws org.codehaus.jackson.JsonParseException,
                              IOException
Creates Java POJO object from JSON string.

Type Parameters:
T -
Parameters:
fr - File stream to read JSON string.
pojoClass - POJO class
Returns:
POJO object
Throws:
org.codehaus.jackson.JsonParseException
IOException

toJson

public static String toJson(Object pojo,
                            boolean prettyPrint)
                     throws org.codehaus.jackson.map.JsonMappingException,
                            org.codehaus.jackson.JsonGenerationException,
                            IOException
Serialises Java POJO object to JSON string.

Parameters:
pojo - POJO object
prettyPrint - JSON pretty print
Returns:
JSON string
Throws:
org.codehaus.jackson.map.JsonMappingException
org.codehaus.jackson.JsonGenerationException
IOException

toJson

public static void toJson(Object pojo,
                          FileWriter fw,
                          boolean prettyPrint)
                   throws org.codehaus.jackson.map.JsonMappingException,
                          org.codehaus.jackson.JsonGenerationException,
                          IOException
Serialises Java POJO object to JSON string.

Parameters:
pojo - POJO object
fw - File stream to write JSON string.
prettyPrint - JSON pretty print
Throws:
org.codehaus.jackson.map.JsonMappingException
org.codehaus.jackson.JsonGenerationException
IOException

toJson

public static org.codehaus.jackson.node.ArrayNode toJson(List<?> list)
Converts a list to JSON array.

Parameters:
list - List
Returns:
Returns JSON array.

toJson

public static org.codehaus.jackson.node.ObjectNode toJson(Map<?,?> map)
Converts a map to JSON object.

Parameters:
map - Map
Returns:
Returns JSON object.


Copyright © 2013. All Rights Reserved.