1

My Haskell code needs to call a Java web server. The web server will be sending a serialized Java object as a response. I have the compiled Java class but am unsure how to use it in Haskell. Is there a way to properly deserialize the response into a Haskell object without copying and pasting the Java class into a Haskell type?

Garrett M
  • 103
  • 2
  • 10
  • 6
    Do you have control of both the Haskell code and the Java code? If so, you might want to consider using a language-agnostic protocol like [Protobuf](https://developers.google.com/protocol-buffers/?hl=en), [Thrift](http://thrift.apache.org/) or others. – yshavit Jan 03 '16 at 20:14
  • 1
    If you're just sending a Java object serialized using Java's default serialization...don't. Serialize it some custom way, as @yshavit mentioned. – Louis Wasserman Jan 03 '16 at 20:15
  • @yshavit I don't have access to the Java codebase. It is an external service I call. The only thing I have from them is the compiled Java class for the response to deserialize back into. – Garrett M Jan 03 '16 at 20:19
  • @GarrettM And they require it serialized by native Java serialization? Ugh. First thing you should do is email their support people to tell them that that's unacceptable. After that, I'm not sure the best approach. I might almost suggest writing a very small Java service with an API that accepts Protobuf/Thrift/etc, and returns back a byte array of the serialized Java object. So, you send over a message describing what you want, the service creates the Java object, serializes it, and sends back those bytes. Pretty ugly, but also pretty simple. – yshavit Jan 03 '16 at 20:21
  • @yshavit What counts as native Java serialization? We can use any serialization package we want but I haven't heard of a Haskell serialization package that can take a Java file as the type. For your middle layer, are you suggesting I write my Haskell codebase to send and receive Protobuf or Thrift messages and have a middle Java layer that converts each of those messages into the appropriate Java object? Doesn't that just move the problem down and force me to manually convert from Protobuf or Thrift to Java? Would something like Swagger help at all? – Garrett M Jan 03 '16 at 20:28
  • 1
    @GarrettM By native Java serialization, I meant [this](https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html). I assumed that's what you meant by "sending a serialized Java object," but it's not clear. I haven't used Swagger. And yes, you understood my suggestion. The advantage is that you don't need to understand the Java serialization protocol, you can just have the middle layer deserialize the object (which it already knows how to do), construct a Thrift/etc object (which is easy), and then send that to your Haskell app (which is easy to send and use). – yshavit Jan 03 '16 at 20:45
  • @yshavit In that case, we do use Java serialization. I haven't looked at those options yet but doesn't protobuf and thrift require knowing the model they'll be transforming into? I'm really trying to avoid copying the model from the Java class to anything else. – Garrett M Jan 03 '16 at 20:48
  • 1
    Yes, they do. I think you're out of luck, unless you can find a Haskell library that knows how to read native Java serialization (asking for one is off topic for SO (see item 4 [here](http://stackoverflow.com/help/on-topic)). Sorry! – yshavit Jan 03 '16 at 21:06
  • @yshavit Thanks for all the help. I appreciate it. – Garrett M Jan 03 '16 at 21:27

0 Answers0