How to de-serialize byte[] to Java POJO(or Object) at client side, i.e how will client get the class information?

Viewed 455

I have two related questions:

  1. I'm able to send byte[] over the network for this class:
class Employee implements Serializable{
    byte[] avroBytes, //avro data in byte[] array
    String name
}

In my test case, using MockMvc, I'm able to de-serialize it, and then cast it to Employee object because my test case can use the same POJO from code. However the client will not have this POJO or class information. After it receives the bytes, how can it reconstruct this POJO(or whatever is the equivalent in other languages) or Object? Do I need to send in some class information in the header? It's possible that the client is using Python or some other language, so I need a way for this class information to be generic.

In my test case this works:

 Employee res = (Employee ) SerializationUtils.deserialize(result.getResponse().getContentAsByteArray());

How will client cast the de-serialized bytes to Employee if they don't have this POJO/information? I could do it in my test case because my code has the Employee class in code.

Additional information to comments:

I serialized it using org.springframework.util.SerializationUtils serialize() method

5 Answers

If you need POJO on the client side, you need to share generated code that the schema produce. If you don't need POJO you can populate GenericRecord directly with the schema.

UPDATE

Schema schema = new Schema.Parser().parse(new File("Employee.avsc"));    DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(schema);
Decoder decoder = DecoderFactory.get().directBinaryDecoder(result.getResponse().getContentAsByteArray(), null);
GenericRecord gr= datumReader.read(null, decoder);

Jackson can resolve your problem. It has the ability to convert object to byte array and then convert from byte array to another object with a similar structure.
Conversion will be performed in next way:

Object1 -> JSON -> byte[]
byte[] -> JSON -> Object2

Example:

public class TestClass {
    @Test
    public void methodTest() throws IOException {
        Employee1 employee1 = new Employee1();
        employee1.bytes = new byte[] {1, 2, 3};
        employee1.name = "name";

        ObjectMapper objectMapper = new ObjectMapper();
        //convert Employee to byte array
        byte[] serialized = objectMapper.writeValueAsBytes(employee1);

        //convert from byte array to another object with a similar structure
        Employee2 employee2 = objectMapper.reader().readValue(serialized, Employee2.class);

        Assert.assertArrayEquals(employee1.bytes, employee2.bytes);
        Assert.assertEquals(employee1.name, employee2.name);
    }
}

public class Employee1 implements Serializable {
   public byte[] bytes;
   public String name;
}

public class Employee2 implements Serializable {
    public byte[] bytes;
    public String name;
}

Jackson provides the ability to specify an exact type for conversion. It is achieved thru intermediate JSON.

I think this is a more generic question about SOA (service-oriented architecture) architecture and how it works. so when it comes to producing and consuming messages between two brokers it needs some kind of interoperability. so in this case, from the beginning, we had a lot of frameworks and tools to achieve these use cases, starting with CORBA, and RPC (creating stubs stubs to consume the service from outside), However quoting "The client will not have this POJO or class information." -> This statement is about more of Serilization vs Marshaling

so for your use case assuming this is a REST service (than SOAP), you can use consuming types such as application/(json, etc..). please see more about consuming a rest service

  • if "sending byte[]" isn't necessary, just send data in format of JSON / XML / any-random-middle-format
    • String is just an another kind of byte[]
  • java.io.Serializable / Protobuf lib / any-random-serialization-lib

Client could use a Map<String, Object> or equivalent in ther programming language.

Related