def __call__(self, value, ctx): """ Decodes a Confluent Schema Registry formatted Avro bytes to an object. Arguments: value (bytes): bytes ctx (SerializationContext): Metadata pertaining to the serialization operation. Raises: SerializerError if an error occurs ready data. Returns: object: if ``from_dict`` is set, otherwise dict. If no value is supplied None is returned. """ if value is None: return None if len(value) <= 5: raise SerializationError("Message too small. This message was not" " produced with a Confluent" " Schema Registry serializer") with _ContextStringIO(value) as payload: magic, schema_id = unpack('>bI', payload.read(5)) if magic != _MAGIC_BYTE: raise SerializationError("Unknown magic byte. This message was" " not produced with a Confluent" " Schema Registry serializer") writer_schema = self._writer_schemas.get(schema_id, None) if writer_schema is None: schema = self._registry.get_schema(schema_id) prepared_schema = _schema_loads(schema.schema_str) writer_schema = parse_schema(loads( prepared_schema.schema_str)) self._writer_schemas[schema_id] = writer_schema obj_dict = schemaless_reader(payload, writer_schema, self._reader_schema) if self._from_dict is not None: return self._from_dict(obj_dict, ctx) return obj_dict
def __call__(self, value, ctx): """ Deserializes Schema Registry formatted JSON to JSON object literal(dict). Args: value (bytes): Confluent Schema Registry formatted JSON bytes ctx (SerializationContext): Metadata pertaining to the serialization operation. Returns: dict: Deserialized JSON Raises: SerializerError: If ``value`` cannot be validated by the schema configured with this JsonDeserializer instance. """ if value is None: return None if len(value) <= 5: raise SerializationError( "Message too small. This message was not" " produced with a Confluent" " Schema Registry serializer" ) with _ContextStringIO(value) as payload: magic, schema_id = struct.unpack(">bI", payload.read(5)) if magic != _MAGIC_BYTE: raise SerializationError( "Unknown magic byte. This message was" " not produced with a Confluent" " Schema Registry serializer" ) # JSON documents are self-describing; no need to query schema obj_dict = json.loads(payload.read(), encoding="utf8") try: validate(instance=obj_dict, schema=self._parsed_schema) except ValidationError as ve: raise SerializationError(ve.message) if self._from_dict is not None: return self._from_dict(obj_dict, ctx) return obj_dict
def __call__(self, value, ctx): if value is None: return None try: return _struct.unpack('>q', value)[0] except _struct.error as e: raise SerializationError(str(e))
def __call__(self, obj, ctx): """ Serializes an object to the Confluent Schema Registry's JSON binary format. Args: obj (object): object instance to serialize. ctx (SerializationContext): Metadata pertaining to the serialization operation. Note: None objects are represented as Kafka Null. Raises: SerializerError if any error occurs serializing obj Returns: bytes: Confluent Schema Registry formatted JSON bytes """ if obj is None: return None subject = self._subject_name_func(ctx, self._schema_name) # Check to ensure this schema has been registered under subject_name. if self._auto_register and subject not in self._known_subjects: # The schema name will always be the same. We can't however register # a schema without a subject so we set the schema_id here to handle # the initial registration. self._schema_id = self._registry.register_schema( subject, self._schema) self._known_subjects.add(subject) elif not self._auto_register and subject not in self._known_subjects: registered_schema = self._registry.lookup_schema( subject, self._schema) self._schema_id = registered_schema.schema_id self._known_subjects.add(subject) if self._to_dict is not None: value = self._to_dict(obj, ctx) else: value = obj try: validate(instance=value, schema=self._parsed_schema) except ValidationError as ve: raise SerializationError(ve.message) with _ContextStringIO() as fo: # Write the magic byte and schema ID in network byte order (big endian) fo.write(struct.pack('>bI', _MAGIC_BYTE, self._schema_id)) # JSON dump always writes a str never bytes # https://docs.python.org/3/library/json.html fo.write(json.dumps(value).encode('utf8')) return fo.getvalue()
def __call__(self, value, ctx): """ Deserializes Schema Registry formatted Protobuf to Protobuf Message. Args: value (bytes): Confluent Schema Registry formatted Protobuf bytes. ctx (SerializationContext): Metadata pertaining to the serialization operation. Returns: Message: Protobuf Message instance. Raises: SerializerError: If response payload and expected Message type differ. """ if value is None: return None # SR wire protocol + msg_index length if len(value) < 6: raise SerializationError("Message too small. This message was not" " produced with a Confluent" " Schema Registry serializer") with _ContextStringIO(value) as payload: magic, schema_id = struct.unpack('>bI', payload.read(5)) if magic != _MAGIC_BYTE: raise SerializationError("Unknown magic byte. This message was" " not produced with a Confluent" " Schema Registry serializer") # Protobuf Messages are self-describing; no need to query schema # Move the reader cursor past the index _ = self._decode_index(payload, zigzag=not self._use_deprecated_format) msg = self._msg_class() try: msg.ParseFromString(payload.read()) except DecodeError as e: raise SerializationError(str(e)) return msg