-
Notifications
You must be signed in to change notification settings - Fork 0
/
Json5Light.java
131 lines (114 loc) · 4.23 KB
/
Json5Light.java
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
122
123
124
125
126
127
128
129
130
131
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 zone.cogni.asquare.cube.json5;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
/**
* <p>
* A Jackson parser which can read a subset of the JSON5 standard.
* </p>
* <p>
* JSON5 a more permissive, somewhat more readable superset of JSON.
* See <a href="https://www.json5.org">json5.org</a> for more details.
* </p>
* <p>
* JSON5 features
* <ul>
* <li>ecmascript identifier keys</li>
* <li>single trailing comma</li>
* <li>single quoted string</li>
* <li>multiline string using escapes</li>
* <li>allow character escapes</li>
* <li><strike>hexadecimal numbers</strike></li>
* <li><strike>leading or trailing decimal point</strike></li>
* <li>negative and positive infinity and NaN</li>
* <li><strike>explicit plus sign</strike></li>
* <li>single and multiline comments</li>
* <li><strike>additional whitespace</strike></li>
* </ul>
* </p>
*/
public class Json5Light {
/**
* <p>
* JSON5 features which are enabled via Jackson are:
* ecmascript identifiers, single trailing comma, single quoted strings,
* multiline strings, character escapes, non-numeric numbers and comments.
* </p>
* <p>
* In case of multiline strings you MUST use double quotes e.g. <code>"multiline string here"</code>
* </p>
*
* @return a Jackson {@link ObjectMapper ObjectMapper} with
* as many features as possible enabled.
*/
public static ObjectMapper getJson5Mapper() {
return new Json5Builder().allowEcmascriptIdentifier()
.allowSingleTrailingComma()
.allowSingleQuotedStrings()
.allowMultilineStrings()
.allowCharacterEscapes()
.allowNonNumericNumbers()
.allowComments()
.build();
}
public static class Json5Builder {
private final JsonMapper.Builder builder = JsonMapper.builder();
public Json5Builder allowEcmascriptIdentifier() {
builder.enable(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES);
return this;
}
public Json5Builder allowSingleTrailingComma() {
builder.enable(JsonReadFeature.ALLOW_TRAILING_COMMA);
return this;
}
public Json5Builder allowSingleQuotedStrings() {
builder.enable(JsonReadFeature.ALLOW_SINGLE_QUOTES);
return this;
}
/**
* Not sure if it 100% follow specifications, but if not it is an approximation.
* Builder returned only allows multiline strings in case they are using double-quotes e.g. <code>"example"</code>
*/
public Json5Builder allowMultilineStrings() {
builder.enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS)
.enable(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
return this;
}
/**
* Not sure if it 100% follow specifications, but if not it is an approximation.
*/
public Json5Builder allowCharacterEscapes() {
builder.enable(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER);
return this;
}
public Json5Builder allowNonNumericNumbers() {
builder.enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS);
return this;
}
public Json5Builder allowComments() {
builder.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS);
return this;
}
public ObjectMapper build() {
return builder.build();
}
}
}