Skip to content

Allow exposing CBOR undefined value as JsonToken.VALUE_EMBEDDED_OBJECT #588

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

Merged
merged 3 commits into from
May 6, 2025

Conversation

iifawzi
Copy link
Contributor

@iifawzi iifawzi commented May 5, 2025

Hello, this PR resolves #137 by

  • Adding a feature flag to decode undefined value as JsonToken.VALUE_EMBEDDED_OBJECT
  • Adding isUndefined method that could be used for both legacy handling and with embedded objects, as an indicator if the last token parsed is undefined or not.

@iifawzi
Copy link
Contributor Author

iifawzi commented May 5, 2025

Defaults for 3.0 should be true, I believe, so it could take place in a separate PR

@iifawzi
Copy link
Contributor Author

iifawzi commented May 5, 2025

I'm not sure if we need to touch getEmbeddedObject, now it will return null, I couldn't think of any other alternative than returning the string "undefined" but not feeling so strong about it. I think null maybe remains valid..

@@ -113,6 +113,8 @@ public final class CBORConstants

public final static int INT_BREAK = 0xFF;

public final static int SIMPLE_VALUE_UNDEFINED = 0xF7;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use a Javadoc

@cowtowncoder
Copy link
Member

I'm not sure if we need to touch getEmbeddedObject, now it will return null, I couldn't think of any other alternative than returning the string "undefined" but not feeling so strong about it. I think null maybe remains valid..

Good question. I don't think constant String would be any better.

But somehow not setting anything does seem odd. I think we can go with that; as long as behavior is tested I think we are ok. Will add one specific suggestion on code, for clearing _binaryValue explicitly: while redundant, works as sort of documentation.

The only (?) concern I have is about future embedded values we might want to return, and whether null having specific semantics might be problematic. But that's hypothetical for now so... no need to solve problem we do not have.

* @since 2.20
*/
public boolean isUndefined() {
return (_inputBuffer[_inputPtr - 1] & 0xFF) == SIMPLE_VALUE_UNDEFINED;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmh. Not a fan of this approach but... I guess it'll do. Let's preface it by

(_currToken == JsonToken.VALUE_EMBEDDED_OBJECT || _currToken == JsonToken.VALUE_NULL)

to avoid likelihood of incorrect deterimation (binary segment ending with byte 0xF7).

Also: not 100% sure if bounds checks would be needed.
Actually, must check that _inputBuffer is not null -- _releaseBuffers() clears it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I can add that.

*
* @since 2.9.6
*/
protected JsonToken _decodeUndefinedValue() throws IOException {
protected JsonToken _decodeUndefinedValue() {
if (CBORParser.Feature.HANDLE_UNDEFINED_AS_EMBEDDED_OBJECT.enabledIn(_formatFeatures)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add _binaryValue = null; just as sort of documentation of intent. I know it should be cleared by nextToken() and other places but it seems better be explicit in this particular case.

@cowtowncoder
Copy link
Member

Ok I added release notes and made minor tweaks -- actually can add Javadoc too and then merge.

@cowtowncoder cowtowncoder merged commit 6504b46 into FasterXML:2.x May 6, 2025
4 checks passed
@cowtowncoder
Copy link
Member

@iifawzi Merged into 2.20, will merge forward into 3.x, so separate PR just needed for defaults change and unit tests (likely).

@iifawzi
Copy link
Contributor Author

iifawzi commented May 6, 2025

I'm not sure if we need to touch getEmbeddedObject, now it will return null, I couldn't think of any other alternative than returning the string "undefined" but not feeling so strong about it. I think null maybe remains valid..

Good question. I don't think constant String would be any better.

But somehow not setting anything does seem odd. I think we can go with that; as long as behavior is tested I think we are ok. Will add one specific suggestion on code, for clearing _binaryValue explicitly: while redundant, works as sort of documentation.

The only (?) concern I have is about future embedded values we might want to return, and whether null having specific semantics might be problematic. But that's hypothetical for now so... no need to solve problem we do not have.

Thanks for doing the changes, I also believe for future embedded values (SimpleValue) we need to return the value itself as null would be meaningless and unexpected, but yes, maybe we discuss it in the corresponding pr later

Thank you for completing the missing points

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.

Allow exposing CBOR "undefined" value as JsonToken.VALUE_EMBEDDED_OBJECT (with embedded value of null)
2 participants