0

How can we use json.dumps directly in the constructor, instead of calling it via a separate function?

def json_serialize(obj, *args):
    return json.dumps(obj)

class KafkaProducer(object):

    def __init__(self, config):
        config = {
            "key.serializer": json_serialize,
            "value.serializer": json_serialize,
        }
        self.producer = SerializingProducer(config)

    def produce(self, *args, **kwargs):
        self.producer.produce(*args, **kwargs)
        self.producer.flush()

When I use produce method of KafkaProducer class it works with the above implementation. As json_serialize function is used to do only json.dumps(obj).

If I tried this using json.dumps direct in KafkaProducer as given below and then call produce a method of class it won't work. gives error as:


    KafkaError{code=_KEY_SERIALIZATION,val=-162,str="dumps() takes 1 positional argument but 2 were given"}

class KafkaProducer(object):

    def __init__(self, config):
        config = {
            "key.serializer": json.dumps,
            "value.serializer": json.dumps,
        }
        self.producer = SerializingProducer(config)

    def produce(self, *args, **kwargs):
        self.producer.produce(*args, **kwargs)
        self.producer.flush()
Akash Pagar
  • 637
  • 8
  • 22

1 Answers1

1

Well, you can't. key.serializer and value.serializer functions must have the following signature: Callable(obj, SerializationContext) -> bytes. json.dumps doesn't accept SerializationContext as a parameter, so you need to use some wrapper around it. That's why you need json_serialize function.

Alexander Volkovsky
  • 2,588
  • 7
  • 13