Package org.openbravo.service.json
Class JsonUtils
- java.lang.Object
-
- org.openbravo.service.json.JsonUtils
-
public class JsonUtils extends Object
Contains utility methods used in this module.- Author:
- mtaal
-
-
Constructor Summary
Constructors Constructor Description JsonUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.codehaus.jettison.json.JSONObject
buildCriteria(Map<String,String> parameters)
static String
convertExceptionToJson(Throwable throwable)
Converts an exception to its json represention.static String
convertFromXSDToJavaFormat(String dateValue)
Removes the colon in the timezone definition, seeconvertToCorrectXSDFormat(String)
.static String
convertToCorrectXSDFormat(String dateValue)
Adds a colon in the timezone part of the formatted date, see here: http://weblogs.java.net/blog/felipegaucho/archive/2009/12/06/jaxb-customization-xsddatetimestatic SimpleDateFormat
createDateFormat()
static SimpleDateFormat
createDateTimeFormat()
Note the formatted date string must be repaired in the timezone to follow the XSD format, see:convertToCorrectXSDFormat(String)
.static SimpleDateFormat
createJSTimeFormat()
static SimpleDateFormat
createTimeFormat()
static SimpleDateFormat
createTimeFormatWithoutGMTOffset()
static List<String>
getAdditionalProperties(Map<String,String> parameters)
Gets the value of theJsonConstants.ADDITIONAL_PROPERTIES_PARAMETER
in the parameters map and returns it as a list of String, if no parameter is set an empty list is returned.static String
getEmptyResult()
Returns an empty result for a fetch callstatic List<Property>
getIdentifierSet(Property property)
A utility method which retrieves the properties used for identifying an entity and its first level references.static List<Property>
getPropertiesOnPath(Entity entity, String propertyPath)
Determines the list of properties based on the property path (for example bankAccount.bank.name).static boolean
isQueryTimeout(Throwable localThrowable)
static boolean
isValueEmpty(String value)
Returns whether a JSON value is empty.static void
merge(Map<String,Object> sourceProperties, org.codehaus.jettison.json.JSONObject target)
Merges the entries of a givenMap
with the properties of a targetJSONObject
.static void
merge(org.codehaus.jettison.json.JSONArray source, org.codehaus.jettison.json.JSONArray target)
Puts all the objects of a given JSONArray into a target JSONArray.static void
merge(org.codehaus.jettison.json.JSONObject source, org.codehaus.jettison.json.JSONObject target)
Merges a givenJSONObject
with a targetJSONObject
.
-
-
-
Method Detail
-
getIdentifierSet
public static List<Property> getIdentifierSet(Property property)
A utility method which retrieves the properties used for identifying an entity and its first level references. Takes into accoun the different scenarios implemented in theIdentifierProvider.getIdentifier(Object)
method. Is used by distinct queries to get the smallest possible select clauses.
-
createDateFormat
public static SimpleDateFormat createDateFormat()
- Returns:
- a new instance of the
SimpleDateFormat
using a format of yyyy-MM-dd (xml schema date format). The date format has lenient set to true.
-
createTimeFormat
public static SimpleDateFormat createTimeFormat()
- Returns:
- a new instance of the
SimpleDateFormat
using a format of HH:MM:SS+0000. The date format has lenient set to true.
-
createTimeFormatWithoutGMTOffset
public static SimpleDateFormat createTimeFormatWithoutGMTOffset()
- Returns:
- a new instance of the
SimpleDateFormat
using a format of HH:MM:SS. The date format has lenient set to true.
-
createJSTimeFormat
public static SimpleDateFormat createJSTimeFormat()
- Returns:
- a new instance of the
SimpleDateFormat
using a format of yyyy-MM-dd'T'HH:mm:ss (see http://www.w3.org/TR/xmlschema-2/#dateTime). The date format has lenient set to true.
-
createDateTimeFormat
public static SimpleDateFormat createDateTimeFormat()
Note the formatted date string must be repaired in the timezone to follow the XSD format, see:convertToCorrectXSDFormat(String)
.- Returns:
- a new instance of the
SimpleDateFormat
using a format of yyyy-MM-dd'T'HH:mm:ss (xml schema date time format). The date format has lenient set to true.
-
convertToCorrectXSDFormat
public static String convertToCorrectXSDFormat(String dateValue)
Adds a colon in the timezone part of the formatted date, see here: http://weblogs.java.net/blog/felipegaucho/archive/2009/12/06/jaxb-customization-xsddatetime- Parameters:
dateValue
- the date value without a :, for example 2009-12-06T15:59:34+0100- Returns:
- a colon added in the timezone part: 2009-12-06T15:59:34+01:00
-
convertFromXSDToJavaFormat
public static String convertFromXSDToJavaFormat(String dateValue)
Removes the colon in the timezone definition, seeconvertToCorrectXSDFormat(String)
.
-
getAdditionalProperties
public static List<String> getAdditionalProperties(Map<String,String> parameters)
Gets the value of theJsonConstants.ADDITIONAL_PROPERTIES_PARAMETER
in the parameters map and returns it as a list of String, if no parameter is set an empty list is returned.- Parameters:
parameters
- the parameter map to search for theJsonConstants.ADDITIONAL_PROPERTIES_PARAMETER
parameter- Returns:
- the values in the
JsonConstants.ADDITIONAL_PROPERTIES_PARAMETER
parameter
-
convertExceptionToJson
public static String convertExceptionToJson(Throwable throwable)
Converts an exception to its json represention. Uses the Smartclient format for the json string, see here: RestDataSource- Parameters:
throwable
- the exception to convert to json- Returns:
- the resulting json string
-
isQueryTimeout
public static boolean isQueryTimeout(Throwable localThrowable)
-
getEmptyResult
public static String getEmptyResult()
Returns an empty result for a fetch call- Returns:
- the JSON representation of an empty result
-
getPropertiesOnPath
public static List<Property> getPropertiesOnPath(Entity entity, String propertyPath)
Determines the list of properties based on the property path (for example bankAccount.bank.name).- Parameters:
entity
- the entity to start from, the first property in the property path is a property of this entitypropertyPath
- the property path, i.e. property names separated by dots- Returns:
- the list of properties determined from the property path
-
buildCriteria
public static org.codehaus.jettison.json.JSONObject buildCriteria(Map<String,String> parameters)
-
isValueEmpty
public static boolean isValueEmpty(String value)
Returns whether a JSON value is empty. The following values are considered as empty:- An empty string
null
"null"
literal"undefined"
literal
-
merge
public static void merge(org.codehaus.jettison.json.JSONObject source, org.codehaus.jettison.json.JSONObject target) throws org.codehaus.jettison.json.JSONException
Merges a givenJSONObject
with a targetJSONObject
. If a property in the source object already exists in the target object, the following happens depending on the type of that property:
- If it is aJSONObject
, both source and target object are merged by calling this method recursively. If the property on the target object is not of typeJSONObject
, then aJSONException
will be thrown.
- If it is aMap
, the entries of the map and the target property are merged by invokingmerge(Map, JSONObject)
if the target property is aJSONObject
or by putting all the entries if the target property is aMap
. If the property on the target object is neither of typeJSONObject
orMap
, then aJSONException
will be thrown.
- It it is a JSONArray, both source and target arrays are merged by invokingmerge(JSONArray, JSONArray)
. If the property on the target object is not of typeJSONArray
, then aJSONException
will be thrown.
- For the rest of the cases, the property is overridden in the target object. This method mutates thetarget
object: after invoking this method it will contain the result of merging the properties of the two given objects.- Parameters:
source
- The source objecttarget
- The object where the properties are merged. It is mutated by this method.- Throws:
org.codehaus.jettison.json.JSONException
- in case the merge operation cannot be completed
-
merge
public static void merge(Map<String,Object> sourceProperties, org.codehaus.jettison.json.JSONObject target) throws org.codehaus.jettison.json.JSONException
Merges the entries of a givenMap
with the properties of a targetJSONObject
. Seemerge(JSONObject, JSONObject)
- Throws:
org.codehaus.jettison.json.JSONException
-
merge
public static void merge(org.codehaus.jettison.json.JSONArray source, org.codehaus.jettison.json.JSONArray target) throws org.codehaus.jettison.json.JSONException
Puts all the objects of a given JSONArray into a target JSONArray.- Parameters:
source
- The source arraytarget
- The array where the elements of the source array are put. Note that this method modifies the contents of this array by adding on it the elements of the source array.- Throws:
org.codehaus.jettison.json.JSONException
- in case the merge operation cannot be completed
-
-