Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/xjs/data/JsonLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public boolean isNull() {

@Override
public boolean asBoolean() {
switch (this.value) {
case TRUE: return true;
case FALSE: return false;
default: return super.asBoolean();
}
return switch (this.value) {
case TRUE -> true;
case FALSE -> false;
default -> super.asBoolean();
};
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/xjs/data/serialization/parser/DjsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import xjs.data.Json;
import xjs.data.JsonArray;
import xjs.data.JsonLiteral;
import xjs.data.JsonNumber;
import xjs.data.JsonObject;
import xjs.data.JsonString;
import xjs.data.JsonValue;
Expand Down Expand Up @@ -257,6 +258,8 @@ protected JsonValue readSingle() {
}
final String text = t.parsed();
return switch (text) {
case "infinity" -> new JsonNumber(Double.POSITIVE_INFINITY);
case "-infinity" -> new JsonNumber(Double.NEGATIVE_INFINITY);
case "true" -> JsonLiteral.jsonTrue();
case "false" -> JsonLiteral.jsonFalse();
case "null" -> JsonLiteral.jsonNull();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/xjs/data/serialization/token/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ protected Token number() throws IOException {
} else if (reader.current == '-') {
reader.read();
if (!reader.isDigit()) {
if (reader.readInfinity()) {
return this.newNumberToken(reader.endCapture(), Double.NEGATIVE_INFINITY);
}
reader.invalidateCapture();
return this.newSymbolToken('-');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,29 @@ public boolean readDigit() throws IOException {
return true;
}

/**
* Reads the string "infinity" from the current index. If
* the reader encounters a syntax error, a {@link SyntaxException}
* will be thrown.
*
* @return true, if the content from the current index is "infinity".
* @throws IOException If the underlying reader throws an exception.
*/
public boolean readInfinity() throws IOException {
char[] infinity = new char[]{'i', 'n', 'f', 'i', 'n', 'i', 't', 'y'};
for (char c : infinity) {
if (!this.readIf(c)) {
return false;
}
}

// check if it is exactly "infinity", not "infinityy" or similar
if (this.readIf('y')) {
throw this.unexpected('y');
}
return true;
}

/**
* Reads all possible digits from the current point.
*
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/xjs/data/serialization/writer/ElementWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,14 @@ protected void writeNumber(final double decimal) throws IOException {
this.tw.write(Long.toString(integer));
return;
}
String res = BigDecimal.valueOf(decimal).toEngineeringString();
String res;
if (decimal == Double.POSITIVE_INFINITY) {
res = "infinity";
} else if (decimal == Double.NEGATIVE_INFINITY) {
res = "-infinity";
} else {
res = BigDecimal.valueOf(decimal).toEngineeringString();
}
if (res.endsWith(".0")) {
res = res.substring(0, res.length() - 2);
} else if (res.contains("E")) {
Expand Down Expand Up @@ -525,15 +532,15 @@ protected static String doEscapeString(
if (c == quote) {
return "\\" + quote;
}
switch (c) {
case '\t': return "\\t";
case '\n': return "\\n";
case '\r': return "\\r";
case '\f': return "\\f";
case '\b': return "\\b";
case '\\': return "\\\\";
default: return null;
}
return switch (c) {
case '\t' -> "\\t";
case '\n' -> "\\n";
case '\r' -> "\\r";
case '\f' -> "\\f";
case '\b' -> "\\b";
case '\\' -> "\\\\";
default -> null;
};
}

protected void writeMulti(final String value) throws IOException {
Expand Down
19 changes: 5 additions & 14 deletions src/main/java/xjs/data/serialization/writer/JsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,11 @@ protected void writeValue() throws IOException {
final JsonValue value = this.current();

switch (value.getType()) {
case OBJECT:
this.writeObject();
break;
case ARRAY:
this.writeArray();
break;
case NUMBER:
this.writeNumber(value.asDouble());
break;
case STRING:
this.writeQuoted(value.asString(), '"');
break;
default:
this.tw.write(value.toString());
case OBJECT -> this.writeObject();
case ARRAY -> this.writeArray();
case NUMBER -> this.writeNumber(value.asDouble());
case STRING -> this.writeQuoted(value.asString(), '"');
default -> this.tw.write(value.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public void parse_readsMultipleUnquotedKeys() {
.matches(this.parse("{k1:'v1',k2:'v2'}")));
}

@Test
public void parse_readsInfiniteNumbers() {
assertEquals(Double.POSITIVE_INFINITY, this.parse("infinity").asDouble());
assertEquals(Double.NEGATIVE_INFINITY, this.parse("-infinity").asDouble());

assertThrows(SyntaxException.class, () -> this.parse("-infinityy"));
}

@Test
public void parse_readsOpenRoot() {
assertTrue(new JsonObject().add("a", 1).add("b", 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public void write_printsInteger() {
assertEquals("1234", write(Json.value(1234)));
}

@Test
public void write_printsInfiniteNumbers() {
assertEquals("infinity", write(Json.value(Double.POSITIVE_INFINITY)));
assertEquals("-infinity", write(Json.value(Double.NEGATIVE_INFINITY)));
}

@Test
public void write_printsDecimal() {
assertEquals("12.34", write(Json.value(12.34)));
Expand Down