Package groovy.json

Enum JsonParserType

  • All Implemented Interfaces:
    Serializable, Comparable<JsonParserType>

    public enum JsonParserType
    extends Enum<JsonParserType>
    Allows selection of parser type for new new JsonSlurper.

    To enable the INDEX_OVERLAY parser do this:

                 parser = new JsonSlurper().setType(JsonParserType.INDEX_OVERLAY);
    INDEX_OVERLAY should be your parser of choice.

    CHAR_BUFFER is the parser of choice due to element of least surprise and need to mimic existing Slurper behavior as much as possible.

    Use CHARACTER_SOURCE for large file parsing.

    Use LAX if you want to enable relaxed JSON parsing, i.e., allow comments, no quote strings, etc.

    Use CHAR_BUFFER for a non-fancy but super fast parser.


    Use Cases:

    Use LAX for config files as it allows comments. Use INDEX_OVERLAY for REST calls, WebSocket messages, AJAX, inter process communication, etc. Use CHAR_BUFFER if eager parsing of ints, dates, longs, are appealing. Use CHARACTER_SOURCE if you are dealing with large JSON files over 2MB. INDEX_OVERLAY is highly tuned for object deserialization from JSON.

    • Enum Constant Detail


        public static final JsonParserType INDEX_OVERLAY
        Fastest parser, but has pointers (indexes really) to original char buffer. Care must be used if putting parse maps into a long term cache as members of map maybe index overlay objects pointing to original buffer. You can mitigate these risks by using chop and lazy chop. Chop eagerly dices up the buffer so each Value element points to a small copy of the original buffer. Lazy Chop dices up the buffer when a list get or map get is called so if an GPath expression or such is applied. You do not need chop or lazy chop if you are not putting the map into a long term cache. You do not need chop or lazy chop if you are doing object de-serialization. Recommendation is to use this for JSON buffers under 2MB.

        public static final JsonParserType CHARACTER_SOURCE
        Parser uses an abstraction that allows it to handle any size file by using a char [] windowing, built on top or Reader. This parser is slower than INDEX_OVERLAY and CHAR_BUFFER, but can handle really large files without OOM exceptions. Although slower than other groovy parsers it is as fast as many common JSON parsers. Recommendation is to use this for JSON buffers over 2MB.
      • LAX

        public static final JsonParserType LAX
        LAX mode allows keys with no quotes, keys with single quotes, strings elements in JSON with no quotes or single quotes. It also allows comments //, # and even multi-line /* comments. LAX is an INDEX_OVERLAY parser. Its speed is comparable to INDEX_OVERLAY. It is slightly slower than INDEX_OVERLAY, but faster than the other options.

        public static final JsonParserType CHAR_BUFFER
        This is a basic parser with no index overlay. It is wicked fast, but not as fast at the INDEX_OVERLAY. It should be on average the fastest known JSON parser on the JVM circa Jan 2014. But not as fast as INDEX_OVERLAY.
    • Method Detail

      • values

        public static JsonParserType[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (JsonParserType c : JsonParserType.values())
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static JsonParserType valueOf​(String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        name - the name of the enum constant to be returned.
        the enum constant with the specified name
        IllegalArgumentException - if this enum type has no constant with the specified name
        NullPointerException - if the argument is null