Class ParseSupport

java.lang.Object
org.curioswitch.common.protobuf.json.ParseSupport

public final class ParseSupport extends Object
Static methods for parsing various protobuf types. Parsing code, including generated bytecode, calls these methods to actually read and normalize data from input. Many of these methods are only called from generated bytecode, but it is still worth extracting into static methods to minimize the amount of generated bytecode managed and optimized by the VM and reduce the complexity of code generation.

All values may be quoted, and integer values may be represented by the equivalent floating point value. Numeric values are all checked to be in their valid range or throw an InvalidProtocolBufferException.

  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    checkArrayEnd(com.fasterxml.jackson.core.JsonParser parser)
    Returns whether the current token is ']'.
    static boolean
    checkNull(com.fasterxml.jackson.core.JsonParser parser)
    Returns whether the current token is 'null'.
    static boolean
    checkObjectEnd(com.fasterxml.jackson.core.JsonToken token)
    Returns whether the JsonToken is '}'.
    static void
    checkRecursionLimit(int currentDepth)
    Checks whether the currentDepth of nested message parsing is higher than the limit, and throws InvalidProtocolBufferException if so.
    static int
    Returns the default value for an enum if it was read as an unknown, for singular fields.
    static void
    parseArrayStart(com.fasterxml.jackson.core.JsonParser parser)
    Checks whether the current token is '[' and advances past it.
    static boolean
    parseBool(com.fasterxml.jackson.core.JsonParser parser)
    Parsers a bool value out of the input.
    static com.google.protobuf.ByteString
    parseBytes(com.fasterxml.jackson.core.JsonParser parser)
    Parsers a bytes value out of the input.
    static double
    parseDouble(com.fasterxml.jackson.core.JsonParser parser)
    Parsers a double value out of the input.
    static int
    parseEnum(com.fasterxml.jackson.core.JsonParser parser, com.google.protobuf.Descriptors.EnumDescriptor descriptor, boolean ignoringUnknownFields)
    Parsers an enum value out of the input.
    static float
    parseFloat(com.fasterxml.jackson.core.JsonParser parser)
    Parsers a float value out of the input.
    static int
    parseInt32(com.fasterxml.jackson.core.JsonParser parser)
    Parsers an int32 value out of the input.
    static long
    parseInt64(com.fasterxml.jackson.core.JsonParser parser)
    Parsers an int64 value out of the input.
    static <T extends com.google.protobuf.Message>
    T
    parseMessage(com.fasterxml.jackson.core.JsonParser parser, TypeSpecificMarshaller<T> marshaller, int currentDepth)
    Parsers a Message value out of the input.
    static void
    parseObjectStart(com.fasterxml.jackson.core.JsonParser parser)
    Checks the current token is '{' and advances past it.
    static String
    parseString(com.fasterxml.jackson.core.JsonParser parser)
    Parsers a string value out of the input.
    static int
    parseUInt32(com.fasterxml.jackson.core.JsonParser parser)
    Parsers a uint32 value out of the input.
    static long
    parseUInt64(com.fasterxml.jackson.core.JsonParser parser)
    Parsers a uint64 value out of the input.
    static int
    throwIfFieldAlreadyWritten(int setFieldsBits, int fieldBitMask, String fullName)
    Checks the field presence of the field with number fieldNumber and variableName fullName.
    static void
    throwIfOneofAlreadyWritten(com.fasterxml.jackson.core.JsonParser parser, Object oneofCase, String fieldName, boolean ignoreNull)
    Checks whether the oneof whose oneofCase has already been set.
    static void
    throwIfRepeatedValueNull(com.fasterxml.jackson.core.JsonParser parser)
    Throws an exception if the current token is 'null'.
    static void
    throwIfUnknownField(String fieldName, String messageName)
    Throws an InvalidProtocolBufferException indicating the field with variableName fieldName is not part of Message with variableName messageName.

    Methods inherited from class java.lang.Object

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

    • parseObjectStart

      public static void parseObjectStart(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Checks the current token is '{' and advances past it. For parsing the beginning of JSON objects (Messages and Maps).
      Throws:
      IOException
    • checkObjectEnd

      public static boolean checkObjectEnd(com.fasterxml.jackson.core.JsonToken token)
      Returns whether the JsonToken is '}'. This is the terminating condition when iterating over fields in a JSON object. Does not advance the token as if it's not the end, it means it's a field and needs to be parsed, not skipped.
    • parseArrayStart

      public static void parseArrayStart(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Checks whether the current token is '[' and advances past it.
      Throws:
      IOException
    • checkArrayEnd

      public static boolean checkArrayEnd(com.fasterxml.jackson.core.JsonParser parser)
      Returns whether the current token is ']'. This is the terminating condition when iterating over elements in a JSON array. Does not advance the token as if it's not the end, it means it's an element and needs to be parsed, not skipped.
    • checkNull

      public static boolean checkNull(com.fasterxml.jackson.core.JsonParser parser)
      Returns whether the current token is 'null'. This is used to skip over fields that are set to null, which we treat as defaults.
    • throwIfRepeatedValueNull

      public static void throwIfRepeatedValueNull(com.fasterxml.jackson.core.JsonParser parser) throws com.google.protobuf.InvalidProtocolBufferException
      Throws an exception if the current token is 'null'. This is used to prevent repeated elements from being set to 'null', which is not allowed.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException
    • parseInt32

      public static int parseInt32(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Parsers an int32 value out of the input.
      Throws:
      IOException
    • parseInt64

      public static long parseInt64(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Parsers an int64 value out of the input.
      Throws:
      IOException
    • parseUInt32

      public static int parseUInt32(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Parsers a uint32 value out of the input.
      Throws:
      IOException
    • parseUInt64

      public static long parseUInt64(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Parsers a uint64 value out of the input.
      Throws:
      IOException
    • parseBool

      public static boolean parseBool(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Parsers a bool value out of the input.
      Throws:
      IOException
    • parseFloat

      public static float parseFloat(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Parsers a float value out of the input.
      Throws:
      IOException
    • parseDouble

      public static double parseDouble(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Parsers a double value out of the input.
      Throws:
      IOException
    • parseString

      public static String parseString(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Parsers a string value out of the input.
      Throws:
      IOException
    • parseBytes

      public static com.google.protobuf.ByteString parseBytes(com.fasterxml.jackson.core.JsonParser parser) throws IOException
      Parsers a bytes value out of the input.
      Throws:
      IOException
    • parseEnum

      public static int parseEnum(com.fasterxml.jackson.core.JsonParser parser, com.google.protobuf.Descriptors.EnumDescriptor descriptor, boolean ignoringUnknownFields) throws IOException
      Parsers an enum value out of the input. Supports both numeric and string representations.
      Throws:
      IOException
    • mapUnknownEnumValue

      public static int mapUnknownEnumValue(int value)
      Returns the default value for an enum if it was read as an unknown, for singular fields.
    • parseMessage

      public static <T extends com.google.protobuf.Message> T parseMessage(com.fasterxml.jackson.core.JsonParser parser, TypeSpecificMarshaller<T> marshaller, int currentDepth) throws IOException
      Parsers a Message value out of the input.
      Throws:
      IOException
    • throwIfFieldAlreadyWritten

      public static int throwIfFieldAlreadyWritten(int setFieldsBits, int fieldBitMask, String fullName) throws com.google.protobuf.InvalidProtocolBufferException
      Checks the field presence of the field with number fieldNumber and variableName fullName. If the field has already been set, an InvalidProtocolBufferException is thrown.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException
    • throwIfOneofAlreadyWritten

      public static void throwIfOneofAlreadyWritten(com.fasterxml.jackson.core.JsonParser parser, Object oneofCase, String fieldName, boolean ignoreNull) throws com.google.protobuf.InvalidProtocolBufferException
      Checks whether the oneof whose oneofCase has already been set. If so, an InvalidProtocolBufferException is thrown.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException
    • throwIfUnknownField

      public static void throwIfUnknownField(String fieldName, String messageName) throws com.google.protobuf.InvalidProtocolBufferException
      Throws an InvalidProtocolBufferException indicating the field with variableName fieldName is not part of Message with variableName messageName. Called from code after determining a field is unknown.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException
    • checkRecursionLimit

      public static void checkRecursionLimit(int currentDepth) throws com.google.protobuf.InvalidProtocolBufferException
      Checks whether the currentDepth of nested message parsing is higher than the limit, and throws InvalidProtocolBufferException if so. This is used to prevent stack exhaustion attacks with extremely nested recursive messages.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException