Class JSONFormatter

  • Direct Known Subclasses:
    JSONCountFormatter, JSONResultFormatter, ObjectFormatter

    public class JSONFormatter
    extends Formatter
    Base class for formatters that process JSON data. The following basic structure is assumed: The result set is a single JavaScript object literal, with an optional callback provided by the user, and some result status meta-data to round it off (see formatFooter).
    Author:
    Alex Kalderimis
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String KEY_CALLBACK
      The key for the callback
      static java.lang.String KEY_HEADER_OBJS
      Things that should go into the header
      static java.lang.String KEY_INTRO
      the key for the result object.
      static java.lang.String KEY_KV_PAIRS
      A map of optional key value pairs that should go in the header of the object.
      static java.lang.String KEY_OUTRO
      The way to wrap up after the result.
      static java.lang.String KEY_QUOTE
      Whether we should quote the result item.
      static java.lang.String KEY_TIME
      The key for the execution time
      static java.util.Set<java.lang.String> RESERVED_KEYS
      keys which you aren't allowed to set.
    • Constructor Summary

      Constructors 
      Constructor Description
      JSONFormatter()
      Constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void declarePrinted()
      Signal that we have started printing results and that it isn't safe to print headers.
      protected void formatAttributes​(java.util.Map<java.lang.String,​java.lang.Object> attributes, java.lang.StringBuilder sb)
      Format the header attributes.
      java.lang.String formatFooter​(java.lang.String errorMessage, int errorCode)
      Put on the final brace, and close the call-back bracket if needed.
      java.lang.String formatHeader​(java.util.Map<java.lang.String,​java.lang.Object> attributes)
      Add the opening brace, and a call-back if any
      java.lang.String formatResult​(java.util.List<java.lang.String> resultRow)
      In normal cases a list with a single JSON string item is expected.
      • Methods inherited from class java.lang.Object

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

      • KEY_CALLBACK

        public static final java.lang.String KEY_CALLBACK
        The key for the callback
        See Also:
        Constant Field Values
      • KEY_INTRO

        public static final java.lang.String KEY_INTRO
        the key for the result object.
        See Also:
        Constant Field Values
      • KEY_OUTRO

        public static final java.lang.String KEY_OUTRO
        The way to wrap up after the result.
        See Also:
        Constant Field Values
      • KEY_QUOTE

        public static final java.lang.String KEY_QUOTE
        Whether we should quote the result item.
        See Also:
        Constant Field Values
      • KEY_HEADER_OBJS

        public static final java.lang.String KEY_HEADER_OBJS
        Things that should go into the header
        See Also:
        Constant Field Values
      • KEY_KV_PAIRS

        public static final java.lang.String KEY_KV_PAIRS
        A map of optional key value pairs that should go in the header of the object. The map should be of type Map - woe betide you if you violate this stern imprecation.
        See Also:
        Constant Field Values
      • KEY_TIME

        public static final java.lang.String KEY_TIME
        The key for the execution time
        See Also:
        Constant Field Values
      • RESERVED_KEYS

        public static final java.util.Set<java.lang.String> RESERVED_KEYS
        keys which you aren't allowed to set.
    • Constructor Detail

      • JSONFormatter

        public JSONFormatter()
        Constructor
    • Method Detail

      • formatHeader

        public java.lang.String formatHeader​(java.util.Map<java.lang.String,​java.lang.Object> attributes)
        Add the opening brace, and a call-back if any
        Specified by:
        formatHeader in class Formatter
        Parameters:
        attributes - the attributes passed in from the containing output
        Returns:
        the header
        See Also:
        Formatter.formatHeader(java.util.Map)
      • formatAttributes

        protected void formatAttributes​(java.util.Map<java.lang.String,​java.lang.Object> attributes,
                                        java.lang.StringBuilder sb)
        Format the header attributes.
        Parameters:
        attributes - The header attributes.
        sb - Where to format them to.
      • formatResult

        public java.lang.String formatResult​(java.util.List<java.lang.String> resultRow)
        In normal cases a list with a single JSON string item is expected. But just in case, this formatter will simply join any strings it gets given, delimiting with a comma. It is the responsibility of whoever is feeding me these lines to add any necessary commas between them.
        Specified by:
        formatResult in class Formatter
        Parameters:
        resultRow - the row as a list of strings
        Returns:
        A formatted result line, or the empty string if the row is empty
        See Also:
        Formatter.formatResult(java.util.List)
      • declarePrinted

        protected void declarePrinted()
        Signal that we have started printing results and that it isn't safe to print headers.
      • formatFooter

        public java.lang.String formatFooter​(java.lang.String errorMessage,
                                             int errorCode)
        Put on the final brace, and close the call-back bracket if needed. If an error has been reported, format that nicely, escaping problematic JavaScript characters appropriately in the message portion.
        Specified by:
        formatFooter in class Formatter
        Parameters:
        errorMessage - The message reporting the problem encountered in processing this request, or null if there was none
        errorCode - The status code for the request (200 on success)
        Returns:
        The formatted footer string.
        See Also:
        org.intermine.webservice.server.output.Formatter#formatFooter()