Debian Codegraph @

src/org/eclipse/parsson/JsonContext.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
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 * Parsson configuration.
33 * Values are composed from properties {@code Map}, system properties and default value.
34 */
35 final class JsonContext {
36
37 /** Default maximum value of BigInteger scale value limit. */
38 private static final int DEFAULT_MAX_BIGINTEGER_SCALE = 100000;
39
40 /** Default maximum number of characters of BigDecimal source being parsed. */
41 private static final int DEFAULT_MAX_BIGDECIMAL_LEN = 1100;
42
43 /** Default maximum level of nesting. */
44 private static final int DEFAULT_MAX_DEPTH = 1000;
45
46 /**
47 * Custom char[] pool instance property. Can be set in properties {@code Map} only.
48 */
49 static final String PROPERTY_BUFFER_POOL = BufferPool.class.getName();
50
51 private final Map<String, ?> config;
52
53 // Maximum value of BigInteger scale value
54 private final int bigIntegerScaleLimit;
55
56 // Maximum number of characters of BigDecimal source
57 private final int bigDecimalLengthLimit;
58
59 // Maximum depth to parse
60 private final int depthLimit;
61
62 // Whether JSON pretty printing is enabled
63 private final boolean prettyPrinting;
64
65 // Whether duplicate keys in JsonObject shall be rejected.
66 private final boolean rejectDuplicateKeys;
67
68 private final BufferPool bufferPool;
69
70 /**
71 * Creates an instance of Parsson configuration.
72 *
73 * @param config a {@code Map} of provider specific properties to configure the JSON parsers
74 * @param defaultPool default char[] pool to use when no instance is configured
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 * Creates an instance of Parsson configuration.
88 *
89 * @param config a map of provider specific properties to configure the JSON parsers
90 * @param defaultPool default char[] pool to use when no instance is configured
91 * @param properties properties to store in local copy of provider specific properties {@code Map}
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 // Try config Map first
143 Integer intConfig = config != null ? getIntProperty(propertyName, config) : null;
144 if (intConfig != null) {
145 return intConfig;
146 }
147 // Try system properties as fallback.
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 // Try config Map first
154 Boolean booleanConfig = config != null ? getBooleanProperty(propertyName, config) : null;
155 if (booleanConfig != null) {
156 return booleanConfig;
157 }
158 // Try system properties as fallback.
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 // Returns true when property exists or null otherwise. Property value is ignored.
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 // Returns true when property exists or false otherwise. Property value is ignored.
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 // Constructor helper: Copy provider specific properties Map. Only specified properties are added.
217 // Instance prettyPrinting and rejectDuplicateKeys variables must be initialized before
218 // this method is called.
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 // Some properties need special handling.
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 // Rest of properties are copied without changes
238 default:
239 if (config.containsKey(propertyName)) {
240 newConfig.put(propertyName, config.get(propertyName));
241 }
242 }
243 }
244 return newConfig;
245 }
246
247 }