spring-batch icon indicating copy to clipboard operation
spring-batch copied to clipboard

Add ExecutionContextSerializer implementation based on Gson

Open fmbenhassine opened this issue 2 years ago • 4 comments

The goal of this issue is to add an implementation of ExecutionContextSerializer based on Google's Gson, to give our users the choice between at least two implementations (ie Jackson and Gson) out-of-the-box, as mentioned in #4140.

fmbenhassine avatar Jul 14 '22 07:07 fmbenhassine

For some types, a serialization round trip with gson does not work OOTB. The following tests are failing with gson 2.9.1:

@Test
void testGsonSerializationRoundTripForDate() {
    Gson gson = new Gson();
    Date src = new Date();
    String s = gson.toJson(src);
    Date date = gson.fromJson(new StringReader(s), Date.class);
    Assertions.assertEquals(date, src);
}

@Test
void testGsonSerializationRoundTripForLocalDate() {
    Gson gson = new Gson();
    LocalDate src = LocalDate.now();
    String s = gson.toJson(src);
    LocalDate date = gson.fromJson(new StringReader(s), LocalDate.class);
    Assertions.assertEquals(date, src);
}

fmbenhassine avatar Oct 19 '22 13:10 fmbenhassine

Hi. @fmbenhassine

Could I handle this issue? If you are okay, I want to try to implement it. I already took a look at your draft.

Do I have to make my branch from GH-4151 of your repository?

ssjf409 avatar Dec 25 '23 16:12 ssjf409

@ssjf409 Sure! Thank you for your offer to help. You can start from the latest main branch if you want to contribute a new implementation.

My attempt was almost done, except that I couldn't figure out how to make gson serialize the type information (similar to jackson which writes an attribute @class: fully.qualified.class.name). I believe this should be configured on the gson instance, but I could not find how at the time. Do you know how to do that? I prefer digging further and try to delegate this to the serializarion library than handling it manually in the execution serializer implementation of Spring Batch.

fmbenhassine avatar Jan 09 '24 14:01 fmbenhassine

Thanks for your guide.👍

Actually. I didn't think about that you mentioned (I couldn't figure out how to make gson serialize the type information (similar to jackson which writes an attribute @class: fully.qualified.class.name)). I just it is enough to meet to condition for roundtrip. However, I try to find a way.

ssjf409 avatar Jan 13 '24 07:01 ssjf409