-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80d9340
commit b48a32a
Showing
3 changed files
with
87 additions
and
1 deletion.
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
72 changes: 72 additions & 0 deletions
72
leshan-standalone/src/main/java/leshan/server/servlet/json/TlvDeserializer.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,72 @@ | ||
package leshan.server.servlet.json; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import leshan.server.lwm2m.tlv.Tlv; | ||
import leshan.server.lwm2m.tlv.TlvType; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.JsonPrimitive; | ||
|
||
public class TlvDeserializer implements JsonDeserializer<Tlv> { | ||
|
||
@Override | ||
public Tlv deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
throws JsonParseException { | ||
|
||
if (json == null) { | ||
return null; | ||
} | ||
|
||
if (json.isJsonObject()) { | ||
JsonObject object = (JsonObject) json; | ||
|
||
// id | ||
int id; | ||
if (object.has("id")) { | ||
id = object.get("id").getAsInt(); | ||
} else { | ||
throw new JsonParseException("Missing id"); | ||
} | ||
|
||
// type | ||
TlvType tlvtype = null; | ||
if (object.has("type")) { | ||
String type = object.get("type").getAsString(); | ||
tlvtype = TlvType.valueOf(type); | ||
} else { | ||
throw new JsonParseException("Missing type"); | ||
} | ||
|
||
switch (tlvtype) { | ||
case RESOURCE_VALUE: | ||
case RESOURCE_INSTANCE: | ||
if (object.has("value")) { | ||
// TODO manage long and date | ||
JsonPrimitive jsonPrimitive = object.get("value").getAsJsonPrimitive(); | ||
if (jsonPrimitive.isString()) { | ||
return Tlv.newStringValue(tlvtype, jsonPrimitive.getAsString(), id); | ||
} else if (jsonPrimitive.isNumber()) { | ||
return Tlv.newIntegerValue(tlvtype, jsonPrimitive.getAsInt(), id); | ||
|
||
} else if (jsonPrimitive.isBoolean()) { | ||
return Tlv.newBooleanValue(tlvtype, jsonPrimitive.getAsBoolean(), id); | ||
} | ||
} else { | ||
throw new JsonParseException("Missing value"); | ||
} | ||
break; | ||
case OBJECT_INSTANCE: | ||
case MULTIPLE_RESOURCE: | ||
// TODO manage resources field (children) | ||
throw new JsonParseException("OBJECT_INSTANCE and MULTIPLE_RESOURCE not supported"); | ||
} | ||
|
||
} | ||
return null; | ||
} | ||
} |
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