Skip to content

Commit

Permalink
Merge pull request #30 from JavaWebStack/feature/json-schema-support
Browse files Browse the repository at this point in the history
Feature: json schema support
  • Loading branch information
JanHolger authored Dec 19, 2023
2 parents 59aac8e + db7d4d3 commit 078a613
Show file tree
Hide file tree
Showing 34 changed files with 653 additions and 244 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>4.10.2</version>
<version>4.11.1</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/javawebstack/abstractdata/AbstractArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public AbstractArray array(boolean strict) throws AbstractCoercingException {
}

public AbstractObject object(boolean strict) throws AbstractCoercingException {
if(strict)
if (strict)
throw new AbstractCoercingException(Type.OBJECT, Type.ARRAY);
AbstractObject object = new AbstractObject();
for(int i=0; i< elements.size(); i++)
for (int i = 0; i < elements.size(); i++)
object.set(String.valueOf(i), elements.get(i));
return object;
}
Expand Down Expand Up @@ -227,11 +227,11 @@ public AbstractElement query(String query) {
try {
int index = Integer.parseInt(q[0]);
AbstractElement e = get(index);
if(e == null || q.length == 1)
if (e == null || q.length == 1)
return e;
if(e.isObject())
if (e.isObject())
return e.object().query(q[1]);
if(e.isArray())
if (e.isArray())
return e.array().query(q[1]);
return null;
} catch (NumberFormatException nfe) {
Expand Down Expand Up @@ -281,7 +281,7 @@ public List<String> toStringList() {

public List<String> toStringList(boolean strict) {
List<String> list = new ArrayList<>();
for(AbstractElement e : elements)
for (AbstractElement e : elements)
list.add(e.string(strict));
return list;
}
Expand All @@ -292,7 +292,7 @@ public List<AbstractObject> toObjectList() {

public List<AbstractObject> toObjectList(boolean strict) {
List<AbstractObject> list = new ArrayList<>();
for(AbstractElement e : elements)
for (AbstractElement e : elements)
list.add(e.object(strict));
return list;
}
Expand Down Expand Up @@ -365,7 +365,7 @@ public boolean equals(Object obj, boolean strict) {
}
}

public boolean equals (Object obj) {
public boolean equals(Object obj) {
return equals(obj, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean equals(Object obj, boolean strict) {
return obj == null || obj instanceof AbstractNull;
}

public boolean equals (Object obj) {
public boolean equals(Object obj) {
return equals(obj, false);
}
}
12 changes: 6 additions & 6 deletions src/main/java/org/javawebstack/abstractdata/AbstractObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public AbstractObject object(boolean strict) throws AbstractCoercingException {
}

public AbstractArray array(boolean strict) throws AbstractCoercingException {
if(strict)
if (strict)
throw new AbstractCoercingException(Type.ARRAY, Type.OBJECT);
AbstractArray array = new AbstractArray();
for (int i = 0; i < size(); i++) {
Expand All @@ -94,17 +94,17 @@ public AbstractElement get(String key, AbstractElement orElse) {
public AbstractElement query(String query) {
String[] q = query.split("\\.", 2);
AbstractElement e = get(q[0]);
if(e == null || q.length == 1)
if (e == null || q.length == 1)
return e;
if(e.isObject())
if (e.isObject())
return e.object().query(q[1]);
if(e.isArray())
if (e.isArray())
return e.array().query(q[1]);
return null;
}

public AbstractElement query(String query, AbstractElement orElse) {
if(orElse == null)
if (orElse == null)
orElse = AbstractNull.VALUE;
AbstractElement value = query(query);
return (value != null && !value.isNull()) ? value : orElse;
Expand Down Expand Up @@ -288,7 +288,7 @@ public boolean equals(Object obj, boolean strict) {
}
}

public boolean equals (Object obj) {
public boolean equals(Object obj) {
return equals(obj, false);
}
}
22 changes: 11 additions & 11 deletions src/main/java/org/javawebstack/abstractdata/AbstractPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AbstractPath {

public AbstractPath(String name) {
this(ROOT, name);
if(name == null || name.isEmpty())
if (name == null || name.isEmpty())
throw new IllegalArgumentException("Name can not be null or empty");
}

Expand Down Expand Up @@ -43,14 +43,14 @@ public AbstractPath clone() {

public AbstractPath concat(AbstractPath path) {
AbstractPath cloned = clone();
for(String part : path.getParts())
for (String part : path.getParts())
cloned = cloned.subPath(part);
return cloned;
}

public List<String> getParts() {
List<String> parts = parent != null ? parent.getParts() : new ArrayList<>();
if(name != null)
if (name != null)
parts.add(name);
return parts;
}
Expand All @@ -61,31 +61,31 @@ public String toString() {

public static AbstractPath parse(String s) {
s = s.trim();
if(s.isEmpty())
if (s.isEmpty())
return ROOT;
String[] spl = s.split("\\.");
AbstractPath path = new AbstractPath(spl[0]);
for(int i=1; i<spl.length; i++) {
for (int i = 1; i < spl.length; i++) {
String sub = spl[i];
if(sub.isEmpty())
if (sub.isEmpty())
throw new IllegalArgumentException("Invalid empty sub-path");
path = path.subPath(spl[i]);
}
return path;
}

public boolean equals(Object obj) {
if(!(obj instanceof AbstractPath))
if (!(obj instanceof AbstractPath))
return false;
AbstractPath other = (AbstractPath) obj;
if(parent != null) {
if(!parent.equals(other.parent))
if (parent != null) {
if (!parent.equals(other.parent))
return false;
} else {
if(other.parent != null)
if (other.parent != null)
return false;
}
if(name == null)
if (name == null)
return other.name == null;
return name.equals(other.name);
}
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/org/javawebstack/abstractdata/AbstractPrimitive.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ public class AbstractPrimitive implements AbstractElement {
private final Object value;

public AbstractPrimitive(Number value) {
if(value == null)
if (value == null)
throw new NullPointerException("AbstractPrimitive value can not be null");
this.value = value;
}

public AbstractPrimitive(Boolean value) {
if(value == null)
if (value == null)
throw new NullPointerException("AbstractPrimitive value can not be null");
this.value = value;
}

public AbstractPrimitive(String value) {
if(value == null)
if (value == null)
throw new NullPointerException("AbstractPrimitive value can not be null");
this.value = value;
}
Expand Down Expand Up @@ -53,8 +53,8 @@ public String string() throws AbstractCoercingException {
}

public String string(boolean strict) throws AbstractCoercingException {
if(!(value instanceof String)) {
if(strict)
if (!(value instanceof String)) {
if (strict)
throw new AbstractCoercingException(Type.STRING, getType());
switch (getType()) {
case BOOLEAN:
Expand All @@ -73,8 +73,8 @@ public Number number() throws AbstractCoercingException {
}

public Number number(boolean strict) throws AbstractCoercingException {
if(!(value instanceof Number)) {
if(strict)
if (!(value instanceof Number)) {
if (strict)
throw new AbstractCoercingException(Type.NUMBER, getType());
switch (getType()) {
case BOOLEAN:
Expand All @@ -98,8 +98,8 @@ public Boolean bool() throws AbstractCoercingException {
}

public Boolean bool(boolean strict) throws AbstractCoercingException {
if(!(value instanceof Boolean)) {
if(strict)
if (!(value instanceof Boolean)) {
if (strict)
throw new AbstractCoercingException(Type.BOOLEAN, getType());
switch (getType()) {
case STRING: {
Expand All @@ -120,9 +120,9 @@ public Boolean bool(boolean strict) throws AbstractCoercingException {
}
case NUMBER: {
long l = ((Number) value).longValue();
if(l == 0)
if (l == 0)
return false;
if(l == 1)
if (l == 1)
return true;
throw new AbstractCoercingException(Type.BOOLEAN, this);
}
Expand Down Expand Up @@ -182,17 +182,18 @@ public boolean equals(Object obj, boolean strict) {
return false;
AbstractPrimitive primitive = (AbstractPrimitive) obj;
try {
if(isBoolean())
if (isBoolean())
return bool() == primitive.bool(strict);
if(isNumber())
if (isNumber())
return number().equals(primitive.number(strict));
if(isString())
if (isString())
return string().equals(primitive.string(strict));
} catch (AbstractCoercingException ignored) {}
} catch (AbstractCoercingException ignored) {
}
return false;
}

public boolean equals (Object obj) {
public boolean equals(Object obj) {
return equals(obj, false);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.javawebstack.abstractdata;

public enum NamingPolicy {

NONE(org.javawebstack.abstractdata.mapper.naming.NamingPolicy.NONE),
CAMEL_CASE(org.javawebstack.abstractdata.mapper.naming.NamingPolicy.CAMEL_CASE),
PASCAL_CASE(org.javawebstack.abstractdata.mapper.naming.NamingPolicy.PASCAL_CASE),
Expand Down
53 changes: 27 additions & 26 deletions src/main/java/org/javawebstack/abstractdata/bson/BsonConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,72 +97,73 @@ public AbstractElement toAbstract(BsonValue value) {
}

public BsonValue toBson(AbstractElement element) {
if(element == null || element.isNull())
if (element == null || element.isNull())
return BsonNull.VALUE;
if(element.isString())
if (element.isString())
return new BsonString(element.string());
if(element.isBoolean())
if (element.isBoolean())
return new BsonBoolean(element.bool());
if(element.isNumber()) {
if (element.isNumber()) {
Number n = element.number();
if(n instanceof Integer || n instanceof Short || n instanceof Byte) {
if (n instanceof Integer || n instanceof Short || n instanceof Byte) {
return new BsonInt32(n.intValue());
} else if(n instanceof Long) {
} else if (n instanceof Long) {
return new BsonInt64(n.longValue());
} else if(n instanceof Float || n instanceof Double) {
} else if (n instanceof Float || n instanceof Double) {
return new BsonDouble(n.doubleValue());
}
}
if(element.isArray()) {
if (element.isArray()) {
BsonArray a = new BsonArray();
for(AbstractElement e : element.array())
for (AbstractElement e : element.array())
a.add(toBson(e));
return a;
}
AbstractObject o = element.object();
if(o.size() == 1 && o.has("$oid") && o.get("$oid").isString())
if (o.size() == 1 && o.has("$oid") && o.get("$oid").isString())
return new BsonObjectId(new ObjectId(o.string("$oid")));
if(o.size() == 1 && o.has("$undefined") && o.get("$undefined").isBoolean())
if (o.size() == 1 && o.has("$undefined") && o.get("$undefined").isBoolean())
return new BsonUndefined();
if(o.size() == 1 && o.has("$date") && o.get("$date").isString()) {
if (o.size() == 1 && o.has("$date") && o.get("$date").isString()) {
try {
return new BsonDateTime(dateFormat.parse(o.string("$date")).getTime());
} catch (ParseException ignored) {}
} catch (ParseException ignored) {
}
}
if(o.size() == 1 && o.has("$numberDecimal") && o.get("$numberDecimal").isString())
if (o.size() == 1 && o.has("$numberDecimal") && o.get("$numberDecimal").isString())
return new BsonDecimal128(Decimal128.parse(o.string("$numberDecimal")));
if(o.size() == 1 && o.has("$minKey") && o.get("$minKey").isNumber())
if (o.size() == 1 && o.has("$minKey") && o.get("$minKey").isNumber())
return new BsonMinKey();
if(o.size() == 1 && o.has("$maxKey") && o.get("$maxKey").isNumber())
if (o.size() == 1 && o.has("$maxKey") && o.get("$maxKey").isNumber())
return new BsonMinKey();
if(o.size() == 1 && o.has("$symbol") && o.get("$symbol").isString())
if (o.size() == 1 && o.has("$symbol") && o.get("$symbol").isString())
return new BsonSymbol(o.string("$symbol"));
if(o.size() == 1 && o.has("$code") && o.get("$code").isString())
if (o.size() == 1 && o.has("$code") && o.get("$code").isString())
return new BsonJavaScript(o.string("$code"));
if(o.size() == 2 && o.has("$code") && o.has("$scope") && o.get("$code").isString() && o.get("$scope").isObject())
if (o.size() == 2 && o.has("$code") && o.has("$scope") && o.get("$code").isString() && o.get("$scope").isObject())
return new BsonJavaScriptWithScope(o.string("$code"), toBson(o.get("$scope")).asDocument());
if(o.size() == 1 && o.has("$timestamp") && o.get("$timestamp").isObject()) {
if (o.size() == 1 && o.has("$timestamp") && o.get("$timestamp").isObject()) {
AbstractObject ts = o.object("$timestamp");
if(ts.has("t") && ts.has("i") && ts.get("t").isNumber() && ts.get("i").isNumber()) {
if (ts.has("t") && ts.has("i") && ts.get("t").isNumber() && ts.get("i").isNumber()) {
return new BsonTimestamp((int) ts.number("t").longValue(), (int) ts.number("i").longValue());
}
}
if(o.size() == 1 && o.has("$regularExpression") && o.get("$regularExpression").isObject()) {
if (o.size() == 1 && o.has("$regularExpression") && o.get("$regularExpression").isObject()) {
AbstractObject re = o.object("$regularExpression");
if(re.has("pattern") && re.has("options") && re.get("pattern").isString() && (re.get("options").isString() || re.get("options").isNull())) {
if (re.has("pattern") && re.has("options") && re.get("pattern").isString() && (re.get("options").isString() || re.get("options").isNull())) {
return new BsonRegularExpression(re.string("pattern"), (String) re.toObject());
}
}
if(o.size() == 1 && o.has("$binary") && o.get("$binary").isObject()) {
if (o.size() == 1 && o.has("$binary") && o.get("$binary").isObject()) {
AbstractObject bin = o.object("$binary");
if(bin.has("base64") && bin.has("subType") && bin.get("base64").isString() && bin.get("subType").isString()) {
if (bin.has("base64") && bin.has("subType") && bin.get("base64").isString() && bin.get("subType").isString()) {
byte[] data = Base64.getDecoder().decode(bin.string("base64"));
byte type = (byte) Integer.parseInt(bin.string("subType"), 16);
return new BsonBinary(type, data);
}
}
BsonDocument doc = new BsonDocument(o.size());
for(String k : o.keys())
for (String k : o.keys())
doc.put(k, toBson(o.get(k)));
return doc;
}
Expand Down
Loading

0 comments on commit 078a613

Please sign in to comment.