diff options
author | Simon Rettberg | 2020-01-16 12:15:36 +0100 |
---|---|---|
committer | Simon Rettberg | 2020-01-16 12:15:36 +0100 |
commit | 5f086bd47a462265b568cddd77f9af1f6a06633f (patch) | |
tree | 87abe4728bc945797f5a6fe71c0173af70a821b3 | |
parent | Use different compression thresholds for UDP and TCP (diff) | |
download | taskman-lite-5f086bd47a462265b568cddd77f9af1f6a06633f.tar.gz taskman-lite-5f086bd47a462265b568cddd77f9af1f6a06633f.tar.xz taskman-lite-5f086bd47a462265b568cddd77f9af1f6a06633f.zip |
Deserialize Strings to UTF-8 byte[]
-rw-r--r-- | daemon/src/main/java/org/openslx/taskmanager/util/ByteArrayDeserializer.java | 31 | ||||
-rw-r--r-- | daemon/src/main/java/org/openslx/taskmanager/util/Util.java | 5 |
2 files changed, 35 insertions, 1 deletions
diff --git a/daemon/src/main/java/org/openslx/taskmanager/util/ByteArrayDeserializer.java b/daemon/src/main/java/org/openslx/taskmanager/util/ByteArrayDeserializer.java new file mode 100644 index 0000000..bb2631f --- /dev/null +++ b/daemon/src/main/java/org/openslx/taskmanager/util/ByteArrayDeserializer.java @@ -0,0 +1,31 @@ +package org.openslx.taskmanager.util; + +import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; + +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +public class ByteArrayDeserializer implements JsonDeserializer<byte[]> +{ + + @Override + public byte[] deserialize( JsonElement json, Type typeOfT, JsonDeserializationContext context ) throws JsonParseException + { + if ( json.isJsonArray() ) { + JsonArray ja = json.getAsJsonArray(); + int len = ja.size(); + byte[] ba = new byte[ len ]; + int i = 0; + for ( JsonElement el : ja ) { + ba[i++] = el.getAsByte(); + } + return ba; + } + return json.getAsString().getBytes( StandardCharsets.UTF_8 ); + } + +} diff --git a/daemon/src/main/java/org/openslx/taskmanager/util/Util.java b/daemon/src/main/java/org/openslx/taskmanager/util/Util.java index 361c0e5..92602ff 100644 --- a/daemon/src/main/java/org/openslx/taskmanager/util/Util.java +++ b/daemon/src/main/java/org/openslx/taskmanager/util/Util.java @@ -18,7 +18,10 @@ public class Util */ public static Gson explicitGsonInstance() { - return gsonBuilder.excludeFieldsWithoutExposeAnnotation().create(); + return gsonBuilder + .excludeFieldsWithoutExposeAnnotation() + .registerTypeAdapter( byte[].class, new ByteArrayDeserializer() ) + .create(); } public static int parseInt(String str, int def) |