Skip to content

Commit

Permalink
Final touches before merge
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 11, 2024
1 parent 21476de commit 2862feb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ private JrsValue nodeFrom(JsonParser p) throws IOException
Map<String, JrsValue> 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);
}
Expand All @@ -84,10 +84,6 @@ private JrsValue nodeFrom(JsonParser p) throws IOException
throw new UnsupportedOperationException("Unsupported token id " + tokenId + " (" + p.currentToken() + ")");
}

private boolean duplicateCheck(Map<String, JrsValue> values, String currentName) {
return _failOnDuplicateKeys && values.containsKey(currentName);
}

@Override
public void writeTree(JsonGenerator g, TreeNode treeNode) throws IOException {
if (treeNode == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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..
Expand Down

0 comments on commit 2862feb

Please sign in to comment.