Skip to content

Commit

Permalink
Use unsigned byte string representation for bootstrap REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 7, 2016
1 parent a06ff2b commit 86c9f9f
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
Expand All @@ -31,6 +32,14 @@
import org.eclipse.leshan.server.bootstrap.demo.ConfigurationChecker.ConfigurationException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;

/**
Expand All @@ -39,14 +48,29 @@
@SuppressWarnings("serial")
public class BootstrapServlet extends HttpServlet {

private static class SignedByteUnsignedByteAdapter implements JsonSerializer<Byte>, JsonDeserializer<Byte> {

@Override
public Byte deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return json.getAsByte();
}

@Override
public JsonElement serialize(Byte src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive((int) src & 0xff);
}
}

private final BootstrapStoreImpl bsStore;

private final Gson gson;

public BootstrapServlet(BootstrapStoreImpl bsStore) {
this.bsStore = bsStore;

this.gson = new Gson();
this.gson = new GsonBuilder().registerTypeHierarchyAdapter(Byte.class, new SignedByteUnsignedByteAdapter())
.create();
}

@Override
Expand Down

0 comments on commit 86c9f9f

Please sign in to comment.