Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json ADT #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions library/src/main/java/com/dslplatform/json/adt/JsonArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dslplatform.json.adt;

// uncomment for java 21
// public record JsonArray(DslJsonValue[] value) implements JsonValue { }

public final class JsonArray implements JsonValue {
public final JsonValue[] value;

public JsonArray(JsonValue[] value) {
this.value = value;
}
}
18 changes: 18 additions & 0 deletions library/src/main/java/com/dslplatform/json/adt/JsonBoolean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.dslplatform.json.adt;

// uncomment for java 21
// public record JsonBoolean(boolean value) implements JsonValue {
// public static final JsonBoolean TRUE = new JsonBoolean(true);
// public static final JsonBoolean FALSE = new JsonBoolean(false);
// }

public final class JsonBoolean implements JsonValue {
public static final JsonBoolean TRUE = new JsonBoolean(true);
public static final JsonBoolean FALSE = new JsonBoolean(false);

public final boolean value;

private JsonBoolean(boolean value) {
this.value = value;
}
}
6 changes: 6 additions & 0 deletions library/src/main/java/com/dslplatform/json/adt/JsonNull.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.dslplatform.json.adt;

public final class JsonNull implements JsonValue {
private JsonNull() {}
public static final JsonNull INSTANCE = new JsonNull();
}
12 changes: 12 additions & 0 deletions library/src/main/java/com/dslplatform/json/adt/JsonNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dslplatform.json.adt;

// uncomment for java 21
// public record JsonNumber(double value) implements JsonValue { }

public final class JsonNumber implements JsonValue {
public final double value;

public JsonNumber(double value) {
this.value = value;
}
}
24 changes: 24 additions & 0 deletions library/src/main/java/com/dslplatform/json/adt/JsonObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.dslplatform.json.adt;

// uncomment for java 21
// public record JsonObject(Entry[] value) implements JsonValue {
// public record Entry(String name, JsonValue value) {}
// }

public final class JsonObject implements JsonValue {
public final Entry[] value;

public JsonObject(Entry[] value) {
this.value = value;
}

public static class Entry {
public final String name;
public final JsonValue value;

public Entry(String name, JsonValue value) {
this.name = name;
this.value = value;
}
}
}
12 changes: 12 additions & 0 deletions library/src/main/java/com/dslplatform/json/adt/JsonString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dslplatform.json.adt;

// uncomment for java 21
// public record JsonString(String value) implements JsonValue { }

public final class JsonString implements JsonValue {
public final String value;

public JsonString(String value) {
this.value = value;
}
}
5 changes: 5 additions & 0 deletions library/src/main/java/com/dslplatform/json/adt/JsonValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.dslplatform.json.adt;

// uncomment for java 21
public /*sealed*/ interface JsonValue /*permits DslJsonArray, DslJsonBoolean, JsonNull, DslJsonNumber, DslJsonObject, DslJsonString*/ {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.dslplatform.json.adt;

import com.dslplatform.json.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

@JsonConverter(target = JsonValue.class)
public abstract class JsonValueConverter {

public static JsonValue read(JsonReader reader) throws IOException {
if (reader.wasNull()) {
return JsonNull.INSTANCE;
} else if (reader.wasTrue()) {
return JsonBoolean.TRUE;
} else if (reader.wasFalse()) {
return JsonBoolean.FALSE;
} else if (reader.last() == '{') {
reader.getNextToken();
HashMap<String, JsonValue> elements = new HashMap<>();
while (reader.last() != '}') {
String key = reader.readKey();
assert reader.getNextToken() == ':';
elements.put(key, read(reader));
reader.getNextToken();
if (reader.last() == ',') reader.getNextToken();
}
return new JsonObject(elements.entrySet().stream().map(e -> new JsonObject.Entry(e.getKey(), e.getValue())).toArray(JsonObject.Entry[]::new));
} else if (reader.last() == '[') {
reader.getNextToken();
ArrayList<JsonValue> elements = new ArrayList<>();
while (reader.last() != ']') {
elements.add(read(reader));
reader.getNextToken();
if (reader.last() == ',') reader.getNextToken();
}
return new JsonArray(elements.toArray(new JsonValue[0]));
} else if (reader.last() == '\"') {
return new JsonString(StringConverter.deserialize(reader));
} else {
return new JsonNumber(NumberConverter.deserializeDouble(reader));
}
}

public static void write(JsonWriter writer, JsonValue value) {
if (value == null || value == JsonNull.INSTANCE) writer.writeNull();
if (value == JsonBoolean.TRUE) writer.writeAscii("true");
else if (value == JsonBoolean.FALSE) writer.writeAscii("false");
else if (value instanceof JsonObject) {
JsonObject o = (JsonObject) value;
writer.writeAscii("{");
boolean first = true;
for (int i = 0; i < o.value.length; i++) {
if (!first) writer.writeAscii(",");
JsonObject.Entry entry = o.value[i];
writer.writeString(entry.name);
writer.writeAscii(":");
write(writer, entry.value);
first = false;
}
writer.writeAscii("}");
} else if (value instanceof JsonArray) {
JsonArray a = (JsonArray) value;
writer.writeAscii("[");
boolean first = true;
for (int i = 0; i <a.value.length; i++) {
if (!first) writer.writeAscii(",");
write(writer, a.value[i]);
first = false;
}
writer.writeAscii("]");
} else if (value instanceof JsonString) {
StringConverter.serialize(((JsonString)value).value, writer);
} else if (value instanceof JsonNumber) {
NumberConverter.serialize(((JsonNumber)value).value, writer);
} else {
throw new NotSupportedJsonValueTypeException();
}
}

public final static class NotSupportedJsonValueTypeException extends RuntimeException {}
}