Class EvalMinifier


  • public final class EvalMinifier
    extends Object
    Given a string of valid JSON that is going to be parsed via Javascript's eval builtin, tries to reduce the number of bytes sent over the wire by turning it into a Javascript expression that pools constants.
    • Constructor Detail

      • EvalMinifier

        public EvalMinifier()
    • Method Detail

      • minify

        public static String minify​(String jsonish)
        Renders JSON-like content safe for use with Javascript eval.

        The output is a Javascript expression, not a statement, so if it contains an object ({properties}) then it still needs to be wrapped in parentheses before being passed to eval as via eval('(' + s + ')') or eval('0,' + s).

        Parameters:
        jsonish - a string of JSON-like content as defined by JsonSanitizer.
        Returns:
        a valid Javascript expression that has no free variables and whose execution will have no side-effects, and which can be embedded safely in an HTML <script></script> element or inside an XML <![CDATA[...]]> section.
      • minify

        public static String minify​(String jsonish,
                                    int maximumNestingDepth)
        Same as minify(String), but allows to set custom maximum nesting depth.
        Parameters:
        jsonish - a string of JSON-like content as defined by JsonSanitizer.
        maximumNestingDepth - the maximum nesting depth for the JsonSanitizer
        Returns:
        see minify(String)