Class JsonBinary
JSON type, translating the encoded representation into
method calls on a supplied JsonFormatter implementation.
Binary Format
Each JSON value (scalar, object or array) has a one byte type identifier followed by the actual value.Scalar
The binary value may contain a single scalar that is one of:- null
- boolean
- int16
- int32
- int64
- uint16
- uint32
- uint64
- double
- string
DATEas a string of the formYYYY-MM-DDwhereYYYYcan be positive or negativeTIMEas a string of the formHH-MM-SSwhereHHcan be positive or negativeDATETIMEas a string of the formYYYY-MM-DD HH-mm-SS.sssssswhereYYYYcan be positive or negativeTIMESTAMPas the number of microseconds past epoch (January 1, 1970), or if negative the number of microseconds before epoch (January 1, 1970)- any other MySQL value encoded as an opaque binary value
JSON Object
If the value is a JSON object, its binary representation will have a header that contains:- the member count
- the size of the binary value in bytes
- a list of pointers to each key
- a list of pointers to each value
JSON Array
If the value is a JSON array, the binary representation will have a header with- the element count
- the size of the binary value in bytes
- a list of pointers to each value
Grammar
The grammar of the binary representation of JSON objects are defined in the MySQL codebase in the json_binary.h file:
doc ::= type value
type ::=
0x00 | // small JSON object
0x01 | // large JSON object
0x02 | // small JSON array
0x03 | // large JSON array
0x04 | // literal (true/false/null)
0x05 | // int16
0x06 | // uint16
0x07 | // int32
0x08 | // uint32
0x09 | // int64
0x0a | // uint64
0x0b | // double
0x0c | // utf8mb4 string
0x0f // custom data (any MySQL data type)
value ::=
object |
array |
literal |
number |
string |
custom-data
object ::= element-count size key-entry* value-entry* key* value*
array ::= element-count size value-entry* value*
// number of members in object or number of elements in array
element-count ::=
uint16 | // if used in small JSON object/array
uint32 // if used in large JSON object/array
// number of bytes in the binary representation of the object or array
size ::=
uint16 | // if used in small JSON object/array
uint32 // if used in large JSON object/array
key-entry ::= key-offset key-length
key-offset ::=
uint16 | // if used in small JSON object
uint32 // if used in large JSON object
key-length ::= uint16 // key length must be less than 64KB
value-entry ::= type offset-or-inlined-value
// This field holds either the offset to where the value is stored,
// or the value itself if it is small enough to be inlined (that is,
// if it is a JSON literal or a small enough [u]int).
offset-or-inlined-value ::=
uint16 | // if used in small JSON object/array
uint32 // if used in large JSON object/array
key ::= utf8mb4-data
literal ::=
0x00 | // JSON null literal
0x01 | // JSON true literal
0x02 | // JSON false literal
number ::= .... // little-endian format for [u]int(16|32|64), whereas
// double is stored in a platform-independent, eight-byte
// format using float8store()
string ::= data-length utf8mb4-data
custom-data ::= custom-type data-length binary-data
custom-type ::= uint8 // type identifier that matches the
// internal enum_field_types enum
data-length ::= uint8* // If the high bit of a byte is 1, the length
// field is continued in the next byte,
// otherwise it is the last byte of the length
// field. So we need 1 byte to represent
// lengths up to 127, 2 bytes to represent
// lengths up to 16383, and so on...
- Author:
- Randall Hauch
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classClass used internally to hold key entry information.protected static final classClass used internally to hold value entry information. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static StringasHex(byte b) protected static StringasHex(int value) static voidparse(byte[] bytes, JsonFormatter formatter) Parse the MySQL binary representation of aJSONvalue and call the suppliedJsonFormatterfor the various components of the value.voidparse(JsonFormatter formatter) protected voidparse(ValueType type, JsonFormatter formatter) protected voidparseArray(boolean small, JsonFormatter formatter) Parse a JSON array.static StringparseAsString(byte[] bytes) Parse the MySQL binary representation of aJSONvalue and return the JSON string representation.protected voidparseBoolean(JsonFormatter formatter) Parse a literal value that is either null,true, orfalse.protected voidparseDate(JsonFormatter formatter) Parse aDATEvalue, which is stored using the same format asDATETIME: 5 bytes + fractional-seconds storage.protected voidparseDatetime(JsonFormatter formatter) Parse aDATETIMEvalue, which is stored as 5 bytes + fractional-seconds storage.protected voidparseDecimal(int length, JsonFormatter formatter) Parse aDECIMALvalue.protected voidparseDouble(JsonFormatter formatter) Parse a 8 byte double value.protected voidparseInt16(JsonFormatter formatter) Parse a 2 byte integer value.protected voidparseInt32(JsonFormatter formatter) Parse a 4 byte integer value.protected voidparseInt64(JsonFormatter formatter) Parse a 8 byte integer value.protected voidparseObject(boolean small, JsonFormatter formatter) Parse a JSON object.protected voidparseOpaque(JsonFormatter formatter) Parse an opaque type.protected voidparseOpaqueValue(ColumnType type, int length, JsonFormatter formatter) protected voidparseString(JsonFormatter formatter) Parse the length and value of a string stored in MySQL's "utf8mb" character set (which equates to Java's UTF-8 character set.protected voidparseTime(JsonFormatter formatter) Parse aTIMEvalue, which is stored using the same format asDATETIME: 5 bytes + fractional-seconds storage.protected voidparseUInt16(JsonFormatter formatter) Parse a 2 byte unsigned integer value.protected voidparseUInt32(JsonFormatter formatter) Parse a 4 byte unsigned integer value.protected voidparseUInt64(JsonFormatter formatter) Parse a 8 byte unsigned integer value.protected longreadBigEndianLong(int numBytes) protected intprotected intprotected intprotected intprotected longprotected Booleanprotected intprotected longprotected BigIntegerprotected intreadUnsignedIndex(int maxValue, boolean isSmall, String desc) protected ValueTypeprotected intRead a variable-length integer value.
-
Constructor Details
-
JsonBinary
public JsonBinary(byte[] bytes) -
JsonBinary
-
-
Method Details
-
parseAsString
Parse the MySQL binary representation of aJSONvalue and return the JSON string representation.This method is equivalent to
parse(byte[], JsonFormatter)using theJsonStringFormatter.- Parameters:
bytes- the binary representation; may not be null- Returns:
- the JSON string representation; never null
- Throws:
IOException- if there is a problem reading or processing the binary representation
-
parse
Parse the MySQL binary representation of aJSONvalue and call the suppliedJsonFormatterfor the various components of the value.- Parameters:
bytes- the binary representation; may not be nullformatter- the formatter that will be called as the binary representation is parsed; may not be null- Throws:
IOException- if there is a problem reading or processing the binary representation
-
getString
-
parse
- Throws:
IOException
-
parse
- Throws:
IOException
-
parseObject
Parse a JSON object.The grammar of the binary representation of JSON objects are defined in the MySQL code base in the json_binary.h file:
Grammar
value ::= object | array | literal | number | string | custom-data object ::= element-count size key-entry* value-entry* key* value* // number of members in object or number of elements in array element-count ::= uint16 | // if used in small JSON object/array uint32 // if used in large JSON object/array // number of bytes in the binary representation of the object or array size ::= uint16 | // if used in small JSON object/array uint32 // if used in large JSON object/array key-entry ::= key-offset key-length key-offset ::= uint16 | // if used in small JSON object uint32 // if used in large JSON object key-length ::= uint16 // key length must be less than 64KB value-entry ::= type offset-or-inlined-value // This field holds either the offset to where the value is stored, // or the value itself if it is small enough to be inlined (that is, // if it is a JSON literal or a small enough [u]int). offset-or-inlined-value ::= uint16 | // if used in small JSON object/array uint32 // if used in large JSON object/array key ::= utf8mb4-data literal ::= 0x00 | // JSON null literal 0x01 | // JSON true literal 0x02 | // JSON false literal number ::= .... // little-endian format for [u]int(16|32|64), whereas // double is stored in a platform-independent, eight-byte // format using float8store() string ::= data-length utf8mb4-data custom-data ::= custom-type data-length binary-data custom-type ::= uint8 // type identifier that matches the // internal enum_field_types enum data-length ::= uint8* // If the high bit of a byte is 1, the length // field is continued in the next byte, // otherwise it is the last byte of the length // field. So we need 1 byte to represent // lengths up to 127, 2 bytes to represent // lengths up to 16383, and so on...- Parameters:
small-trueif the object being read is "small", orfalseotherwiseformatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseArray
Parse a JSON array.The grammar of the binary representation of JSON objects are defined in the MySQL code base in the json_binary.h file, and are:
Grammar
Grammar
value ::= object | array | literal | number | string | custom-data array ::= element-count size value-entry* value* // number of members in object or number of elements in array element-count ::= uint16 | // if used in small JSON object/array uint32 // if used in large JSON object/array // number of bytes in the binary representation of the object or array size ::= uint16 | // if used in small JSON object/array uint32 // if used in large JSON object/array value-entry ::= type offset-or-inlined-value // This field holds either the offset to where the value is stored, // or the value itself if it is small enough to be inlined (that is, // if it is a JSON literal or a small enough [u]int). offset-or-inlined-value ::= uint16 | // if used in small JSON object/array uint32 // if used in large JSON object/array key ::= utf8mb4-data literal ::= 0x00 | // JSON null literal 0x01 | // JSON true literal 0x02 | // JSON false literal number ::= .... // little-endian format for [u]int(16|32|64), whereas // double is stored in a platform-independent, eight-byte // format using float8store() string ::= data-length utf8mb4-data custom-data ::= custom-type data-length binary-data custom-type ::= uint8 // type identifier that matches the // internal enum_field_types enum data-length ::= uint8* // If the high bit of a byte is 1, the length // field is continued in the next byte, // otherwise it is the last byte of the length // field. So we need 1 byte to represent // lengths up to 127, 2 bytes to represent // lengths up to 16383, and so on...- Parameters:
small-trueif the object being read is "small", orfalseotherwiseformatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseBoolean
Parse a literal value that is either null,true, orfalse.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseInt16
Parse a 2 byte integer value.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseUInt16
Parse a 2 byte unsigned integer value.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseInt32
Parse a 4 byte integer value.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseUInt32
Parse a 4 byte unsigned integer value.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseInt64
Parse a 8 byte integer value.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseUInt64
Parse a 8 byte unsigned integer value.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseDouble
Parse a 8 byte double value.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseString
Parse the length and value of a string stored in MySQL's "utf8mb" character set (which equates to Java's UTF-8 character set. The length is avariable length integerlength of the string.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseOpaque
Parse an opaque type. Specific types such asDATE,TIME, andDATETIMEvalues are stored as opaque types, though they are to be unpacked. TIMESTAMPs are also stored as opaque types, but converted by MySQL toDATETIMEprior to storage. Other MySQL types are stored as opaque types and passed on to the formatter as opaque values.See the MySQL source code for the logic used in this method.
Grammar
custom-data ::= custom-type data-length binary-data custom-type ::= uint8 // type identifier that matches the // internal enum_field_types enum data-length ::= uint8* // If the high bit of a byte is 1, the length // field is continued in the next byte, // otherwise it is the last byte of the length // field. So we need 1 byte to represent // lengths up to 127, 2 bytes to represent // lengths up to 16383, and so on...- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseDate
Parse aDATEvalue, which is stored using the same format asDATETIME: 5 bytes + fractional-seconds storage. However, the hour, minute, second, and fractional seconds are ignored.The non-fractional part is 40 bits:
1 bit sign (1= non-negative, 0= negative) 17 bits year*13+month (year 0-9999, month 0-12) 5 bits day (0-31) 5 bits hour (0-23) 6 bits minute (0-59) 6 bits second (0-59)
The fractional part is typically dependent upon the fsp (i.e., fractional seconds part) defined by a column, but in the case of JSON it is always 3 bytes.The format of all temporal values is outlined in the MySQL documentation, although since the MySQL
JSONtype is only available in 5.7, only version 2 of the date-time formats are necessary.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseTime
Parse aTIMEvalue, which is stored using the same format asDATETIME: 5 bytes + fractional-seconds storage. However, the year, month, and day values are ignoredThe non-fractional part is 40 bits:
1 bit sign (1= non-negative, 0= negative) 17 bits year*13+month (year 0-9999, month 0-12) 5 bits day (0-31) 5 bits hour (0-23) 6 bits minute (0-59) 6 bits second (0-59)
The fractional part is typically dependent upon the fsp (i.e., fractional seconds part) defined by a column, but in the case of JSON it is always 3 bytes.The format of all temporal values is outlined in the MySQL documentation, although since the MySQL
JSONtype is only available in 5.7, only version 2 of the date-time formats are necessary.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseDatetime
Parse aDATETIMEvalue, which is stored as 5 bytes + fractional-seconds storage.The non-fractional part is 40 bits:
1 bit sign (1= non-negative, 0= negative) 17 bits year*13+month (year 0-9999, month 0-12) 5 bits day (0-31) 5 bits hour (0-23) 6 bits minute (0-59) 6 bits second (0-59)
The sign bit is always 1. A value of 0 (negative) is reserved. The fractional part is typically dependent upon the fsp (i.e., fractional seconds part) defined by a column, but in the case of JSON it is always 3 bytes. Unlike the documentation, however, the 8 byte value is in little-endian form.The format of all temporal values is outlined in the MySQL documentation, although since the MySQL
JSONtype is only available in 5.7, only version 2 of the date-time formats are necessary.- Parameters:
formatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseDecimal
Parse aDECIMALvalue. The first two bytes are the precision and scale, followed by the binary representation of the decimal itself.- Parameters:
length- the length of the complete binary representationformatter- the formatter to be notified of the parsed value; may not be null- Throws:
IOException- if there is a problem reading the JSON value
-
parseOpaqueValue
protected void parseOpaqueValue(ColumnType type, int length, JsonFormatter formatter) throws IOException - Throws:
IOException
-
readFractionalSecondsInMicroseconds
- Throws:
IOException
-
readBigEndianLong
- Throws:
IOException
-
readUnsignedIndex
- Throws:
IOException
-
readInt16
- Throws:
IOException
-
readUInt16
- Throws:
IOException
-
readInt24
- Throws:
IOException
-
readInt32
- Throws:
IOException
-
readUInt32
- Throws:
IOException
-
readInt64
- Throws:
IOException
-
readUInt64
- Throws:
IOException
-
readVariableInt
Read a variable-length integer value.If the high bit of a byte is 1, the length field is continued in the next byte, otherwise it is the last byte of the length field. So we need 1 byte to represent lengths up to 127, 2 bytes to represent lengths up to 16383, and so on...
- Returns:
- the integer value
- Throws:
IOException- if we don't encounter an end-of-int marker
-
readLiteral
- Throws:
IOException
-
readValueType
- Throws:
IOException
-
asHex
-
asHex
-