Skip to content

Commit

Permalink
Fix #131: add method for extensions to check state of JSON.Features (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Mar 8, 2024
1 parent 68f23bf commit 084bdad
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,11 @@ private static class ExtContextImpl extends ExtensionContext {
_builder = b;
}

@Override
public boolean isEnabled(JSON.Feature feature) {
return _builder.isEnabled(feature);
}

@Override
public ExtensionContext setTreeCodec(TreeCodec tc) {
_builder.treeCodec(tc);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.jr.ob.api;

import com.fasterxml.jackson.core.TreeCodec;
import com.fasterxml.jackson.jr.ob.JSON;

/**
* API that lets {@link com.fasterxml.jackson.jr.ob.JacksonJrExtension}s to register handlers
Expand All @@ -10,6 +11,19 @@
*/
public abstract class ExtensionContext
{
// // // Config access

/**
* Method for checking whether given {@code JSON.Feature} is enabled.
*
* @param feature Feature to check
*
* @return True if given {@code JSON.Feature} is enabled; {@code false} if not
*
* @since 2.17
*/
public abstract boolean isEnabled(JSON.Feature feature);

// // // Override of (and access to) singleton handlers

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public class JacksonJrsTreeCodec extends TreeCodec

public static final JacksonJrsTreeCodec SINGLETON = new JacksonJrsTreeCodec();

protected ObjectCodec _objectCodec;
protected final ObjectCodec _objectCodec;

// @since 2.17
protected boolean _failOnDuplicateKeys;

public JacksonJrsTreeCodec() {
this(null);
}
Expand All @@ -26,6 +29,11 @@ public JacksonJrsTreeCodec(ObjectCodec codec) {
_objectCodec = codec;
}

// @since 2.17
public void setFailOnDuplicateKeys(boolean state) {
_failOnDuplicateKeys = state;
}

@SuppressWarnings("unchecked")
@Override
public <T extends TreeNode> T readTree(JsonParser p) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.jr.stree;

import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.JacksonJrExtension;
import com.fasterxml.jackson.jr.ob.api.ExtensionContext;

Expand All @@ -23,9 +24,10 @@ public JrSimpleTreeExtension() {
public JrSimpleTreeExtension(JacksonJrsTreeCodec tc) {
_codec = tc;
}

@Override
protected void register(ExtensionContext ctxt) {
_codec.setFailOnDuplicateKeys(ctxt.isEnabled(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS));
ctxt.setTreeCodec(_codec);
}
}
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Not yet released

#7: Support deserialization of `int[]`
(contributed by @Shounaks)
#131: Add mechanism for `JacksonJrExtension`s to access state of `JSON.Feature`s

2.17.0-rc1 (26-Feb-2024)

Expand Down

0 comments on commit 084bdad

Please sign in to comment.