-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #278 from kagkarlsson/serializer_improvements
Document serialization, new serializers
- Loading branch information
Showing
29 changed files
with
865 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/Serializer.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/serializer/GsonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (C) Gustav Karlsson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.kagkarlsson.scheduler.serializer; | ||
|
||
import com.github.kagkarlsson.scheduler.serializer.gson.InstantAdapter; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Instant; | ||
import java.util.function.Consumer; | ||
|
||
public class GsonSerializer implements Serializer { | ||
public static final Charset CHARSET = StandardCharsets.UTF_8; | ||
private final Gson gson; | ||
|
||
public static GsonBuilder getDefaultGson() { | ||
return new GsonBuilder() | ||
.serializeNulls() | ||
.registerTypeAdapter(Instant.class, new InstantAdapter()); | ||
} | ||
|
||
public GsonSerializer() { | ||
this(getDefaultGson().create()); | ||
} | ||
|
||
public GsonSerializer(Gson gson) { | ||
this.gson = gson; | ||
} | ||
|
||
public GsonSerializer(Consumer<GsonBuilder> gsonCustomizer) { | ||
GsonBuilder defaultGson = getDefaultGson(); | ||
gsonCustomizer.accept(defaultGson); | ||
this.gson = defaultGson.create(); | ||
} | ||
|
||
@Override | ||
public byte[] serialize(Object object) { | ||
return gson.toJson(object).getBytes(CHARSET); | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(Class<T> clazz, byte[] serializedData) { | ||
return gson.fromJson(new String(serializedData, CHARSET), clazz); | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
...cheduler/src/main/java/com/github/kagkarlsson/scheduler/serializer/JacksonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Copyright (C) Gustav Karlsson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.kagkarlsson.scheduler.serializer; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.github.kagkarlsson.scheduler.exceptions.SerializationException; | ||
import com.github.kagkarlsson.scheduler.serializer.gson.InstantAdapter; | ||
import com.github.kagkarlsson.scheduler.serializer.jackson.InstantDeserializer; | ||
import com.github.kagkarlsson.scheduler.serializer.jackson.InstantSerializer; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
import java.io.IOException; | ||
import java.sql.Time; | ||
import java.time.Instant; | ||
import java.util.function.Consumer; | ||
|
||
public class JacksonSerializer implements Serializer { | ||
private final ObjectMapper objectMapper; | ||
|
||
public static ObjectMapper getDefaultObjectMapper() { | ||
SimpleModule module = new SimpleModule(); | ||
module.addSerializer(Instant.class, new InstantSerializer()); | ||
module.addDeserializer(Instant.class, new InstantDeserializer()); | ||
|
||
return new ObjectMapper() | ||
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) | ||
.registerModule(module); | ||
} | ||
|
||
public JacksonSerializer() { | ||
this(getDefaultObjectMapper()); | ||
} | ||
|
||
public JacksonSerializer(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
public JacksonSerializer(Consumer<ObjectMapper> objectMapperCustomizer) { | ||
ObjectMapper defaultObjectMapper = getDefaultObjectMapper(); | ||
objectMapperCustomizer.accept(defaultObjectMapper); | ||
this.objectMapper = defaultObjectMapper; | ||
} | ||
|
||
@Override | ||
public byte[] serialize(Object object) { | ||
try { | ||
return objectMapper.writeValueAsBytes(object); | ||
} catch (JsonProcessingException e) { | ||
throw new SerializationException("Failed to serialize object.", e); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(Class<T> clazz, byte[] serializedData) { | ||
try { | ||
return objectMapper.readValue(serializedData, clazz); | ||
} catch (IOException e) { | ||
throw new SerializationException("Failed to deserialize object.", e); | ||
} | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/serializer/JavaSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright (C) Gustav Karlsson | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.kagkarlsson.scheduler.serializer; | ||
|
||
import com.github.kagkarlsson.scheduler.exceptions.SerializationException; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.ObjectInput; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutput; | ||
import java.io.ObjectOutputStream; | ||
|
||
public class JavaSerializer implements Serializer { | ||
|
||
public byte[] serialize(Object data) { | ||
if (data == null) | ||
return null; | ||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
ObjectOutput out = new ObjectOutputStream(bos)) { | ||
out.writeObject(data); | ||
return bos.toByteArray(); | ||
} catch (Exception e) { | ||
throw new SerializationException("Failed to serialize object", e); | ||
} | ||
} | ||
|
||
public <T> T deserialize(Class<T> clazz, byte[] serializedData) { | ||
if (serializedData == null) | ||
return null; | ||
try (ByteArrayInputStream bis = new ByteArrayInputStream(serializedData); | ||
ObjectInput in = new ObjectInputStream(bis)) { | ||
return clazz.cast(in.readObject()); | ||
} catch (Exception e) { | ||
throw new SerializationException("Failed to deserialize object", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.