Skip to content

Commit

Permalink
GH-1504: Make SenMLPack,SenMLRecord,JsonArrayEntry,JsonRootObject final
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslawLegierski authored and sbernard31 committed Jan 9, 2025
1 parent f954615 commit 3478efd
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,27 @@

public class JsonArrayEntry {

private String name;
private final String name;

private Number floatValue;
private final Number floatValue;

private Boolean booleanValue;
private final Boolean booleanValue;

private String objectLinkValue;
private final String objectLinkValue;

private String stringValue;
private final String stringValue;

private BigDecimal time;
private final BigDecimal time;

public JsonArrayEntry(String name, Number floatValue, Boolean booleanValue, String objectLinkValue,
String stringValue, BigDecimal time) {
this.name = name;
this.floatValue = floatValue;
this.booleanValue = booleanValue;
this.objectLinkValue = objectLinkValue;
this.stringValue = stringValue;
this.time = time;
}

public ResourceModel.Type getType() {
if (booleanValue != null) {
Expand All @@ -57,50 +67,26 @@ public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public BigDecimal getTime() {
return time;
}

public void setTime(BigDecimal time) {
this.time = time;
}

public Number getFloatValue() {
return floatValue;
}

public void setFloatValue(Number floatValue) {
this.floatValue = floatValue;
}

public Boolean getBooleanValue() {
return booleanValue;
}

public void setBooleanValue(Boolean booleanValue) {
this.booleanValue = booleanValue;
}

public String getObjectLinkValue() {
return objectLinkValue;
}

public void setObjectLinkValue(String objectLinkValue) {
this.objectLinkValue = objectLinkValue;
}

public String getStringValue() {
return stringValue;
}

public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}

public Object getResourceValue() {

if (booleanValue != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.eclipse.leshan.core.json;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand All @@ -27,35 +28,26 @@
*/
public class JsonRootObject {

private String baseName = null;
private final String baseName;

private List<JsonArrayEntry> jsonArray;
private final List<JsonArrayEntry> jsonArray;

private BigDecimal baseTime;
private final BigDecimal baseTime;

public JsonRootObject() {
public JsonRootObject(String baseName, List<JsonArrayEntry> jsonArray, BigDecimal baseTime) {
this.baseName = baseName;
this.jsonArray = Collections.unmodifiableList(new ArrayList<>(jsonArray));
this.baseTime = baseTime;
}

public String getBaseName() {
return baseName;
}

public void setBaseName(String baseName) {
this.baseName = baseName;
}

public BigDecimal getBaseTime() {
return baseTime;
}

public void setBaseTime(BigDecimal baseTime) {
this.baseTime = baseTime;
}

public void setResourceList(List<JsonArrayEntry> jsonArray) {
this.jsonArray = jsonArray;
}

public List<JsonArrayEntry> getResourceList() {
if (jsonArray == null)
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,38 @@ public JsonArrayEntry deserialize(JsonNode o) throws JsonException {
if (o == null)
return null;

JsonArrayEntry jae = new JsonArrayEntry();
String name = null;
BigDecimal time = null;
Number floatValue = null;
Boolean booleanValue = null;
String stringValue = null;
String objectLinkValue = null;

JsonNode n = o.get("n");
if (n != null && n.isTextual())
jae.setName(n.asText());
name = n.asText();

JsonNode t = o.get("t");
if (t != null && t.isNumber())
jae.setTime(new BigDecimal(t.asText()));
time = new BigDecimal(t.asText());

JsonNode v = o.get("v");
if (v != null && v.isNumber())
jae.setFloatValue(v.numberValue());
floatValue = v.numberValue();

JsonNode bv = o.get("bv");
if (bv != null && bv.isBoolean())
jae.setBooleanValue(bv.asBoolean());
booleanValue = bv.asBoolean();

JsonNode sv = o.get("sv");
if (sv != null && sv.isTextual())
jae.setStringValue(sv.asText());
stringValue = sv.asText();

JsonNode ov = o.get("ov");
if (ov != null && ov.isTextual())
jae.setObjectLinkValue(ov.asText());
objectLinkValue = ov.asText();

JsonArrayEntry jae = new JsonArrayEntry(name, floatValue, booleanValue, objectLinkValue, stringValue, time);

if (jae.getType() == null) {
throw new JsonException("Missing value(v,bv,ov,sv) field for entry %s", o.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package org.eclipse.leshan.core.json.jackson;

import java.math.BigDecimal;
import java.util.List;

import org.eclipse.leshan.core.json.JsonArrayEntry;
import org.eclipse.leshan.core.json.JsonRootObject;
import org.eclipse.leshan.core.util.json.JacksonJsonSerDes;
import org.eclipse.leshan.core.util.json.JsonException;
Expand Down Expand Up @@ -52,22 +54,24 @@ public JsonRootObject deserialize(JsonNode jsonNode) throws JsonException {
if (jsonNode == null)
return null;

JsonRootObject jro = new JsonRootObject();
List<JsonArrayEntry> ResourceList;
String baseName = null;
BigDecimal baseTime = null;

JsonNode e = jsonNode.get("e");
if (e != null && e.isArray())
jro.setResourceList(serDes.deserialize(e.elements()));
ResourceList = serDes.deserialize(e.elements());
else
throw new JsonException("'e' field is missing for %s", jsonNode.toString());

JsonNode bn = jsonNode.get("bn");
if (bn != null && bn.isTextual())
jro.setBaseName(bn.asText());
baseName = bn.asText();

JsonNode bt = jsonNode.get("bt");
if (bt != null && bt.isNumber())
jro.setBaseTime(new BigDecimal(bt.asText()));
baseTime = new BigDecimal(bt.asText());

return jro;
return new JsonRootObject(baseName, ResourceList, baseTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ public byte[] encode(LwM2mNode node, String rootPath, LwM2mPath path, LwM2mModel
internalEncoder.requestPath = path;
internalEncoder.converter = converter;
node.accept(internalEncoder);
JsonRootObject jsonObject = new JsonRootObject();
jsonObject.setResourceList(internalEncoder.resourceList);
jsonObject.setBaseName(internalEncoder.baseName);
JsonRootObject jsonObject = new JsonRootObject(internalEncoder.baseName, internalEncoder.resourceList, null);
try {
return encoder.toJsonLwM2m(jsonObject).getBytes();
} catch (LwM2mJsonException e) {
Expand Down Expand Up @@ -126,9 +124,7 @@ public byte[] encodeTimestampedData(List<TimestampedLwM2mNode> timestampedNodes,
baseName = internalEncoder.baseName;
}
}
JsonRootObject jsonObject = new JsonRootObject();
jsonObject.setResourceList(entries);
jsonObject.setBaseName(internalEncoder.baseName);
JsonRootObject jsonObject = new JsonRootObject(internalEncoder.baseName, entries, null);
try {
return encoder.toJsonLwM2m(jsonObject).getBytes();
} catch (LwM2mJsonException e) {
Expand Down Expand Up @@ -263,61 +259,68 @@ private ArrayList<JsonArrayEntry> lwM2mResourceToJsonArrayEntry(String resourceP
private JsonArrayEntry createJsonArrayEntry(String name, BigDecimal timestampInSeconds, Type type,
Type expectedType, Object value) {
// Create resource element
JsonArrayEntry jsonResourceElt = new JsonArrayEntry();
jsonResourceElt.setName(name);
jsonResourceElt.setTime(timestampInSeconds);
JsonArrayEntry jsonResourceElt = new JsonArrayEntry(name, null, null, null, null, timestampInSeconds);

// Convert value using expected type
LwM2mPath lwM2mResourcePath = name != null ? new LwM2mPath(name) : null;
Object convertedValue = converter.convertValue(value, type, expectedType, lwM2mResourcePath);
this.setResourceValue(convertedValue, expectedType, jsonResourceElt, lwM2mResourcePath);
jsonResourceElt = this.setResourceValue(convertedValue, expectedType, jsonResourceElt, lwM2mResourcePath);

return jsonResourceElt;
}

private void setResourceValue(Object value, Type type, JsonArrayEntry jsonResource, LwM2mPath resourcePath) {
private JsonArrayEntry setResourceValue(Object value, Type type, JsonArrayEntry jsonResource,
LwM2mPath resourcePath) {
LOG.trace("Encoding value {} in JSON", value);

if (type == null || type == Type.NONE) {
throw new CodecException(
"Unable to encode value for resource {} without type(probably a executable one)", resourcePath);
}

String name = jsonResource.getName();
BigDecimal time = jsonResource.getTime();
Number floatValue = jsonResource.getFloatValue();
Boolean booleanValue = jsonResource.getBooleanValue();
String stringValue = jsonResource.getStringValue();
String objectLinkValue = jsonResource.getObjectLinkValue();

// Following table 20 in the Specs
switch (type) {
case STRING:
jsonResource.setStringValue((String) value);
stringValue = (String) value;
break;
case INTEGER:
case UNSIGNED_INTEGER:
case FLOAT:
jsonResource.setFloatValue((Number) value);
floatValue = (Number) value;
break;
case BOOLEAN:
jsonResource.setBooleanValue((Boolean) value);
booleanValue = (Boolean) value;
break;
case TIME:
// Specs device object example page 44, rec 13 is Time
// represented as float?
jsonResource.setFloatValue((((Date) value).getTime() / 1000L));
floatValue = (((Date) value).getTime() / 1000L);
break;
case OPAQUE:
jsonResource.setStringValue(base64Encoder.encode((byte[]) value));
stringValue = base64Encoder.encode((byte[]) value);
break;
case OBJLNK:
try {
jsonResource.setStringValue(((ObjectLink) value).encodeToString());
stringValue = ((ObjectLink) value).encodeToString();
} catch (IllegalArgumentException e) {
throw new CodecException(e, "Invalid value [%s] for objectLink resource [%s] ", value,
resourcePath);
}
break;
case CORELINK:
jsonResource.setStringValue(linkSerializer.serializeCoreLinkFormat((Link[]) value));
stringValue = linkSerializer.serializeCoreLinkFormat((Link[]) value);
break;
default:
throw new CodecException("Invalid value type %s for %s", type, resourcePath);
}
return new JsonArrayEntry(name, floatValue, booleanValue, objectLinkValue, stringValue, time);
}
}
}
Loading

0 comments on commit 3478efd

Please sign in to comment.