Skip to content

Commit 078a613

Browse files
authored
Merge pull request #30 from JavaWebStack/feature/json-schema-support
Feature: json schema support
2 parents 59aac8e + db7d4d3 commit 078a613

34 files changed

+653
-244
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<dependency>
5050
<groupId>org.mongodb</groupId>
5151
<artifactId>bson</artifactId>
52-
<version>4.10.2</version>
52+
<version>4.11.1</version>
5353
<optional>true</optional>
5454
</dependency>
5555
<dependency>

src/main/java/org/javawebstack/abstractdata/AbstractArray.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public AbstractArray array(boolean strict) throws AbstractCoercingException {
2222
}
2323

2424
public AbstractObject object(boolean strict) throws AbstractCoercingException {
25-
if(strict)
25+
if (strict)
2626
throw new AbstractCoercingException(Type.OBJECT, Type.ARRAY);
2727
AbstractObject object = new AbstractObject();
28-
for(int i=0; i< elements.size(); i++)
28+
for (int i = 0; i < elements.size(); i++)
2929
object.set(String.valueOf(i), elements.get(i));
3030
return object;
3131
}
@@ -227,11 +227,11 @@ public AbstractElement query(String query) {
227227
try {
228228
int index = Integer.parseInt(q[0]);
229229
AbstractElement e = get(index);
230-
if(e == null || q.length == 1)
230+
if (e == null || q.length == 1)
231231
return e;
232-
if(e.isObject())
232+
if (e.isObject())
233233
return e.object().query(q[1]);
234-
if(e.isArray())
234+
if (e.isArray())
235235
return e.array().query(q[1]);
236236
return null;
237237
} catch (NumberFormatException nfe) {
@@ -281,7 +281,7 @@ public List<String> toStringList() {
281281

282282
public List<String> toStringList(boolean strict) {
283283
List<String> list = new ArrayList<>();
284-
for(AbstractElement e : elements)
284+
for (AbstractElement e : elements)
285285
list.add(e.string(strict));
286286
return list;
287287
}
@@ -292,7 +292,7 @@ public List<AbstractObject> toObjectList() {
292292

293293
public List<AbstractObject> toObjectList(boolean strict) {
294294
List<AbstractObject> list = new ArrayList<>();
295-
for(AbstractElement e : elements)
295+
for (AbstractElement e : elements)
296296
list.add(e.object(strict));
297297
return list;
298298
}
@@ -365,7 +365,7 @@ public boolean equals(Object obj, boolean strict) {
365365
}
366366
}
367367

368-
public boolean equals (Object obj) {
368+
public boolean equals(Object obj) {
369369
return equals(obj, false);
370370
}
371371

src/main/java/org/javawebstack/abstractdata/AbstractNull.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public boolean equals(Object obj, boolean strict) {
6060
return obj == null || obj instanceof AbstractNull;
6161
}
6262

63-
public boolean equals (Object obj) {
63+
public boolean equals(Object obj) {
6464
return equals(obj, false);
6565
}
6666
}

src/main/java/org/javawebstack/abstractdata/AbstractObject.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public AbstractObject object(boolean strict) throws AbstractCoercingException {
7171
}
7272

7373
public AbstractArray array(boolean strict) throws AbstractCoercingException {
74-
if(strict)
74+
if (strict)
7575
throw new AbstractCoercingException(Type.ARRAY, Type.OBJECT);
7676
AbstractArray array = new AbstractArray();
7777
for (int i = 0; i < size(); i++) {
@@ -94,17 +94,17 @@ public AbstractElement get(String key, AbstractElement orElse) {
9494
public AbstractElement query(String query) {
9595
String[] q = query.split("\\.", 2);
9696
AbstractElement e = get(q[0]);
97-
if(e == null || q.length == 1)
97+
if (e == null || q.length == 1)
9898
return e;
99-
if(e.isObject())
99+
if (e.isObject())
100100
return e.object().query(q[1]);
101-
if(e.isArray())
101+
if (e.isArray())
102102
return e.array().query(q[1]);
103103
return null;
104104
}
105105

106106
public AbstractElement query(String query, AbstractElement orElse) {
107-
if(orElse == null)
107+
if (orElse == null)
108108
orElse = AbstractNull.VALUE;
109109
AbstractElement value = query(query);
110110
return (value != null && !value.isNull()) ? value : orElse;
@@ -288,7 +288,7 @@ public boolean equals(Object obj, boolean strict) {
288288
}
289289
}
290290

291-
public boolean equals (Object obj) {
291+
public boolean equals(Object obj) {
292292
return equals(obj, false);
293293
}
294294
}

src/main/java/org/javawebstack/abstractdata/AbstractPath.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AbstractPath {
1313

1414
public AbstractPath(String name) {
1515
this(ROOT, name);
16-
if(name == null || name.isEmpty())
16+
if (name == null || name.isEmpty())
1717
throw new IllegalArgumentException("Name can not be null or empty");
1818
}
1919

@@ -43,14 +43,14 @@ public AbstractPath clone() {
4343

4444
public AbstractPath concat(AbstractPath path) {
4545
AbstractPath cloned = clone();
46-
for(String part : path.getParts())
46+
for (String part : path.getParts())
4747
cloned = cloned.subPath(part);
4848
return cloned;
4949
}
5050

5151
public List<String> getParts() {
5252
List<String> parts = parent != null ? parent.getParts() : new ArrayList<>();
53-
if(name != null)
53+
if (name != null)
5454
parts.add(name);
5555
return parts;
5656
}
@@ -61,31 +61,31 @@ public String toString() {
6161

6262
public static AbstractPath parse(String s) {
6363
s = s.trim();
64-
if(s.isEmpty())
64+
if (s.isEmpty())
6565
return ROOT;
6666
String[] spl = s.split("\\.");
6767
AbstractPath path = new AbstractPath(spl[0]);
68-
for(int i=1; i<spl.length; i++) {
68+
for (int i = 1; i < spl.length; i++) {
6969
String sub = spl[i];
70-
if(sub.isEmpty())
70+
if (sub.isEmpty())
7171
throw new IllegalArgumentException("Invalid empty sub-path");
7272
path = path.subPath(spl[i]);
7373
}
7474
return path;
7575
}
7676

7777
public boolean equals(Object obj) {
78-
if(!(obj instanceof AbstractPath))
78+
if (!(obj instanceof AbstractPath))
7979
return false;
8080
AbstractPath other = (AbstractPath) obj;
81-
if(parent != null) {
82-
if(!parent.equals(other.parent))
81+
if (parent != null) {
82+
if (!parent.equals(other.parent))
8383
return false;
8484
} else {
85-
if(other.parent != null)
85+
if (other.parent != null)
8686
return false;
8787
}
88-
if(name == null)
88+
if (name == null)
8989
return other.name == null;
9090
return name.equals(other.name);
9191
}

src/main/java/org/javawebstack/abstractdata/AbstractPrimitive.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ public class AbstractPrimitive implements AbstractElement {
1111
private final Object value;
1212

1313
public AbstractPrimitive(Number value) {
14-
if(value == null)
14+
if (value == null)
1515
throw new NullPointerException("AbstractPrimitive value can not be null");
1616
this.value = value;
1717
}
1818

1919
public AbstractPrimitive(Boolean value) {
20-
if(value == null)
20+
if (value == null)
2121
throw new NullPointerException("AbstractPrimitive value can not be null");
2222
this.value = value;
2323
}
2424

2525
public AbstractPrimitive(String value) {
26-
if(value == null)
26+
if (value == null)
2727
throw new NullPointerException("AbstractPrimitive value can not be null");
2828
this.value = value;
2929
}
@@ -53,8 +53,8 @@ public String string() throws AbstractCoercingException {
5353
}
5454

5555
public String string(boolean strict) throws AbstractCoercingException {
56-
if(!(value instanceof String)) {
57-
if(strict)
56+
if (!(value instanceof String)) {
57+
if (strict)
5858
throw new AbstractCoercingException(Type.STRING, getType());
5959
switch (getType()) {
6060
case BOOLEAN:
@@ -73,8 +73,8 @@ public Number number() throws AbstractCoercingException {
7373
}
7474

7575
public Number number(boolean strict) throws AbstractCoercingException {
76-
if(!(value instanceof Number)) {
77-
if(strict)
76+
if (!(value instanceof Number)) {
77+
if (strict)
7878
throw new AbstractCoercingException(Type.NUMBER, getType());
7979
switch (getType()) {
8080
case BOOLEAN:
@@ -98,8 +98,8 @@ public Boolean bool() throws AbstractCoercingException {
9898
}
9999

100100
public Boolean bool(boolean strict) throws AbstractCoercingException {
101-
if(!(value instanceof Boolean)) {
102-
if(strict)
101+
if (!(value instanceof Boolean)) {
102+
if (strict)
103103
throw new AbstractCoercingException(Type.BOOLEAN, getType());
104104
switch (getType()) {
105105
case STRING: {
@@ -120,9 +120,9 @@ public Boolean bool(boolean strict) throws AbstractCoercingException {
120120
}
121121
case NUMBER: {
122122
long l = ((Number) value).longValue();
123-
if(l == 0)
123+
if (l == 0)
124124
return false;
125-
if(l == 1)
125+
if (l == 1)
126126
return true;
127127
throw new AbstractCoercingException(Type.BOOLEAN, this);
128128
}
@@ -182,17 +182,18 @@ public boolean equals(Object obj, boolean strict) {
182182
return false;
183183
AbstractPrimitive primitive = (AbstractPrimitive) obj;
184184
try {
185-
if(isBoolean())
185+
if (isBoolean())
186186
return bool() == primitive.bool(strict);
187-
if(isNumber())
187+
if (isNumber())
188188
return number().equals(primitive.number(strict));
189-
if(isString())
189+
if (isString())
190190
return string().equals(primitive.string(strict));
191-
} catch (AbstractCoercingException ignored) {}
191+
} catch (AbstractCoercingException ignored) {
192+
}
192193
return false;
193194
}
194195

195-
public boolean equals (Object obj) {
196+
public boolean equals(Object obj) {
196197
return equals(obj, false);
197198
}
198199
}

src/main/java/org/javawebstack/abstractdata/NamingPolicy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.javawebstack.abstractdata;
22

33
public enum NamingPolicy {
4-
4+
55
NONE(org.javawebstack.abstractdata.mapper.naming.NamingPolicy.NONE),
66
CAMEL_CASE(org.javawebstack.abstractdata.mapper.naming.NamingPolicy.CAMEL_CASE),
77
PASCAL_CASE(org.javawebstack.abstractdata.mapper.naming.NamingPolicy.PASCAL_CASE),

src/main/java/org/javawebstack/abstractdata/bson/BsonConverter.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,72 +97,73 @@ public AbstractElement toAbstract(BsonValue value) {
9797
}
9898

9999
public BsonValue toBson(AbstractElement element) {
100-
if(element == null || element.isNull())
100+
if (element == null || element.isNull())
101101
return BsonNull.VALUE;
102-
if(element.isString())
102+
if (element.isString())
103103
return new BsonString(element.string());
104-
if(element.isBoolean())
104+
if (element.isBoolean())
105105
return new BsonBoolean(element.bool());
106-
if(element.isNumber()) {
106+
if (element.isNumber()) {
107107
Number n = element.number();
108-
if(n instanceof Integer || n instanceof Short || n instanceof Byte) {
108+
if (n instanceof Integer || n instanceof Short || n instanceof Byte) {
109109
return new BsonInt32(n.intValue());
110-
} else if(n instanceof Long) {
110+
} else if (n instanceof Long) {
111111
return new BsonInt64(n.longValue());
112-
} else if(n instanceof Float || n instanceof Double) {
112+
} else if (n instanceof Float || n instanceof Double) {
113113
return new BsonDouble(n.doubleValue());
114114
}
115115
}
116-
if(element.isArray()) {
116+
if (element.isArray()) {
117117
BsonArray a = new BsonArray();
118-
for(AbstractElement e : element.array())
118+
for (AbstractElement e : element.array())
119119
a.add(toBson(e));
120120
return a;
121121
}
122122
AbstractObject o = element.object();
123-
if(o.size() == 1 && o.has("$oid") && o.get("$oid").isString())
123+
if (o.size() == 1 && o.has("$oid") && o.get("$oid").isString())
124124
return new BsonObjectId(new ObjectId(o.string("$oid")));
125-
if(o.size() == 1 && o.has("$undefined") && o.get("$undefined").isBoolean())
125+
if (o.size() == 1 && o.has("$undefined") && o.get("$undefined").isBoolean())
126126
return new BsonUndefined();
127-
if(o.size() == 1 && o.has("$date") && o.get("$date").isString()) {
127+
if (o.size() == 1 && o.has("$date") && o.get("$date").isString()) {
128128
try {
129129
return new BsonDateTime(dateFormat.parse(o.string("$date")).getTime());
130-
} catch (ParseException ignored) {}
130+
} catch (ParseException ignored) {
131+
}
131132
}
132-
if(o.size() == 1 && o.has("$numberDecimal") && o.get("$numberDecimal").isString())
133+
if (o.size() == 1 && o.has("$numberDecimal") && o.get("$numberDecimal").isString())
133134
return new BsonDecimal128(Decimal128.parse(o.string("$numberDecimal")));
134-
if(o.size() == 1 && o.has("$minKey") && o.get("$minKey").isNumber())
135+
if (o.size() == 1 && o.has("$minKey") && o.get("$minKey").isNumber())
135136
return new BsonMinKey();
136-
if(o.size() == 1 && o.has("$maxKey") && o.get("$maxKey").isNumber())
137+
if (o.size() == 1 && o.has("$maxKey") && o.get("$maxKey").isNumber())
137138
return new BsonMinKey();
138-
if(o.size() == 1 && o.has("$symbol") && o.get("$symbol").isString())
139+
if (o.size() == 1 && o.has("$symbol") && o.get("$symbol").isString())
139140
return new BsonSymbol(o.string("$symbol"));
140-
if(o.size() == 1 && o.has("$code") && o.get("$code").isString())
141+
if (o.size() == 1 && o.has("$code") && o.get("$code").isString())
141142
return new BsonJavaScript(o.string("$code"));
142-
if(o.size() == 2 && o.has("$code") && o.has("$scope") && o.get("$code").isString() && o.get("$scope").isObject())
143+
if (o.size() == 2 && o.has("$code") && o.has("$scope") && o.get("$code").isString() && o.get("$scope").isObject())
143144
return new BsonJavaScriptWithScope(o.string("$code"), toBson(o.get("$scope")).asDocument());
144-
if(o.size() == 1 && o.has("$timestamp") && o.get("$timestamp").isObject()) {
145+
if (o.size() == 1 && o.has("$timestamp") && o.get("$timestamp").isObject()) {
145146
AbstractObject ts = o.object("$timestamp");
146-
if(ts.has("t") && ts.has("i") && ts.get("t").isNumber() && ts.get("i").isNumber()) {
147+
if (ts.has("t") && ts.has("i") && ts.get("t").isNumber() && ts.get("i").isNumber()) {
147148
return new BsonTimestamp((int) ts.number("t").longValue(), (int) ts.number("i").longValue());
148149
}
149150
}
150-
if(o.size() == 1 && o.has("$regularExpression") && o.get("$regularExpression").isObject()) {
151+
if (o.size() == 1 && o.has("$regularExpression") && o.get("$regularExpression").isObject()) {
151152
AbstractObject re = o.object("$regularExpression");
152-
if(re.has("pattern") && re.has("options") && re.get("pattern").isString() && (re.get("options").isString() || re.get("options").isNull())) {
153+
if (re.has("pattern") && re.has("options") && re.get("pattern").isString() && (re.get("options").isString() || re.get("options").isNull())) {
153154
return new BsonRegularExpression(re.string("pattern"), (String) re.toObject());
154155
}
155156
}
156-
if(o.size() == 1 && o.has("$binary") && o.get("$binary").isObject()) {
157+
if (o.size() == 1 && o.has("$binary") && o.get("$binary").isObject()) {
157158
AbstractObject bin = o.object("$binary");
158-
if(bin.has("base64") && bin.has("subType") && bin.get("base64").isString() && bin.get("subType").isString()) {
159+
if (bin.has("base64") && bin.has("subType") && bin.get("base64").isString() && bin.get("subType").isString()) {
159160
byte[] data = Base64.getDecoder().decode(bin.string("base64"));
160161
byte type = (byte) Integer.parseInt(bin.string("subType"), 16);
161162
return new BsonBinary(type, data);
162163
}
163164
}
164165
BsonDocument doc = new BsonDocument(o.size());
165-
for(String k : o.keys())
166+
for (String k : o.keys())
166167
doc.put(k, toBson(o.get(k)));
167168
return doc;
168169
}

0 commit comments

Comments
 (0)