Skip to content

Commit ffdbb0e

Browse files
committed
Add ValueType feature
Allow Parser.parse() to accept an EnumSet of ValueType so the caller may limit the allowed value types. This is done during parse because it can fail immediately once the type error is detected, rather than parsing the entire input and checking on return. Added related unit tests.
1 parent 5ff18b0 commit ffdbb0e

File tree

14 files changed

+701
-137
lines changed

14 files changed

+701
-137
lines changed

config/pmd-ruleset.xml

+8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
<rule ref="category/java/bestpractices.xml/JUnit4TestShouldUseBeforeAnnotation"/>
2929
<rule ref="category/java/bestpractices.xml/JUnit4TestShouldUseTestAnnotation"/>
3030
<rule ref="category/java/bestpractices.xml/JUnitAssertionsShouldIncludeMessage"/>
31+
32+
<!-- disable
3133
<rule ref="category/java/bestpractices.xml/JUnitTestContainsTooManyAsserts"/>
34+
-->
35+
3236
<rule ref="category/java/bestpractices.xml/JUnitTestsShouldIncludeAssert"/>
3337
<rule ref="category/java/bestpractices.xml/JUnitUseExpected"/>
3438
<rule ref="category/java/bestpractices.xml/LooseCoupling"/>
@@ -214,7 +218,11 @@
214218
<rule ref="category/java/errorprone.xml/AvoidMultipleUnaryOperators"/>
215219
<rule ref="category/java/errorprone.xml/AvoidUsingOctalValues"/>
216220
<rule ref="category/java/errorprone.xml/BadComparison"/>
221+
222+
<!-- way too many false positives
217223
<rule ref="category/java/errorprone.xml/BeanMembersShouldSerialize"/>
224+
-->
225+
218226
<rule ref="category/java/errorprone.xml/BrokenNullCheck"/>
219227
<rule ref="category/java/errorprone.xml/CallSuperFirst"/>
220228
<rule ref="category/java/errorprone.xml/CallSuperLast"/>

module/jsonurl-core/src/main/java/org/jsonurl/JavaValueFactory.java

+28-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.Collections;
22+
import java.util.EnumSet;
2223
import java.util.HashMap;
2324
import java.util.List;
2425
import java.util.Map;
@@ -82,16 +83,6 @@ public Number getNumber(NumberText text) {
8283
*/
8384
public static final Map<String,Object> EMPTY = Collections.emptyMap();
8485

85-
@Override
86-
default Class<?> getObjectClass() {
87-
return Map.class;
88-
}
89-
90-
@Override
91-
default Class<?> getArrayClass() {
92-
return List.class;
93-
}
94-
9586
@Override
9687
default Object getEmptyComposite() {
9788
return EMPTY;
@@ -140,12 +131,38 @@ default Boolean getFalse() {
140131
default String getString(CharSequence s, int start, int stop) {
141132
return toJavaString(s, start, stop);
142133
}
143-
134+
144135
@Override
145136
default String getString(String s) {
146137
return s;
147138
}
148139

140+
@Override
141+
default boolean isValid(EnumSet<ValueType> types, Object value) {
142+
if (isNull(value)) {
143+
return types.contains(ValueType.NULL);
144+
}
145+
if (isEmpty(value)) {
146+
return ValueType.containsComposite(types);
147+
}
148+
if (value instanceof String) {
149+
return types.contains(ValueType.STRING);
150+
}
151+
if (value instanceof Number) {
152+
return types.contains(ValueType.NUMBER);
153+
}
154+
if (value instanceof Boolean) {
155+
return types.contains(ValueType.BOOLEAN);
156+
}
157+
if (value instanceof List) {
158+
return types.contains(ValueType.ARRAY);
159+
}
160+
if (value instanceof Map) {
161+
return types.contains(ValueType.OBJECT);
162+
}
163+
return false;
164+
}
165+
149166
/**
150167
* Get a java.lang.String from a java.lang.CharSequence
151168
* @param cs input

module/jsonurl-core/src/main/java/org/jsonurl/JsonUrl.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ private static final int percentDecode(
5656
int len) {
5757

5858
if (off + 2 > len) {
59-
return 0;
59+
// return 0;
60+
throw new SyntaxException(ERR_MSG_BADPCTENC, off);
6061
}
6162

6263
int c1 = hexDecode(s.charAt(off));

module/jsonurl-core/src/main/java/org/jsonurl/JsonUrlTextAppender.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public abstract class JsonUrlTextAppender<A extends Appendable, R>
3636
/**
3737
* Destination, provided in constructor.
3838
*/
39-
protected final A out; // NOPMD - not a bean
39+
protected final A out;
4040

4141
/**
4242
* Create a new JsonUrlTextAppender.

0 commit comments

Comments
 (0)