Class JsonHelper

java.lang.Object
com.onenetwork.platform.integ.rest.json.JsonHelper

public class JsonHelper
extends java.lang.Object
A helper class for working with JSON data. maybe we can introduce a new Interface like JSONable, which has two methods: JSONObject toJSON(JSONable); JSONable fromJSON(JSONObject); then the Address/Attachment/etc implementations can implement this interface, and JsonHelper can provide two extra functions: JSONArray toJSONArray(List) List fromJSONArray(JSONArray) for list<-->array conversions
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.lang.String DATE_FORMAT_STR  
    static java.lang.String DATE_FORMAT_TZ_OFFSET_STR  
    static java.lang.String DATE_FORMAT_TZ_STR  
    static java.lang.String DATE_RANGE_END_KEY
    constant to define date range end calendar value key in JSONObject representation.
    static java.lang.String DATE_RANGE_START_KEY
    constant to define date range start calendar value in JSONObject representation.
    static java.lang.String LOCAL_DATE_FORMAT_STR  
  • Constructor Summary

    Constructors 
    Constructor Description
    JsonHelper()  
  • Method Summary

    Modifier and Type Method Description
    static org.json.JSONObject addressToJSON​(Address address)
    Converts the Address object to a JSONObject
    static java.util.List<java.lang.Object> convertJsonArrayToList​(org.json.JSONArray array)
    Returns a List containing all elements of the JSONArray, or an empty List of JSONArray is null
    static java.util.List<java.lang.Long> convertJsonArrayToListOfLong​(org.json.JSONArray array)  
    static java.util.List<java.lang.String> convertJsonArrayToListOfString​(org.json.JSONArray array)  
    static org.json.JSONArray convertListToJsonArray​(java.util.List<?> list)
    Returns a JSONArray constructed with the provided List, or an empty JSONArray if null
    static java.lang.Object convertToJson​(java.lang.Object object)
    Converts a Collection/Array to a JSONArray and a Map to a JSONObject.
    static void deleteATagFromJsonValues​(org.json.JSONObject jsonObject, java.lang.String... propNames)
    Deprecated. 
    static void deleteJsonValues​(org.json.JSONObject jsonObject, java.lang.String... propNames)
    Sets the supplied properties in the supplied json object to the blank string ""
    static boolean equals​(org.json.JSONArray jsonArray1, org.json.JSONArray jsonArray2)  
    static boolean equals​(org.json.JSONObject jsonObject1, org.json.JSONObject jsonObject2)  
    static boolean equals​(org.json.JSONObject jsonObject1, org.json.JSONObject jsonObject2, boolean compareArrOrder)  
    static org.json.JSONObject formatAttachment​(ModelAttachment attachment)  
    static org.json.JSONArray formatAttachments​(java.util.List<? extends ModelAttachment> attachments)  
    static org.json.JSONObject formatDate​(java.util.Calendar cal)
    Deprecated.
    static org.json.JSONObject formatDate​(java.util.Calendar cal, java.text.DateFormat dateFormat, java.util.Locale locale)
    Similar to formatDate(Calendar, Locale), but takes some additional parameters.
    static org.json.JSONObject formatDate​(java.util.Calendar cal, java.text.DateFormat dateFormat, java.util.Locale locale, boolean isLocalDate)
    Similar to formatDate(Calendar, Locale), but takes some additional parameters.
    static org.json.JSONObject formatDate​(java.util.Calendar cal, java.util.Locale locale)
    Formats a date as a JSONObject.
    static org.json.JSONObject formatDate​(java.util.Date date, PlatformUserProfile profile)
    Similar to formatDate(Calendar, Locale), but takes Date as input.
    static org.json.JSONObject formatDateRange​(DateRange dateRange, java.util.Locale locale)  
    static java.text.DateFormat getDateFormat()  
    static java.text.DateFormat getDateFormat​(java.util.Locale locale)  
    static java.text.DateFormat getDateFormatWithTz()  
    static java.text.DateFormat getDateFormatWithTz​(java.util.Locale locale)  
    static java.text.DateFormat getDateFormatWithTzOffset()  
    static java.text.DateFormat getDateFormatWithTzOffset​(java.util.Locale locale)  
    static java.text.DateFormat getLocalDateFormat()  
    static java.lang.String[] getTimezoneCodesForID​(java.lang.String tzId, java.util.Locale locale)
    Returns the standard and daylight saving time timezone codes for a given timezone id.
    static java.lang.String[] getTimezoneOffsetsForID​(java.lang.String tzId)
    Returns the standard and daylight saving time timezone offsets for a given timezone id.
    static java.lang.String getValFromJson​(java.lang.String key, org.json.JSONObject jsonObj)  
    static boolean isJsonArray​(java.lang.Object value)
    Return true if value is String which can be parsed as JSONArray
    static boolean isJsonObject​(java.lang.Object value)
    Return true if value is String which can be parsed as JSONObject
    static boolean isJsonObject​(java.lang.String string)
    Return true if value is String which can be parsed as JSONObject
    static org.json.JSONArray mergeJsonArrays​(org.json.JSONArray destArray, org.json.JSONArray... srcArrays)
    Merge two or more JSONArrays.
    static org.json.JSONObject mergeJSONObject​(org.json.JSONObject original, org.json.JSONObject extension)
    Overrides or extends original with extension
    static Address parseAddress​(org.json.JSONObject addrObj)
    Parses an address which is encoded in a JSONObject.
    static AttachedNote parseAttachedNote​(org.json.JSONObject attachedNoteObj)
    Parses a JSON encoded AttachedNote
    static AttachedNotes parseAttachedNotes​(org.json.JSONObject attachedNotesObj)
    Parses a JSON encoded AttachedNotes.
    static ModelAttachment parseAttachmentFromJson​(org.json.JSONObject json)  
    static java.util.List<ModelAttachment> parseAttachmentsFromJson​(org.json.JSONArray meta)  
    static java.util.Calendar parseDate​(org.json.JSONObject dateObj)
    Deprecated.
    static java.util.Calendar parseDate​(org.json.JSONObject dateObj, java.util.Locale locale)
    Parses a date which is encoded in a JSONObject.
    static java.util.Calendar[] parseDateRange​(org.json.JSONObject dateRange)
    Deprecated.
    static java.util.Calendar[] parseDateRange​(org.json.JSONObject dateRange, java.util.Locale locale)
    Parses a JSON encoded date range where the two dates are JSON encoded dates under the from and to properties of the range.
    static org.json.JSONObject removeEmptyProps​(org.json.JSONObject json)
    Returns a new JSON which is a copy of the original, but where any property containing the empty string as a value has been removed entirely

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • formatAttachment

      public static org.json.JSONObject formatAttachment​(ModelAttachment attachment)
    • formatAttachments

      public static org.json.JSONArray formatAttachments​(java.util.List<? extends ModelAttachment> attachments)
    • parseAttachmentFromJson

      public static ModelAttachment parseAttachmentFromJson​(org.json.JSONObject json)
    • parseAttachmentsFromJson

      public static java.util.List<ModelAttachment> parseAttachmentsFromJson​(org.json.JSONArray meta)
    • addressToJSON

      public static org.json.JSONObject addressToJSON​(Address address)
      Converts the Address object to a JSONObject
      Parameters:
      address -
      Returns:
      JSONObject
    • parseAddress

      public static Address parseAddress​(org.json.JSONObject addrObj)
      Parses an address which is encoded in a JSONObject.
    • parseDate

      public static java.util.Calendar parseDate​(org.json.JSONObject dateObj) throws java.text.ParseException
      Deprecated.
      Throws:
      java.text.ParseException
    • parseDate

      public static java.util.Calendar parseDate​(org.json.JSONObject dateObj, java.util.Locale locale) throws java.text.ParseException
      Parses a date which is encoded in a JSONObject. See formatDate(Calendar, Locale) for the expected format.
      Parameters:
      dateObj - The date json object
      locale - The locale used to parse the date
      Returns:
      The Calendar which the dateObj represents or null if the calendar is null
      Throws:
      java.text.ParseException
    • getTimezoneCodesForID

      public static java.lang.String[] getTimezoneCodesForID​(java.lang.String tzId, java.util.Locale locale)
      Returns the standard and daylight saving time timezone codes for a given timezone id. For example, with input "America/Chicago" it would return an array: [ "CST", "CDT" ].
      Parameters:
      tzId -
      locale -
      Returns:
    • getTimezoneOffsetsForID

      public static java.lang.String[] getTimezoneOffsetsForID​(java.lang.String tzId)
      Returns the standard and daylight saving time timezone offsets for a given timezone id. For example, with input "America/Chicago" it would return an array: [ "-06:00", "-05:00" ].
      Parameters:
      tzId -
      locale -
      Returns:
    • formatDate

      public static org.json.JSONObject formatDate​(java.util.Calendar cal)
      Deprecated.
    • formatDate

      public static org.json.JSONObject formatDate​(java.util.Date date, PlatformUserProfile profile)
      Similar to formatDate(Calendar, Locale), but takes Date as input. Formats the Date in the TimeZone of the given PlatformUserProfile.
    • formatDate

      public static org.json.JSONObject formatDate​(java.util.Calendar cal, java.util.Locale locale)
      Formats a date as a JSONObject. The resulting object will have a date property which is a date string in ISO format, a tzId property which is the timezone id of the date, and a tzCode property which will be the shortened name of the timezone (ex: EST vs EDT). The tzCode will also be localized using the provided locale.
      Parameters:
      cal - The calendar to instance be formatted
      locale - The locale used to format the date
      Returns:
      The JSONObject representation, or null if the Calendar is null
    • formatDate

      public static org.json.JSONObject formatDate​(java.util.Calendar cal, java.text.DateFormat dateFormat, java.util.Locale locale)
      Similar to formatDate(Calendar, Locale), but takes some additional parameters.
      Parameters:
      cal - The calendar instance to be formatted
      dateFormat - A DateFormat to use; if not provided, the ISO format will be used
      locale - The locale used to format the date
      Returns:
      The JSONObject representation, or null if the Calendar is null
    • formatDate

      public static org.json.JSONObject formatDate​(java.util.Calendar cal, java.text.DateFormat dateFormat, java.util.Locale locale, boolean isLocalDate)
      Similar to formatDate(Calendar, Locale), but takes some additional parameters.
      Parameters:
      cal - The calendar instance to be formatted
      dateFormat - A DateFormat to use; if not provided, the ISO format will be used
      locale - The locale used to format the date
      isLocalDate - true if the date is a local date, meaning it will only represent a month, day and year and have no notion of time or time zone.
      Returns:
      The JSONObject representation, or null if the Calendar is null
    • formatDateRange

      public static org.json.JSONObject formatDateRange​(DateRange dateRange, java.util.Locale locale)
    • parseAttachedNotes

      public static AttachedNotes parseAttachedNotes​(org.json.JSONObject attachedNotesObj) throws org.json.JSONException
      Parses a JSON encoded AttachedNotes.
      Parameters:
      attachedNotesObj -
      Returns:
      Throws:
      org.json.JSONException
    • parseAttachedNote

      public static AttachedNote parseAttachedNote​(org.json.JSONObject attachedNoteObj) throws org.json.JSONException
      Parses a JSON encoded AttachedNote
      Parameters:
      attachedNoteObj -
      Returns:
      Throws:
      org.json.JSONException
    • parseDateRange

      public static java.util.Calendar[] parseDateRange​(org.json.JSONObject dateRange) throws java.text.ParseException
      Deprecated.
      Throws:
      java.text.ParseException
    • parseDateRange

      public static java.util.Calendar[] parseDateRange​(org.json.JSONObject dateRange, java.util.Locale locale) throws java.text.ParseException
      Parses a JSON encoded date range where the two dates are JSON encoded dates under the from and to properties of the range. See formatDate(Calendar, Locale) for the expected format of the objects.
      Parameters:
      dateRange - The JSON encoded date range
      locale - The locale used to parse the dates
      Returns:
      An array of calendars. Each calendar can be null.
      Throws:
      java.text.ParseException
    • getDateFormat

      public static java.text.DateFormat getDateFormat()
    • getLocalDateFormat

      public static java.text.DateFormat getLocalDateFormat()
    • getDateFormat

      public static java.text.DateFormat getDateFormat​(java.util.Locale locale)
    • getDateFormatWithTz

      public static java.text.DateFormat getDateFormatWithTz()
    • getDateFormatWithTz

      public static java.text.DateFormat getDateFormatWithTz​(java.util.Locale locale)
    • getDateFormatWithTzOffset

      public static java.text.DateFormat getDateFormatWithTzOffset()
    • getDateFormatWithTzOffset

      public static java.text.DateFormat getDateFormatWithTzOffset​(java.util.Locale locale)
    • equals

      public static boolean equals​(org.json.JSONArray jsonArray1, org.json.JSONArray jsonArray2)
      Parameters:
      jsonArray1 -
      jsonArray2 -
      Returns:
      True if the two arrays are equal.
    • equals

      public static boolean equals​(org.json.JSONObject jsonObject1, org.json.JSONObject jsonObject2)
      Parameters:
      jsonObject1 -
      jsonObject2 -
      Returns:
      True if the two objects are equal.
    • equals

      public static boolean equals​(org.json.JSONObject jsonObject1, org.json.JSONObject jsonObject2, boolean compareArrOrder)
      Parameters:
      jsonObject1 -
      jsonObject2 -
      compareArrElementsOrder - if false then elements in array are sorted before comparison
      Returns:
      True if the two objects are equal.
    • mergeJSONObject

      public static org.json.JSONObject mergeJSONObject​(org.json.JSONObject original, org.json.JSONObject extension)
      Overrides or extends original with extension
      Parameters:
      original -
      extension -
      Returns:
    • mergeJsonArrays

      public static org.json.JSONArray mergeJsonArrays​(org.json.JSONArray destArray, org.json.JSONArray... srcArrays)
      Merge two or more JSONArrays. The srcArrays JSONArrays will each be appended to the destArray JSONArray, and then destArray will be returned. The srcArrays will not be modified.
      Parameters:
      destArray -
      srcArrays -
      Returns:
      destArray
    • deleteJsonValues

      public static void deleteJsonValues​(org.json.JSONObject jsonObject, java.lang.String... propNames)
      Sets the supplied properties in the supplied json object to the blank string ""
      Parameters:
      jsonObject -
      propNames -
    • deleteATagFromJsonValues

      public static void deleteATagFromJsonValues​(org.json.JSONObject jsonObject, java.lang.String... propNames)
      Deprecated.
      Removes "<" and ">" characters from the selected properties from the supplied JSONObject
      Parameters:
      jsonObject -
      propNames -
    • convertListToJsonArray

      public static org.json.JSONArray convertListToJsonArray​(java.util.List<?> list)
      Returns a JSONArray constructed with the provided List, or an empty JSONArray if null
    • convertJsonArrayToList

      public static java.util.List<java.lang.Object> convertJsonArrayToList​(org.json.JSONArray array)
      Returns a List containing all elements of the JSONArray, or an empty List of JSONArray is null
      Parameters:
      array -
    • convertJsonArrayToListOfString

      public static java.util.List<java.lang.String> convertJsonArrayToListOfString​(org.json.JSONArray array)
    • convertJsonArrayToListOfLong

      public static java.util.List<java.lang.Long> convertJsonArrayToListOfLong​(org.json.JSONArray array)
    • convertToJson

      public static java.lang.Object convertToJson​(java.lang.Object object)
      Converts a Collection/Array to a JSONArray and a Map to a JSONObject. This call is recursive so it can convert nested objects and arrays. Note: This logic is mostly copied from the JSONArray and JSONObject classes, as they have constructor variants that perform this conversion from Collection/Array and Map implicitly; this implicit conversion has been disabled for the JSONArray and JSONObject classes used by ONE.
      Parameters:
      object -
      Returns:
    • getValFromJson

      public static java.lang.String getValFromJson​(java.lang.String key, org.json.JSONObject jsonObj)
    • isJsonArray

      public static boolean isJsonArray​(java.lang.Object value)
      Return true if value is String which can be parsed as JSONArray
      Parameters:
      value -
      Returns:
    • isJsonObject

      public static boolean isJsonObject​(java.lang.String string)
      Return true if value is String which can be parsed as JSONObject
      Parameters:
      string -
      Returns:
    • removeEmptyProps

      public static org.json.JSONObject removeEmptyProps​(org.json.JSONObject json)
      Returns a new JSON which is a copy of the original, but where any property containing the empty string as a value has been removed entirely
    • isJsonObject

      public static boolean isJsonObject​(java.lang.Object value)
      Return true if value is String which can be parsed as JSONObject
      Parameters:
      valid -
      Returns: