Skip to content

Commit

Permalink
yes
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed May 12, 2024
1 parent 3a3ca29 commit 330ef30
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rootProject.name = "zume"

include("stubs")
include("api")
include("zson")
include("modern")
include("legacy")
include("primitive")
Expand Down
5 changes: 5 additions & 0 deletions zson/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
operator fun String.invoke(): String = rootProject.properties[this] as? String ?: error("Property $this not found")

dependencies {
implementation(project(":api"))
}
68 changes: 68 additions & 0 deletions zson/src/main/java/dev/nolij/zson/JsonElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dev.nolij.zson;

public class JsonElement {
public final String key;

// 0/1 if boolean, value if number
private double value;

// true = floating point, false = non-floating point, null = boolean
private final Boolean type;

private JsonElement(String key, double value, Boolean type) {
this.key = key;
this.value = value;
this.type = type;
}

public static JsonElement doubleElement(String key, double value) {
return new JsonElement(key, value, true);
}

public static JsonElement intElement(String key, int value) {
return new JsonElement(key, value, false);
}

public static JsonElement booleanElement(String key, boolean value) {
return new JsonElement(key, value ? 1 : 0, null);
}

public String toString() {
return key + ": " + (type == null ?
value == 1 ? "true" : "false"
: type ? value : (int) value);
}

public int getAsInt() {
if(!isInt()) {
throw new IllegalStateException("Element is not an integer");
}
return (int) value;
}

public double getAsDouble() {
if(!isDouble()) {
throw new IllegalStateException("Element is not a double");
}
return value;
}

public boolean getAsBoolean() {
if(!isBoolean()) {
throw new IllegalStateException("Element is not a boolean");
}
return value == 1;
}

public boolean isInt() {
return Boolean.FALSE.equals(type);
}

public boolean isDouble() {
return Boolean.TRUE.equals(type);
}

public boolean isBoolean() {
return type == null;
}
}

0 comments on commit 330ef30

Please sign in to comment.