Search in sources :

Example 1 with JsonRootObject

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

the class LwM2mNodeJsonEncoder method encodeTimestampedData.

public static byte[] encodeTimestampedData(List<TimestampedLwM2mNode> timestampedNodes, LwM2mPath path, LwM2mModel model, LwM2mValueConverter converter) {
    Validate.notNull(timestampedNodes);
    Validate.notNull(path);
    Validate.notNull(model);
    InternalEncoder internalEncoder = new InternalEncoder();
    ArrayList<JsonArrayEntry> entries = new ArrayList<>();
    for (TimestampedLwM2mNode timestampedLwM2mNode : timestampedNodes) {
        internalEncoder.objectId = path.getObjectId();
        internalEncoder.model = model;
        internalEncoder.requestPath = path;
        internalEncoder.converter = converter;
        internalEncoder.resourceList = null;
        internalEncoder.timestamp = timestampedLwM2mNode.getTimestamp();
        timestampedLwM2mNode.getNode().accept(internalEncoder);
        entries.addAll(internalEncoder.resourceList);
    }
    JsonRootObject jsonObject = new JsonRootObject();
    jsonObject.setResourceList(entries);
    jsonObject.setBaseName(path.toString());
    return LwM2mJson.toJsonLwM2m(jsonObject).getBytes();
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) ArrayList(java.util.ArrayList) JsonRootObject(org.eclipse.leshan.json.JsonRootObject) JsonArrayEntry(org.eclipse.leshan.json.JsonArrayEntry)

Example 2 with JsonRootObject

use of org.eclipse.leshan.json.JsonRootObject 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 3 with JsonRootObject

use of org.eclipse.leshan.json.JsonRootObject 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)

Example 4 with JsonRootObject

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

the class LwM2mNodeJsonEncoder method encode.

public static byte[] encode(LwM2mNode node, LwM2mPath path, LwM2mModel model, LwM2mValueConverter converter) throws CodecException {
    Validate.notNull(node);
    Validate.notNull(path);
    Validate.notNull(model);
    InternalEncoder internalEncoder = new InternalEncoder();
    internalEncoder.objectId = path.getObjectId();
    internalEncoder.model = model;
    internalEncoder.requestPath = path;
    internalEncoder.converter = converter;
    node.accept(internalEncoder);
    JsonRootObject jsonObject = new JsonRootObject();
    jsonObject.setResourceList(internalEncoder.resourceList);
    jsonObject.setBaseName(path.toString());
    return LwM2mJson.toJsonLwM2m(jsonObject).getBytes();
}
Also used : JsonRootObject(org.eclipse.leshan.json.JsonRootObject)

Aggregations

JsonRootObject (org.eclipse.leshan.json.JsonRootObject)4 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)2 CodecException (org.eclipse.leshan.core.node.codec.CodecException)2 LwM2mJsonException (org.eclipse.leshan.json.LwM2mJsonException)2 ArrayList (java.util.ArrayList)1 JsonArrayEntry (org.eclipse.leshan.json.JsonArrayEntry)1