Search in sources :

Example 1 with LwM2mJsonException

use of org.eclipse.leshan.json.LwM2mJsonException in project leshan by eclipse.

the class LwM2mNodeJsonDecoder method decode.

@SuppressWarnings("unchecked")
public static <T extends LwM2mNode> T decode(byte[] content, LwM2mPath path, LwM2mModel model, Class<T> nodeClass) throws CodecException {
    try {
        String jsonStrValue = content != null ? new String(content) : "";
        JsonRootObject json = LwM2mJson.fromJsonLwM2m(jsonStrValue);
        List<TimestampedLwM2mNode> timestampedNodes = parseJSON(json, path, model, nodeClass);
        if (timestampedNodes.size() == 0) {
            return null;
        } else {
            // return the most recent value
            return (T) timestampedNodes.get(0).getNode();
        }
    } catch (LwM2mJsonException e) {
        throw new CodecException(e, "Unable to deserialize json [path:%s]", path);
    }
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) JsonRootObject(org.eclipse.leshan.json.JsonRootObject) CodecException(org.eclipse.leshan.core.node.codec.CodecException) LwM2mJsonException(org.eclipse.leshan.json.LwM2mJsonException)

Example 2 with LwM2mJsonException

use of org.eclipse.leshan.json.LwM2mJsonException in project leshan by eclipse.

the class LwM2mNodeJsonDecoder method decodeTimestamped.

public static List<TimestampedLwM2mNode> decodeTimestamped(byte[] content, LwM2mPath path, LwM2mModel model, Class<? extends LwM2mNode> nodeClass) throws CodecException {
    try {
        String jsonStrValue = new String(content);
        JsonRootObject json = LwM2mJson.fromJsonLwM2m(jsonStrValue);
        return parseJSON(json, path, model, nodeClass);
    } catch (LwM2mJsonException e) {
        throw new CodecException(e, "Unable to deserialize json [path:%s]", path);
    }
}
Also used : JsonRootObject(org.eclipse.leshan.json.JsonRootObject) CodecException(org.eclipse.leshan.core.node.codec.CodecException) LwM2mJsonException(org.eclipse.leshan.json.LwM2mJsonException)

Aggregations

CodecException (org.eclipse.leshan.core.node.codec.CodecException)2 JsonRootObject (org.eclipse.leshan.json.JsonRootObject)2 LwM2mJsonException (org.eclipse.leshan.json.LwM2mJsonException)2 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)1