summaryrefslogtreecommitdiffstats
path: root/daemon/src/main/java/org/openslx/taskmanager/util/ByteArrayDeserializer.java
blob: bb2631f0243d130a988cc1e936082251985b9350 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 );
	}

}