shooter
shooter

Reputation: 11

Topology conf is not json-serializable - Storm

I want to transfer a custom object to bolt and the function of this custom object is to get the various bean. But there was a error when I started the program.

The error is "Topology conf is not json-serializable".

How to serialize the custom objects, which can give me some code,thank you!

Upvotes: 1

Views: 313

Answers (1)

Stig Rohde Døssing
Stig Rohde Døssing

Reputation: 3651

Storm uses https://github.com/fangyidong/json-simple to serialize to JSON. You need to make sure that everything you put in the topology conf is serializable to JSON with this library.

The library does have a way to make classes JSON serializable by implementing https://github.com/fangyidong/json-simple/blob/master/src/main/java/org/json/simple/JSONAware.java, but I don't see a nice way to convert back to a Java object.

Probably the easiest solution is that you use some library to convert the object to String or Map before you put it in the topology config, and then use the same library to convert back to Object in the bolt. I can recommend https://github.com/FasterXML/jackson. Here's a simple example of serialization with that library

private static class Demo {
    private final int prop;

    public Demo(@JsonProperty("prop") int prop) {
        this.prop = prop;
    }        

    public int getProp() {
        return prop;
    }
}

public static void main(String[] args) throws Exception {
    ObjectMapper mapper = new ObjectMapper();

    String serialized = mapper.writeValueAsString(new Demo(10));

    Demo deserialized = mapper.readValue(serialized, Demo.class);
}

Upvotes: 1

Related Questions