eclipse-leshan / leshan

Java Library for LWM2M
https://www.eclipse.org/leshan/
BSD 3-Clause "New" or "Revised" License
647 stars 407 forks source link

Do Leshan support payload case which value is null in SENML_JSON/JSON format? #982

Closed pengxiaopeng closed 3 years ago

pengxiaopeng commented 3 years ago

I found that Leshan decoder will throw exception when the payload is [{"bn":"/3/0/3","vs":null}] (SENML_JSON format) will get exception

Caused by: org.eclipse.leshan.core.util.json.JsonException: Invalid SenML record : record must have a value (v,vb,vlo,vd,vs) : {"n":"3","vs":null}

Or payload is {"bn":"/3/0/","e":[{"n":"3","vs":null}] (JSON format) will get exception

Caused by: org.eclipse.leshan.core.util.json.JsonException: Invalid SenML record : record must have a value (v,vb,vlo,vd,vs) : {"n":"3","vs":null}

These cases are not acceptable for lwM2M protocol? Or these exceptions refer to which section of lwM2M specification?

Thank you and I am looking forward to your reply.

sbernard31 commented 3 years ago

This is not clearly said but there is several hints which lets me think this is not allowed.

  1. The SenML RFC defines JSON type for value fields ("v,vb,vlo,vd,vs") . And it seems this is either String , Number or Boolean. There is no null mentioned. They also said that Value is required. There is also no example with null value. This is some hints but not so strong as I guess you could consider that null is a value and a valid value for String, Number or Boolean. (I guess for Sum this will not works so well)
  2. In LWM2M for all data type there is no explanation on how to encode null value. (Note that they talk about null but just for resource which have none datatype which does not make too much sense to me because if there is no datatype there is nothing to encode :exploding_head: ...). Note there is no example too. So this is more little hints.

In core§7.4.5-SenMLJSON

The current format does not support the use of a "null" value in a SenML record to indicate the deletion of a specific Resource Instance. The media types, application/senml-etch+json and application/senml-etch+cbor, will remove this constraint.

If you looked at FETCH and PATCH with Sensor Measurement Lists (SenML) RFC they said :

The key differences from the SenML media type are allowing the use of a "null" value for removing Records with the (i)PATCH method and the lack of value fields in Fetch Records.

This seems to go in the "not null allowed" direction and sounds to be a stronger hint. So for now we consider that null is not allowed. But I guess you could say "this is not allowed just for deletion but nothing say this is not allowed for other use case." If you want we can try to ask question to OMA or IETF to clarify this but not sure we succeed to get answer and this could be long.

Currently Leshan does not allow null value at all (or at least this is the expected behavior). This is true for all content format (not only SENML) because there is not enough detail in the specification about how to handle this. If a resource has no value we return NOT_FOUND. In your case of String DataType (3/0/3), you can also imagine to return an empty string.

sbernard31 commented 3 years ago

Or payload is {"bn":"/3/0/","e":[{"n":"3","sv":null}] (JSON format) will get exception

I guess you mean "vs:null" ?

pengxiaopeng commented 3 years ago

Or payload is {"bn":"/3/0/","e":[{"n":"3","sv":null}] (JSON format) will get exception

I guess you mean "vs:null" ?

Yes, you are right. It is a clerical error. I had corrected it.

pengxiaopeng commented 3 years ago

This is not clearly said but there is several hints which lets me think this is not allowed.

  1. The SenML RFC defines JSON type for value fields ("v,vb,vlo,vd,vs") . And it seems this is either String , Number or Boolean. There is no null mentioned. They also said that Value is required. There is also no example with null value. This is some hints but not so strong as I guess you could consider that null is a value and a valid value for String, Number or Boolean. (I guess for Sum this will not works so well)
  2. In LWM2M for all data type there is no explanation on how to encode null value. (Note that they talk about null but just for resource which have none datatype which does not make too much sense to me because if there is no datatype there is nothing to encode 🤯 ...). Note there is no example too. So this is more little hints.

In core§7.4.5-SenMLJSON

The current format does not support the use of a "null" value in a SenML record to indicate the deletion of a specific Resource Instance. The media types, application/senml-etch+json and application/senml-etch+cbor, will remove this constraint.

If you looked at FETCH and PATCH with Sensor Measurement Lists (SenML) RFC they said :

The key differences from the SenML media type are allowing the use of a "null" value for removing Records with the (i)PATCH method and the lack of value fields in Fetch Records.

This seems to go in the "not null allowed" direction and sounds to be a stronger hint. So for now we consider that null is not allowed. But I guess you could say "this is not allowed just for deletion but nothing say this is not allowed for other use case." If you want we can try to ask question to OMA or IETF to clarify this but not sure we succeed to get answer and this could be long.

Currently Leshan does not allow null value at all (or at least this is the expected behavior). This is true for all content format (not only SENML) because there is not enough detail in the specification about how to handle this. If a resource has no value we return NOT_FOUND. In your case of String DataType (3/0/3), you can also imagine to return an empty string.

Thank you very much for your detailed reply!