Share repeated object keys within a parse (#1091)

This commit is contained in:
labkey-jeckels
2026-10-20 09:54:00 -07:00
parent 69383f746d
commit 293bbf65d3
4 changed files with 214 additions and 1 deletions
+1 -1
View File
@@ -276,7 +276,7 @@ public class JSONObject {
return true;
default:
obj = jsonTokener.nextSimpleValue(c);
key = obj.toString();
key = jsonTokener.shareKey(obj.toString(), jsonParserConfiguration.getMaxSharedKeys());
}
checkKeyForStrictMode(jsonTokener, jsonParserConfiguration, obj);
@@ -4,6 +4,16 @@ package org.json;
* Configuration object for the JSON parser. The configuration is immutable.
*/
public class JSONParserConfiguration extends ParserConfiguration {
/**
* The default maximum number of distinct object keys shared within one parse.
*/
public static final int DEFAULT_MAXIMUM_SHARED_KEYS = 1000;
/**
* Used to indicate that there is no limit on the number of distinct object keys shared within one parse.
*/
public static final int UNLIMITED_SHARED_KEYS = -1;
/**
* Used to indicate whether to overwrite duplicate key or not.
*/
@@ -14,12 +24,18 @@ public class JSONParserConfiguration extends ParserConfiguration {
*/
private boolean useNativeNulls;
/**
* The maximum number of distinct object keys shared within one parse.
*/
private int maxSharedKeys;
/**
* Configuration with the default values.
*/
public JSONParserConfiguration() {
super();
this.overwriteDuplicateKey = false;
this.maxSharedKeys = DEFAULT_MAXIMUM_SHARED_KEYS;
// DO NOT DELETE THE FOLLOWING LINE -- it is used for strictMode testing
// this.strictMode = true;
}
@@ -38,6 +54,7 @@ public class JSONParserConfiguration extends ParserConfiguration {
clone.maxNestingDepth = maxNestingDepth;
clone.keepStrings = keepStrings;
clone.useNativeNulls = useNativeNulls;
clone.maxSharedKeys = maxSharedKeys;
return clone;
}
@@ -89,6 +106,24 @@ public class JSONParserConfiguration extends ParserConfiguration {
return clone;
}
/**
* Defines how many distinct object keys the parser shares within one parse. When a key repeats, for example
* in each element of an array of objects, the parser reuses the first String instance read for that key
* instead of keeping a separate copy in every object. Only the first <code>maxSharedKeys</code> distinct keys
* are shared, so input that begins with many unique keys may need a higher limit. The default is 1000.
* Parsed values are the same with any setting.
*
* @param maxSharedKeys the maximum number of distinct keys to share; 0 disables sharing, and
* {@link #UNLIMITED_SHARED_KEYS} (or any negative value) removes the limit
* @return The existing configuration will not be modified. A new configuration is returned.
*/
public JSONParserConfiguration withMaxSharedKeys(final int maxSharedKeys) {
JSONParserConfiguration clone = this.clone();
clone.maxSharedKeys = Math.max(maxSharedKeys, UNLIMITED_SHARED_KEYS);
return clone;
}
/**
* Sets the strict mode configuration for the JSON parser with default true value
* <p>
@@ -140,6 +175,16 @@ public class JSONParserConfiguration extends ParserConfiguration {
}
/**
* The maximum number of distinct object keys shared within one parse. 0 means keys are not shared,
* and {@link #UNLIMITED_SHARED_KEYS} means there is no limit.
*
* @return The <code>maxSharedKeys</code> configuration value.
*/
public int getMaxSharedKeys() {
return this.maxSharedKeys;
}
/**
* The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters.
* Otherwise, the parser is more relaxed and might tolerate some invalid characters.
+30
View File
@@ -2,6 +2,8 @@ package org.json;
import java.io.*;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
/*
Public Domain.
@@ -35,6 +37,9 @@ public class JSONTokener {
// access to this object is required for strict mode checking
private JSONParserConfiguration jsonParserConfiguration;
/** object keys already read, so repeated keys can share one String instance. */
private Map<String, String> sharedKeys;
/**
* Construct a JSONTokener from a Reader. The caller must close the Reader.
*
@@ -124,6 +129,31 @@ public class JSONTokener {
this.jsonParserConfiguration = jsonParserConfiguration;
}
/**
* Returns an earlier String instance equal to the key if this tokener has read one, otherwise the key itself.
*
* @param key an object key that was just read
* @param maxSharedKeys the maximum number of distinct keys to remember; 0 disables sharing and a negative
* value means no limit
* @return a String equal to <code>key</code>
*/
String shareKey(String key, int maxSharedKeys) {
if (maxSharedKeys == 0) {
return key;
}
if (this.sharedKeys == null) {
this.sharedKeys = new HashMap<String, String>();
}
String shared = this.sharedKeys.get(key);
if (shared != null) {
return shared;
}
if (maxSharedKeys < 0 || this.sharedKeys.size() < maxSharedKeys) {
this.sharedKeys.put(key, key);
}
return key;
}
/**
* Back up one character. This provides a sort of lookahead capability,
* so that you can test for a digit or letter before attempting to parse