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();
}
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);
}
}
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);
}
}
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();
}
Aggregations