diff --git a/jr-stree/src/main/java/com/fasterxml/jackson/jr/stree/JacksonJrsTreeCodec.java b/jr-stree/src/main/java/com/fasterxml/jackson/jr/stree/JacksonJrsTreeCodec.java index 94550cf5..6e14afe5 100644 --- a/jr-stree/src/main/java/com/fasterxml/jackson/jr/stree/JacksonJrsTreeCodec.java +++ b/jr-stree/src/main/java/com/fasterxml/jackson/jr/stree/JacksonJrsTreeCodec.java @@ -64,11 +64,11 @@ private JrsValue nodeFrom(JsonParser p) throws IOException Map values = _map(); while (p.nextToken() != JsonToken.END_OBJECT) { final String currentName = p.currentName(); - if (duplicateCheck(values, currentName)) { + p.nextToken(); + JrsValue prev = values.put(currentName, nodeFrom(p)); + if (_failOnDuplicateKeys && (prev != null)) { throw new JSONObjectException("Duplicate key (key '" + currentName + "')"); } - p.nextToken(); - values.put(currentName, nodeFrom(p)); } return new JrsObject(values); } @@ -84,10 +84,6 @@ private JrsValue nodeFrom(JsonParser p) throws IOException throw new UnsupportedOperationException("Unsupported token id " + tokenId + " (" + p.currentToken() + ")"); } - private boolean duplicateCheck(Map values, String currentName) { - return _failOnDuplicateKeys && values.containsKey(currentName); - } - @Override public void writeTree(JsonGenerator g, TreeNode treeNode) throws IOException { if (treeNode == null) { diff --git a/jr-stree/src/test/java/com/fasterxml/jackson/jr/stree/DupFieldNameInTree51Test.java b/jr-stree/src/test/java/com/fasterxml/jackson/jr/stree/DupFieldNameInTree51Test.java index 42097a8a..0869bfdf 100644 --- a/jr-stree/src/test/java/com/fasterxml/jackson/jr/stree/DupFieldNameInTree51Test.java +++ b/jr-stree/src/test/java/com/fasterxml/jackson/jr/stree/DupFieldNameInTree51Test.java @@ -7,37 +7,32 @@ * Tests for reading content using {@link JSON} with proper * codec registration */ -public class DupFieldNameInTree51Test extends JacksonJrTreeTestBase { - +public class DupFieldNameInTree51Test extends JacksonJrTreeTestBase +{ + private final JSON NO_DUPS_JSON = JSON.builder() + .enable(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS) + .register(new JrSimpleTreeExtension()) + .build(); + + private final JSON DUPS_OK_JSON = JSON.builder() + .disable(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS) + .register(new JrSimpleTreeExtension()) + .build(); + // [jackson-jr#51]: test dup keys for trees too - public void testFailOnDupMapKeys() throws Exception { - JSON j = JSON.builder() - .enable(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS) - .build(); - - final JSON treeJSON = jsonWithTreeCodec(j); - - assertTrue(j.isEnabled(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS)); + public void testFailOnDupMapKeys() throws Exception + { + assertTrue(NO_DUPS_JSON.isEnabled(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS)); final String json = "{\"a\":1,\"b\":2,\"b\":3,\"c\":4}"; try { - /*TreeNode node =*/ treeJSON.treeFrom(json); + /*TreeNode node =*/ NO_DUPS_JSON.treeFrom(json); + fail("Should not pass"); } catch (JSONObjectException e) { verifyException(e, "Duplicate key"); } - } - - public void testFailOnDupMapKeys2() throws Exception { - //missing flag - Enabled by default! - JSON j = JSON.builder().build(); - - final JSON treeJSON = jsonWithTreeCodec(j); - assertTrue(j.isEnabled(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS)); - final String json = "{\"a\":1,\"b\":2,\"b\":3,\"c\":4}"; - try { - /*TreeNode node =*/ treeJSON.treeFrom(json); - } catch (JSONObjectException e) { - verifyException(e, "Duplicate key"); - } + assertFalse(DUPS_OK_JSON.isEnabled(JSON.Feature.FAIL_ON_DUPLICATE_MAP_KEYS)); + // But should pass fine without setting + assertNotNull(DUPS_OK_JSON.treeFrom(json)); } } diff --git a/jr-stree/src/test/java/com/fasterxml/jackson/jr/stree/JacksonJrTreeTestBase.java b/jr-stree/src/test/java/com/fasterxml/jackson/jr/stree/JacksonJrTreeTestBase.java index 9f00883e..1422c079 100644 --- a/jr-stree/src/test/java/com/fasterxml/jackson/jr/stree/JacksonJrTreeTestBase.java +++ b/jr-stree/src/test/java/com/fasterxml/jackson/jr/stree/JacksonJrTreeTestBase.java @@ -44,14 +44,6 @@ protected String a2q(String json) { return json.replace("'", "\""); } - protected JSON jsonWithTreeCodec(JSON config) { - return JSON.builder() - // 13-Feb-2020, tatu: There are 2 different ways actually.. -// .treeCodec(new JacksonJrsTreeCodec()) - .register(new JrSimpleTreeExtension()) - .build(); - } - protected JSON jsonWithTreeCodec() { return JSON.builder() // 13-Feb-2020, tatu: There are 2 different ways actually..