| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package org.eclipse.parsson; |
| 18 |
|
| 19 |
import java.security.AccessController; |
| 20 |
import java.security.PrivilegedAction; |
| 21 |
import java.util.Collections; |
| 22 |
import java.util.HashMap; |
| 23 |
import java.util.Map; |
| 24 |
import java.util.Objects; |
| 25 |
|
| 26 |
import jakarta.json.JsonException; |
| 27 |
import jakarta.json.stream.JsonGenerator; |
| 28 |
import org.eclipse.parsson.api.BufferPool; |
| 29 |
import org.eclipse.parsson.api.JsonConfig; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
final class JsonContext { |
| 36 |
|
| 37 |
|
| 38 |
private static final int DEFAULT_MAX_BIGINTEGER_SCALE = 100000; |
| 39 |
|
| 40 |
|
| 41 |
private static final int DEFAULT_MAX_BIGDECIMAL_LEN = 1100; |
| 42 |
|
| 43 |
|
| 44 |
private static final int DEFAULT_MAX_DEPTH = 1000; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static final String PROPERTY_BUFFER_POOL = BufferPool.class.getName(); |
| 50 |
|
| 51 |
private final Map<String, ?> config; |
| 52 |
|
| 53 |
|
| 54 |
private final int bigIntegerScaleLimit; |
| 55 |
|
| 56 |
|
| 57 |
private final int bigDecimalLengthLimit; |
| 58 |
|
| 59 |
|
| 60 |
private final int depthLimit; |
| 61 |
|
| 62 |
|
| 63 |
private final boolean prettyPrinting; |
| 64 |
|
| 65 |
|
| 66 |
private final boolean rejectDuplicateKeys; |
| 67 |
|
| 68 |
private final BufferPool bufferPool; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
JsonContext(Map<String, ?> config, BufferPool defaultPool) { |
| 77 |
this.bigIntegerScaleLimit = getIntConfig(JsonConfig.MAX_BIGINTEGER_SCALE, config, DEFAULT_MAX_BIGINTEGER_SCALE); |
| 78 |
this.bigDecimalLengthLimit = getIntConfig(JsonConfig.MAX_BIGDECIMAL_LEN, config, DEFAULT_MAX_BIGDECIMAL_LEN); |
| 79 |
this.depthLimit = getIntConfig(JsonConfig.MAX_DEPTH, config, DEFAULT_MAX_DEPTH); |
| 80 |
this.prettyPrinting = getBooleanConfig(JsonGenerator.PRETTY_PRINTING, config); |
| 81 |
this.rejectDuplicateKeys = getBooleanConfig(JsonConfig.REJECT_DUPLICATE_KEYS, config); |
| 82 |
this.bufferPool = getBufferPool(config, defaultPool); |
| 83 |
this.config = config != null ? Collections.unmodifiableMap(config) : null; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
JsonContext(Map<String, ?> config, BufferPool defaultPool, String... properties) { |
| 94 |
this.bigIntegerScaleLimit = getIntConfig(JsonConfig.MAX_BIGINTEGER_SCALE, config, DEFAULT_MAX_BIGINTEGER_SCALE); |
| 95 |
this.bigDecimalLengthLimit = getIntConfig(JsonConfig.MAX_BIGDECIMAL_LEN, config, DEFAULT_MAX_BIGDECIMAL_LEN); |
| 96 |
this.depthLimit = getIntConfig(JsonConfig.MAX_DEPTH, config, DEFAULT_MAX_DEPTH); |
| 97 |
this.prettyPrinting = getBooleanConfig(JsonGenerator.PRETTY_PRINTING, config); |
| 98 |
this.rejectDuplicateKeys = getBooleanConfig(JsonConfig.REJECT_DUPLICATE_KEYS, config); |
| 99 |
this.bufferPool = getBufferPool(config, defaultPool); |
| 100 |
this.config = config != null |
| 101 |
? Collections.unmodifiableMap(copyPropertiesMap(this, config, properties)) : null; |
| 102 |
} |
| 103 |
|
| 104 |
Map<String, ?> config() { |
| 105 |
return config; |
| 106 |
} |
| 107 |
|
| 108 |
Object config(String propertyName) { |
| 109 |
return config != null ? config.get(propertyName) : null; |
| 110 |
} |
| 111 |
|
| 112 |
int bigIntegerScaleLimit() { |
| 113 |
return bigIntegerScaleLimit; |
| 114 |
} |
| 115 |
|
| 116 |
int bigDecimalLengthLimit() { |
| 117 |
return bigDecimalLengthLimit; |
| 118 |
} |
| 119 |
|
| 120 |
int depthLimit() { |
| 121 |
return depthLimit; |
| 122 |
} |
| 123 |
|
| 124 |
boolean prettyPrinting() { |
| 125 |
return prettyPrinting; |
| 126 |
} |
| 127 |
|
| 128 |
boolean rejectDuplicateKeys() { |
| 129 |
return rejectDuplicateKeys; |
| 130 |
} |
| 131 |
|
| 132 |
BufferPool bufferPool() { |
| 133 |
return bufferPool; |
| 134 |
} |
| 135 |
|
| 136 |
private static BufferPool getBufferPool(Map<String, ?> config, BufferPool defaultrPool) { |
| 137 |
BufferPool pool = config != null ? (BufferPool)config.get(PROPERTY_BUFFER_POOL) : null; |
| 138 |
return pool != null ? pool : defaultrPool; |
| 139 |
} |
| 140 |
|
| 141 |
private static int getIntConfig(String propertyName, Map<String, ?> config, int defaultValue) throws JsonException { |
| 142 |
|
| 143 |
Integer intConfig = config != null ? getIntProperty(propertyName, config) : null; |
| 144 |
if (intConfig != null) { |
| 145 |
return intConfig; |
| 146 |
} |
| 147 |
|
| 148 |
intConfig = getIntSystemProperty(propertyName); |
| 149 |
return intConfig != null ? intConfig : defaultValue; |
| 150 |
} |
| 151 |
|
| 152 |
private static boolean getBooleanConfig(String propertyName, Map<String, ?> config) throws JsonException { |
| 153 |
|
| 154 |
Boolean booleanConfig = config != null ? getBooleanProperty(propertyName, config) : null; |
| 155 |
if (booleanConfig != null) { |
| 156 |
return booleanConfig; |
| 157 |
} |
| 158 |
|
| 159 |
return getBooleanSystemProperty(propertyName); |
| 160 |
} |
| 161 |
|
| 162 |
private static Integer getIntProperty(String propertyName, Map<String, ?> config) throws JsonException { |
| 163 |
Object property = config.get(propertyName); |
| 164 |
if (property == null) { |
| 165 |
return null; |
| 166 |
} |
| 167 |
if (property instanceof Number) { |
| 168 |
return ((Number) property).intValue(); |
| 169 |
} |
| 170 |
if (property instanceof String) { |
| 171 |
return propertyStringToInt(propertyName, (String) property); |
| 172 |
} |
| 173 |
throw new JsonException( |
| 174 |
String.format("Could not convert %s property of type %s to Integer", |
| 175 |
propertyName, property.getClass().getName())); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
private static Boolean getBooleanProperty(String propertyName, Map<String, ?> config) throws JsonException { |
| 180 |
return config.containsKey(propertyName) ? true : null; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
private static Integer getIntSystemProperty(String propertyName) throws JsonException { |
| 185 |
String systemProperty = getSystemProperty(propertyName); |
| 186 |
if (systemProperty == null) { |
| 187 |
return null; |
| 188 |
} |
| 189 |
return propertyStringToInt(propertyName, systemProperty); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
private static boolean getBooleanSystemProperty(String propertyName) throws JsonException { |
| 194 |
return getSystemProperty(propertyName) != null; |
| 195 |
} |
| 196 |
|
| 197 |
@SuppressWarnings("removal") |
| 198 |
private static String getSystemProperty(String propertyName) throws JsonException { |
| 199 |
if (System.getSecurityManager() != null) { |
| 200 |
return AccessController.doPrivileged( |
| 201 |
(PrivilegedAction<String>) () -> System.getProperty(propertyName)); |
| 202 |
} else { |
| 203 |
return System.getProperty(propertyName); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
private static int propertyStringToInt(String propertyName, String propertyValue) throws JsonException { |
| 208 |
try { |
| 209 |
return Integer.parseInt(propertyValue); |
| 210 |
} catch (NumberFormatException ex) { |
| 211 |
throw new JsonException( |
| 212 |
String.format("Value of %s property is not a number", propertyName), ex); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
private static Map<String, ?> copyPropertiesMap(JsonContext instance, Map<String, ?> config, String... properties) { |
| 220 |
Objects.requireNonNull(config, "Map of provider specific properties is null"); |
| 221 |
if (properties == null || properties.length == 0) { |
| 222 |
return Collections.emptyMap(); |
| 223 |
} |
| 224 |
Map<String, Object> newConfig = new HashMap<>(properties.length); |
| 225 |
for (String propertyName : properties) { |
| 226 |
switch (propertyName) { |
| 227 |
|
| 228 |
case JsonGenerator.PRETTY_PRINTING: |
| 229 |
if (instance.prettyPrinting) { |
| 230 |
newConfig.put(JsonGenerator.PRETTY_PRINTING, true); |
| 231 |
} |
| 232 |
break; |
| 233 |
case JsonConfig.REJECT_DUPLICATE_KEYS: |
| 234 |
if (instance.rejectDuplicateKeys) { |
| 235 |
newConfig.put(JsonConfig.REJECT_DUPLICATE_KEYS, true); |
| 236 |
} |
| 237 |
|
| 238 |
default: |
| 239 |
if (config.containsKey(propertyName)) { |
| 240 |
newConfig.put(propertyName, config.get(propertyName)); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
return newConfig; |
| 245 |
} |
| 246 |
|
| 247 |
} |