1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
* Copyright (C) 2009-2011 Mathias Doenitz
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.spray.json
import org.parboiled.scala._
import org.parboiled.errors.{ErrorUtils, ParsingException}
import java.lang.StringBuilder
import org.parboiled.Context
/**
* This JSON parser is the almost direct implementation of the JSON grammar
* presented at http://www.json.org as a parboiled PEG parser.
*/
object JsonParser extends Parser {
// the root rule
def Json = rule { WhiteSpace ~ Value ~ EOI }
def JsonObject: Rule1[JsObject] = rule {
"{ " ~ zeroOrMore(Pair, separator = ", ") ~ "} " ~~> (JsObject(_))
}
def Pair = rule { JsonStringUnwrapped ~ ": " ~ Value ~~> (JsField(_, _)) }
def Value: Rule1[JsValue] = rule {
JsonString | JsonNumber | JsonObject | JsonArray | JsonTrue | JsonFalse | JsonNull
}
def JsonString = rule { JsonStringUnwrapped ~~> JsString }
def JsonStringUnwrapped = rule { "\"" ~ Characters ~ "\" " ~~> (_.toString) }
def JsonNumber = rule { group(Integer ~ optional(Frac) ~ optional(Exp)) ~> (JsNumber(_)) ~ WhiteSpace }
def JsonArray = rule { "[ " ~ zeroOrMore(Value, separator = ", ") ~ "] " ~~> (JsArray(_)) }
def Characters = rule { push(new StringBuilder) ~ zeroOrMore("\\" ~ EscapedChar | NormalChar) }
def EscapedChar = rule (
anyOf("\"\\/") ~:% withContext(appendToSb(_)(_))
| "b" ~ appendToSb('\b')
| "f" ~ appendToSb('\f')
| "n" ~ appendToSb('\n')
| "r" ~ appendToSb('\r')
| "t" ~ appendToSb('\t')
| Unicode ~~% withContext((code, ctx) => appendToSb(code.asInstanceOf[Char])(ctx))
)
def NormalChar = rule { !anyOf("\"\\") ~ ANY ~:% (withContext(appendToSb(_)(_))) }
def Unicode = rule { "u" ~ group(HexDigit ~ HexDigit ~ HexDigit ~ HexDigit) ~> (java.lang.Integer.parseInt(_, 16)) }
def Integer = rule { optional("-") ~ (("1" - "9") ~ Digits | Digit) }
def Digits = rule { oneOrMore(Digit) }
def Digit = rule { "0" - "9" }
def HexDigit = rule { "0" - "9" | "a" - "f" | "A" - "Z" }
def Frac = rule { "." ~ Digits }
def Exp = rule { ignoreCase("e") ~ optional(anyOf("+-")) ~ Digits }
def JsonTrue = rule { "true " ~ push(JsTrue) }
def JsonFalse = rule { "false " ~ push(JsFalse) }
def JsonNull = rule { "null " ~ push(JsNull) }
def WhiteSpace: Rule0 = rule { zeroOrMore(anyOf(" \n\r\t\f")) }
// helper method for fast string building
// for maximum performance we use a somewhat unorthodox parsing technqiue that is a bit more verbose (and probably
// less readable) but reduces object allocations during the parsing run to a minimum:
// the Characters rules pushes a StringBuilder object onto the stack which is then directly fed with matched
// and unescaped characters in the sub rules (i.e. no string allocations and value stack operation required)
def appendToSb(c: Char): Context[Any] => Unit = { ctx =>
ctx.getValueStack.peek.asInstanceOf[StringBuilder].append(c)
()
}
/**
* We redefine the default string-to-rule conversion to also match trailing whitespace if the string ends with
* a blank, this keeps the rules free from most whitespace matching clutter
*/
override implicit def toRule(string: String) = {
if (string.endsWith(" ")) str(string.trim) ~ WhiteSpace
else str(string)
}
/**
* The main parsing method. Uses a ReportingParseRunner (which only reports the first error) for simplicity.
*/
def apply(json: String): JsValue = apply(json.toCharArray)
/**
* The main parsing method. Uses a ReportingParseRunner (which only reports the first error) for simplicity.
*/
def apply(json: Array[Char]): JsValue = {
val parsingResult = ReportingParseRunner(Json).run(json)
parsingResult.result.getOrElse {
throw new ParsingException("Invalid JSON source:\n" + ErrorUtils.printParseErrors(parsingResult))
}
}
}
|