-
Notifications
You must be signed in to change notification settings - Fork 408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SenML-CBOR encoder in leshan-core with unit tests #682
Conversation
Signed-off-by: Boya Zhang <zhangboya@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ask myself, if it makes sense to put cbor serialization code in SenMLPackSerDes class instead of a separated class?
for (SenMLRecord record : pack.getRecords()) { | ||
generator.writeStartObject(); | ||
|
||
if (record.getBaseName() != null && record.getBaseName().length() > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using !record.getBaseName().isEmpty()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not all records have base name within same pack, so that only generate base name element when record contains base name and length great than 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant replacing record.getBaseName().length() > 0
by !record.getBaseName().isEmpty()
(keep the record.getBaseName() != null
)
generator.writeEndArray(); | ||
generator.close(); | ||
} catch (Exception e) { | ||
throw new CodecException(e.getLocalizedMessage(), e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of e.getLocalizedMessage() which is already available in e parameter. Should we add a more contextual exception message, like : "Unable to serialize to CBOR." ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition, do you think CodecException can be used here, because of CodecException is thrown when a problem occurs during LWM2M node encoding or decoding. Or to extend a new Exception class for SenML-CBOR, like TlvException and LwM2MJsonException.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep maybe this is even better. You can create a SenMLCborException
.
+ "fbf613063362f306132fa3f800000ffbf613063362f316132fa40a00000ffbf613063372f306132fa456d8000ffbf6" | ||
+ "13063372f316132fa459c4000ffbf613063382f306132fa42fa0000ffbf613063382f316132fa44610000ffbf61306" | ||
+ "1396132fa42c80000ffbf61306231306132fa41700000ffbf61306431312f306132fa00000000ffbf61306231336132" | ||
+ "fa4ea30485ffbf61306231346133662b30323a3030ffbf613062313561336155ffff"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look like to the CBOR payload example from the specification ? Does it should ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, example in TS is {-2: "/3/0/", 0: "0", 3: "Open Mobile Alliance"}...., however generated CBOR notation in test case is {"-2": "/3/0/", 0: "0", 3: "Open Mobile Alliance"}. Field name shall be number type, instead of string type. I will check with Jackson CBOR library, to correct it.
If to give two seperated classes, one is named SenMLCborPackSerDes.java, another is named SenMLJsonPackSerDes.java, will it make sense? |
Yep, something like this. |
1. Using !record.getBaseName().isEmpty(), instead of record.getBaseName().length() > 0 2. Add SenMLCborException for encoder. 3. Separate SenMLJson and SenMLCbor 4. Update unit test with LwM2M 1.1 TS example. Other updates: 1. Upgrade jackson-dataformat-cbor to 2.9.9 which support number type field. 2. Rename method 'toJsonSenML' to 'toSenMLJson'. Signed-off-by: Boya Zhang <zhangboya@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the long delay...
Never so closed to merge it ;)
|
||
public class SenMLCborSerializerTest extends AbstractSenMLTest { | ||
|
||
private Logger LOG = LoggerFactory.getLogger(getClass()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to not use a static attribute for logger ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After reviewing code, I found that LOGGER can be removed completely.
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class SenMLCborSerializerTest extends AbstractSenMLTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, in this project we use "delegation" instead of "inheritance" for reuse code in tests.
But that's not a big deal, if we let it like this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will take care about it.
Signed-off-by: Boya Zhang <zhangboya@gmail.com>
Not so long time. I was taking break as well in past days. It is time to continue contribution. |
Signed-off-by: Boya Zhang <zhangboya@gmail.com>
Integrated in 2.0.x branch (all commits squashed in 876c1a2) Thx @cavenaghi9 👍 |
Signed-off-by: Boya Zhang <zhangboya@gmail.com>
Signed-off-by: Boya Zhang <zhangboya@gmail.com>
SenML-CBOR encoder in leshan-core with unit tests