I have a thrift class generated by thrift struct:
struct Chapter {
1: required i32 id,
2: optional string name,
3: optional string desc,
4: optional i32 questionCount,
5: optional i32 keypointId,
}
as far as i know, we can use TSimpleJSONProtocol.Factory to serialize the object without setXXX and null members. But, how to serialize a list of Chapter objects. when i using Jackson, there must be some setXXX, as follows:
[{"name":"ch1","desc":null,"questionCount":0,"keypointId":1,"setName":true,"setDesc":false,"setQuestionCount":false,"setKeypointId":true},{"name":"ch2","desc":null,"questionCount":0,"keypointId":2,"setName":true,"setDesc":false,"setQuestionCount":false,"setKeypointId":true}]
But what i need is:
[{"name":"ch1","keypointId":1},{"name":"ch2","keypointId":2}]
My simple solution is joining every object:
public static <T extends TBase> String writeValuewithTBase(Collection<T> c) {
try {
if (c == null) {
return null;
} else if (c.isEmpty()) {
return "[]";
}
return c.stream().map(t -> {
try {
return ThriftSerDePool.getSimpleJsonSerializer().toString(t);
} catch (TException e) {
LOG.warn("Failed to serialize object to JSON string", e);
throw new RuntimeException(e);
}
}).collect(Collectors.joining(",", "[", "]"));
} catch (Exception e) {
LOG.warn("Failed to serialize object to JSON string", e);
return "";
}
}
Is any good idea, like using Jackson's Custom_Serializers