Skip to content

Commit

Permalink
v2.73 landed
Browse files Browse the repository at this point in the history
  • Loading branch information
adamansky committed Nov 14, 2022
1 parent 5ad7cd7 commit bc370d1
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ejdb2 (2.73) UNRELEASED; urgency=medium
ejdb2 (2.73) testing; urgency=medium

* Fixed ERR_STREAM_PREMATURE_CLOSE on Node 18 (#346)
* Resolved security issuies with nodejs packages
Expand All @@ -7,7 +7,7 @@ ejdb2 (2.73) UNRELEASED; urgency=medium
* Updated EJDB2Swift submodule
* Replaced old iowow regexp engine with new (iwre.h) based on https://github.com/jserv/cregex

-- Anton Adamansky <[email protected]> Fri, 25 Feb 2022 13:12:16 +0700
-- Anton Adamansky <[email protected]> Mon, 14 Nov 2022 18:42:52 +0200

ejdb2 (2.72) testing; urgency=medium

Expand Down
2 changes: 1 addition & 1 deletion src/bindings/ejdb2_flutter/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5
6
178 changes: 157 additions & 21 deletions src/bindings/ejdb2_jni/src/main/java/com/softmotions/ejdb2/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

/**
* JSON parser/container.
Expand All @@ -27,6 +29,8 @@
*/
public final class JSON implements Comparable<JSON>, Cloneable {

private static final Object UNIQ = new Object();

@Override
public Object clone() {
if (isContainer()) {
Expand All @@ -36,11 +40,11 @@ public Object clone() {
}
}

public static ObjectBuilder buildObject() {
return new ObjectBuilder();
public static ObjectBuilder object(Object... props) {
return new ObjectBuilder().put(props);
}

public static ArrayBuilder buildArray() {
public static ArrayBuilder array() {
return new ArrayBuilder();
}

Expand All @@ -64,9 +68,17 @@ public static JSON fromList(List<Object> list) {
return new JSON(ValueType.ARRAY, list);
}

public static JSON fromObject(Object obj) {
if (obj instanceof JSON) {
return (JSON) obj;
} else {
return new JSON(ValueType.getTypeOf(obj), obj);
}
}

private static final ValueType[] valueTypes = new ValueType[256];
private static final int[] hexDigits = new int['f' + 1];
private static JSON UNKNOWN = new JSON(ValueType.UNKNOWN, null);
private static final int[] hexDigits = new int['f' + 1];
private static JSON UNKNOWN = new JSON(ValueType.UNKNOWN, null);

static {
for (int i = 0; i < valueTypes.length; ++i) {
Expand Down Expand Up @@ -106,13 +118,13 @@ public static JSON fromList(List<Object> list) {
}
}

public final Object value;
public final Object value;
public final ValueType type;

private char[] reusableChars = new char[32];
private byte[] buf = new byte[0];
private int head;
private int tail;
private byte[] buf = new byte[0];
private int head;
private int tail;

JSON(byte[] buf) {
this.buf = buf;
Expand Down Expand Up @@ -164,6 +176,10 @@ public boolean isUnknown() {
return type == ValueType.UNKNOWN;
}

public boolean isNullOrUnknown() {
return type == ValueType.NULL || type == ValueType.UNKNOWN;
}

public boolean isNull() {
return type == ValueType.NULL;
}
Expand Down Expand Up @@ -192,6 +208,16 @@ public boolean isContainer() {
return type == ValueType.OBJECT || type == ValueType.ARRAY;
}

public int length() {
if (type == ValueType.ARRAY) {
return ((List<Object>) value).size();
} else if (type == ValueType.OBJECT) {
return ((Map<String, Object>) value).size();
} else {
return 0;
}
}

public Builder modify() {
return new Builder(this);
}
Expand All @@ -203,6 +229,14 @@ public Set<String> keys() {
return ((Map<String, Object>) value).keySet();
}

public String gets(String key) {
return get(key).asString();
}

public String gets(int index) {
return get(index).asString();
}

public JSON get(String key) {
if (type == ValueType.ARRAY) {
try {
Expand Down Expand Up @@ -269,6 +303,20 @@ public String asString() {
return asStringOr(null);
}

public String[] asStringArray() {
if (type != ValueType.ARRAY) {
return new String[0];
} else {
List<Object> list = (List<Object>) value;
String[] res = new String[list.size()];
for (int i = 0; i < list.size(); ++i) {
var v = list.get(i);
res[i] = v != null ? String.valueOf(v) : null;
}
return res;
}
}

public String asTextOr(String fallbackValue) {
if (type == ValueType.UNKNOWN) {
return fallbackValue;
Expand All @@ -278,7 +326,7 @@ public String asTextOr(String fallbackValue) {
}

public String asText() {
return asTextOr(null);
return asTextOr("");
}

public Boolean asBooleanOr(Boolean fallbackValue) {
Expand Down Expand Up @@ -854,25 +902,25 @@ public static ValueType getTypeOf(Object v) {
}

private static final class NumberChars {
char[] chars;
int charsLength;
char[] chars;
int charsLength;
boolean dotFound;
}

private static final class JsonWriter {
private static final char[] QUOT_CHARS = { '\\', '"' };
private static final char[] BS_CHARS = { '\\', '\\' };
private static final char[] LF_CHARS = { '\\', 'n' };
private static final char[] CR_CHARS = { '\\', 'r' };
private static final char[] TAB_CHARS = { '\\', 't' };
private static final char[] BS_CHARS = { '\\', '\\' };
private static final char[] LF_CHARS = { '\\', 'n' };
private static final char[] CR_CHARS = { '\\', 'r' };
private static final char[] TAB_CHARS = { '\\', 't' };

// In JavaScript, U+2028 and U+2029 characters count as line endings and must be
// encoded.
// http://stackoverflow.com/questions/2965293/javascript-parse-error-on-u2028-unicode-character
private static final char[] UNICODE_2028_CHARS = { '\\', 'u', '2', '0', '2', '8' };
private static final char[] UNICODE_2029_CHARS = { '\\', 'u', '2', '0', '2', '9' };
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f' };
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
'd', 'e', 'f' };

private final Writer writer;

Expand Down Expand Up @@ -986,9 +1034,9 @@ static char[] getReplacementChars(char ch) {
}

public static final class Builder {
final JSON json;
final JSON json;
final ObjectBuilder o;
final ArrayBuilder a;
final ArrayBuilder a;

Builder(JSON json) {
this.json = json;
Expand Down Expand Up @@ -1067,6 +1115,10 @@ public ObjectBuilder put(String key, String val) {
return getO().put(key, val);
}

public ObjectBuilder put(String key, JSON val) {
return getO().put(key, val);
}

public ObjectBuilder put(String key, Number val) {
return getO().put(key, val);
}
Expand Down Expand Up @@ -1129,6 +1181,33 @@ public static final class ArrayBuilder {
this(new ArrayList<>());
}

public ArrayBuilder addAll(String[] all) {
if (all != null) {
for (var v : all) {
add(v);
}
}
return this;
}

public ArrayBuilder addAll(Number[] all) {
if (all != null) {
for (var v : all) {
add(v);
}
}
return this;
}

public ArrayBuilder addAll(Boolean[] all) {
if (all != null) {
for (var v : all) {
add(v);
}
}
return this;
}

public ObjectBuilder addObject() {
ObjectBuilder b = new ObjectBuilder();
value.add(b.value);
Expand Down Expand Up @@ -1161,6 +1240,15 @@ public ArrayBuilder addArray(JSON val) {
return this;
}

public ArrayBuilder add(JSON val) {
if (val != null) {
value.add(val.value);
} else {
addNull();
}
return this;
}

public ArrayBuilder add(String val) {
value.add(val);
return this;
Expand Down Expand Up @@ -1230,7 +1318,7 @@ public ObjectBuilder putObject(String key) {
}

public ObjectBuilder putObject(String key, JSON val) {
if (val.type == ValueType.NULL) {
if (val == null || val.type == ValueType.NULL) {
return putNull(key);
}
if (val.type != ValueType.OBJECT) {
Expand All @@ -1246,6 +1334,12 @@ public ArrayBuilder putArray(String key) {
return b;
}

public ObjectBuilder putArray(String key, Consumer<ArrayBuilder> c) {
ArrayBuilder b = new ArrayBuilder();
c.accept(b);
return this;
}

public ObjectBuilder putArray(String key, JSON val) {
if (val.type == ValueType.NULL) {
return putNull(key);
Expand All @@ -1257,6 +1351,15 @@ public ObjectBuilder putArray(String key, JSON val) {
return this;
}

public ObjectBuilder put(String key, JSON val) {
if (val != null) {
value.put(key, val.value);
} else {
value.put(key, null);
}
return this;
}

public ObjectBuilder put(String key, String val) {
value.put(key, val);
return this;
Expand All @@ -1272,6 +1375,39 @@ public ObjectBuilder put(String key, Boolean val) {
return this;
}

public ObjectBuilder put(String key, UUID val) {
if (val == null) {
putNull(key);
} else {
put(key, val.toString());
}
return this;
}

public ObjectBuilder put(String key, Object o) {
if (o instanceof JSON) {
put(key, (JSON) o);
} else if (o instanceof UUID) {
value.put(key, o.toString());
} else {
value.put(key, o);
}
return this;
}

public ObjectBuilder put(Object... a) {
Object key = UNIQ;
for (var v : a) {
if (key != UNIQ) {
put(String.valueOf(key), v);
key = UNIQ;
} else {
key = v;
}
}
return this;
}

public ObjectBuilder putNull(String key) {
value.put(key, null);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private static void jsonBasicTest() throws Exception {
Map<String, Object> map = (Map<String, Object>) json.value;
assert map.get("foo").equals("bar");

ObjectBuilder b = JSON.buildObject();
ObjectBuilder b = JSON.object();
b.put("foo", "bar").putArray("baz").add(1).add("one").toJSON();

json = b.toJSON().at("/baz/1");
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/ejdb2_jni/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26
27
2 changes: 1 addition & 1 deletion src/bindings/ejdb2_node/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7
8
2 changes: 1 addition & 1 deletion src/bindings/ejdb2_react_native/binding/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5
6

0 comments on commit bc370d1

Please sign in to comment.