summaryrefslogtreecommitdiff
path: root/shared/src/main/scala/spray/json/JsonParserSettings.scala
diff options
context:
space:
mode:
authorJakob Odersky <jakob@odersky.com>2018-03-07 14:07:25 -0800
committerJakob Odersky <jakob@odersky.com>2019-06-10 23:22:15 +0200
commita43a10a12fd5653e6050c652024764416b71ab54 (patch)
tree19824c4e4eb31f849d51520d57a484e4f4ca0640 /shared/src/main/scala/spray/json/JsonParserSettings.scala
parentda8ed26cfed5e958eeb29a3444ff882a46090459 (diff)
downloadspray-json-a43a10a12fd5653e6050c652024764416b71ab54.tar.gz
spray-json-a43a10a12fd5653e6050c652024764416b71ab54.tar.bz2
spray-json-a43a10a12fd5653e6050c652024764416b71ab54.zip
Add support for ScalaJS and Scala Native
Binary compatibility with previous versions is maintained.
Diffstat (limited to 'shared/src/main/scala/spray/json/JsonParserSettings.scala')
-rw-r--r--shared/src/main/scala/spray/json/JsonParserSettings.scala56
1 files changed, 56 insertions, 0 deletions
diff --git a/shared/src/main/scala/spray/json/JsonParserSettings.scala b/shared/src/main/scala/spray/json/JsonParserSettings.scala
new file mode 100644
index 0000000..8c76b0a
--- /dev/null
+++ b/shared/src/main/scala/spray/json/JsonParserSettings.scala
@@ -0,0 +1,56 @@
+package spray.json
+
+/**
+ * Allows to customize settings for the JSON parser.
+ *
+ * Use it like this:
+ *
+ * ```
+ * val customSettings =
+ * JsonParserSettings.default
+ * .withMaxDepth(100)
+ * .withMaxNumberCharacters(20)
+ *
+ * JsonParser(jsonString, customSettings)
+ * // or
+ * jsonString.parseJson(customSettings)
+ * ```
+ */
+sealed trait JsonParserSettings {
+ /**
+ * The JsonParser uses recursive decent parsing that keeps intermediate values on the stack. To prevent
+ * StackOverflowExceptions a limit is enforced on the depth of the parsed JSON structure.
+ *
+ * As a guideline we tested that one level of depth needs about 300 bytes of stack space.
+ *
+ * The default is a depth of 1000.
+ */
+ def maxDepth: Int
+
+ /**
+ * Returns a copy of this settings object with the `maxDepth` setting changed to the new value.
+ */
+ def withMaxDepth(newValue: Int): JsonParserSettings
+
+ /**
+ * The maximum number of characters the parser should support for numbers. This is restricted because creating
+ * `BigDecimal`s with high precision can be very slow (approx. quadratic runtime per amount of characters).
+ */
+ def maxNumberCharacters: Int
+
+ /**
+ * Returns a copy of this settings object with the `maxNumberCharacters` setting changed to the new value.
+ */
+ def withMaxNumberCharacters(newValue: Int): JsonParserSettings
+}
+object JsonParserSettings {
+ val default: JsonParserSettings = SettingsImpl()
+
+ private case class SettingsImpl(
+ maxDepth: Int = 1000,
+ maxNumberCharacters: Int = 100
+ ) extends JsonParserSettings {
+ override def withMaxDepth(newValue: Int): JsonParserSettings = copy(maxDepth = newValue)
+ override def withMaxNumberCharacters(newValue: Int): JsonParserSettings = copy(maxNumberCharacters = newValue)
+ }
+} \ No newline at end of file