Hello! Now the GSON library is used, I want to switch to using JSON with the help of the JACKSON library (at least try it, I heard that it works faster). To work with dates and ByteArray created such a cover:
public class MyGsonWrapper { public static Gson getMyGson() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Date.class, new GsonDateSerializer()); gsonBuilder.registerTypeAdapter(Date.class, new GsonDateDeserializer()); gsonBuilder.registerTypeAdapter(byte[].class, new GsonByteArraySerializer()); return gsonBuilder.create(); } static class GsonDateDeserializer implements JsonDeserializer<Date> { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String s = json.getAsString().replace("/Date(", "").replace(")/", ""); if (s.equals("")) return null; boolean isDateBefore1970 = false; ........... if (isDateBefore1970) return new Date(-Long.valueOf(s) - offset * 60 * 1000); else return new Date(Long.valueOf(s) + offset * 60 * 1000); } } static class GsonDateSerializer implements JsonSerializer<Date> { public JsonElement serialize(Date date, Type typeOfT, JsonSerializationContext context) { return new JsonPrimitive("/Date(" + date.getTime() + ")/"); } } static class GsonByteArraySerializer implements JsonSerializer<byte[]> { public JsonElement serialize(byte[] data, Type typeOfT, JsonSerializationContext context) { return new JsonPrimitive(Base64.encodeBytes(data)); } } }
With ByteArray, JACKSON works fine by itself, therefore the GsonByteArraySerializer is not needed, with dates in milliseconds it is also configured there and it works with them, as I understand it. Maybe I'm in something wrong? Help to remake that would go with GSON on JACKSON library. As I understand it, the new wrapper should start like this:
public class MyJsonWrapper { public static ObjectMapper getMyJson() { ObjectMapper mapper = new ObjectMapper(new SmileFactory()); SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null)); module.addSerializer(Date.class, new JsonDateSerializer()); module.addDeserializer(Date.class, new JsonDateDeserializer()); module.addSerializer(byte[].class, new JsonByteArraySerializer()); mapper.registerModule(module); return mapper; } static class JsonDateDeserializer implements JsonDeserializer<Date> { public Date deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws java.io.IOException, JsonProcessingException { } static class JsonDateSerializer implements JsonSerializer<Date> { public JsonParser serialize(Date date, Type typeOfT, Serializers context) { return new JsonPrimitive("/Date(" + date.getTime() + ")/"); } } static class JsonByteArraySerializer implements JsonSerializer<byte[]> { public JsonParser serialize(byte[] data, Type typeOfT, Serializers context) { return new JsonPrimitive(Base64.encodeBytes(data)); } }
Previously, serialization and deserialization were as follows:
MyGsonWrapper.getMyGson (). ToJson (MyObject) MyGsonWrapper.getMyGson (). FromJson (response.Body, MyObject []. Class);
Maybe this wrapper is not needed at all, can it directly access JACKSON functions? If you still need it, tell me how to change. If it is not needed, then through what specific functions from the library to perform serialization and deserialization and how to access them ...