Class ParseSupport
java.lang.Object
org.curioswitch.common.protobuf.json.ParseSupport
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 TypeMethodDescriptionstatic booleancheckArrayEnd(com.fasterxml.jackson.core.JsonParser parser) Returns whether the current token is ']'.static booleancheckNull(com.fasterxml.jackson.core.JsonParser parser) Returns whether the current token is 'null'.static booleancheckObjectEnd(com.fasterxml.jackson.core.JsonToken token) Returns whether theJsonTokenis '}'.static voidcheckRecursionLimit(int currentDepth) Checks whether thecurrentDepthof nested message parsing is higher than the limit, and throwsInvalidProtocolBufferExceptionif so.static intmapUnknownEnumValue(int value) Returns the default value for an enum if it was read as an unknown, for singular fields.static voidparseArrayStart(com.fasterxml.jackson.core.JsonParser parser) Checks whether the current token is '[' and advances past it.static booleanparseBool(com.fasterxml.jackson.core.JsonParser parser) Parsers a bool value out of the input.static com.google.protobuf.ByteStringparseBytes(com.fasterxml.jackson.core.JsonParser parser) Parsers a bytes value out of the input.static doubleparseDouble(com.fasterxml.jackson.core.JsonParser parser) Parsers a double value out of the input.static intparseEnum(com.fasterxml.jackson.core.JsonParser parser, com.google.protobuf.Descriptors.EnumDescriptor descriptor, boolean ignoringUnknownFields) Parsers an enum value out of the input.static floatparseFloat(com.fasterxml.jackson.core.JsonParser parser) Parsers a float value out of the input.static intparseInt32(com.fasterxml.jackson.core.JsonParser parser) Parsers an int32 value out of the input.static longparseInt64(com.fasterxml.jackson.core.JsonParser parser) Parsers an int64 value out of the input.static <T extends com.google.protobuf.Message>
TparseMessage(com.fasterxml.jackson.core.JsonParser parser, TypeSpecificMarshaller<T> marshaller, int currentDepth) Parsers aMessagevalue out of the input.static voidparseObjectStart(com.fasterxml.jackson.core.JsonParser parser) Checks the current token is '{' and advances past it.static StringparseString(com.fasterxml.jackson.core.JsonParser parser) Parsers a string value out of the input.static intparseUInt32(com.fasterxml.jackson.core.JsonParser parser) Parsers a uint32 value out of the input.static longparseUInt64(com.fasterxml.jackson.core.JsonParser parser) Parsers a uint64 value out of the input.static intthrowIfFieldAlreadyWritten(int setFieldsBits, int fieldBitMask, String fullName) Checks the field presence of the field with numberfieldNumberand variableNamefullName.static voidthrowIfOneofAlreadyWritten(com.fasterxml.jackson.core.JsonParser parser, Object oneofCase, String fieldName, boolean ignoreNull) Checks whether the oneof whoseoneofCasehas already been set.static voidthrowIfRepeatedValueNull(com.fasterxml.jackson.core.JsonParser parser) Throws an exception if the current token is 'null'.static voidthrowIfUnknownField(String fieldName, String messageName) Throws anInvalidProtocolBufferExceptionindicating the field with variableNamefieldNameis not part ofMessagewith variableNamemessageName.
-
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 andMaps).- Throws:
IOException
-
checkObjectEnd
public static boolean checkObjectEnd(com.fasterxml.jackson.core.JsonToken token) Returns whether theJsonTokenis '}'. 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
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
Parsers an int32 value out of the input.- Throws:
IOException
-
parseInt64
Parsers an int64 value out of the input.- Throws:
IOException
-
parseUInt32
Parsers a uint32 value out of the input.- Throws:
IOException
-
parseUInt64
Parsers a uint64 value out of the input.- Throws:
IOException
-
parseBool
Parsers a bool value out of the input.- Throws:
IOException
-
parseFloat
Parsers a float value out of the input.- Throws:
IOException
-
parseDouble
Parsers a double value out of the input.- Throws:
IOException
-
parseString
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 aMessagevalue 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 numberfieldNumberand variableNamefullName. If the field has already been set, anInvalidProtocolBufferExceptionis 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 whoseoneofCasehas already been set. If so, anInvalidProtocolBufferExceptionis thrown.- Throws:
com.google.protobuf.InvalidProtocolBufferException
-
throwIfUnknownField
public static void throwIfUnknownField(String fieldName, String messageName) throws com.google.protobuf.InvalidProtocolBufferException Throws anInvalidProtocolBufferExceptionindicating the field with variableNamefieldNameis not part ofMessagewith variableNamemessageName. 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 thecurrentDepthof nested message parsing is higher than the limit, and throwsInvalidProtocolBufferExceptionif so. This is used to prevent stack exhaustion attacks with extremely nested recursive messages.- Throws:
com.google.protobuf.InvalidProtocolBufferException
-