Skip to content
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

Closed

Conversation

cavenaghi9
Copy link
Contributor

SenML-CBOR encoder in leshan-core with unit tests

Signed-off-by: Boya Zhang <zhangboya@gmail.com>
Copy link
Contributor

@sbernard31 sbernard31 left a 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) {
Copy link
Contributor

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() ?

Copy link
Contributor Author

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.

Copy link
Contributor

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);
Copy link
Contributor

@sbernard31 sbernard31 Apr 15, 2019

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." ?

Copy link
Contributor Author

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.

Copy link
Contributor

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";
Copy link
Contributor

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 ?

Copy link
Contributor Author

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.

@cavenaghi9
Copy link
Contributor Author

I ask myself, if it makes sense to put cbor serialization code in SenMLPackSerDes class instead of a separated class?

If to give two seperated classes, one is named SenMLCborPackSerDes.java, another is named SenMLJsonPackSerDes.java, will it make sense?

@sbernard31
Copy link
Contributor

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>
Copy link
Contributor

@sbernard31 sbernard31 left a 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());
Copy link
Contributor

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 ?

Copy link
Contributor Author

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 {
Copy link
Contributor

@sbernard31 sbernard31 Jun 14, 2019

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 :)

Copy link
Contributor Author

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>
@cavenaghi9
Copy link
Contributor Author

Sorry for the long delay...
Never so closed to merge it ;)

Not so long time. I was taking break as well in past days. It is time to continue contribution.

sbernard31 pushed a commit that referenced this pull request Jun 19, 2019
Signed-off-by: Boya Zhang <zhangboya@gmail.com>
@sbernard31
Copy link
Contributor

Integrated in 2.0.x branch (all commits squashed in 876c1a2)

Thx @cavenaghi9 👍

@sbernard31 sbernard31 closed this Jun 19, 2019
cavenaghi9 added a commit to cavenaghi9/leshan that referenced this pull request Jun 26, 2019
Signed-off-by: Boya Zhang <zhangboya@gmail.com>
sbernard31 pushed a commit that referenced this pull request Jul 16, 2020
Signed-off-by: Boya Zhang <zhangboya@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants