Skip to content

Commit

Permalink
Merge pull request #7 from rhysdh540/master
Browse files Browse the repository at this point in the history
Allow serializing enums
  • Loading branch information
Nolij authored Jul 9, 2024
2 parents e1f6631 + aad7375 commit 769d5aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.caching = true
org.gradle.configuration-cache = false

maven_group = dev.nolij
project_version = 0.3
project_version = 0.4
project_name = zson

# Dependencies
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/dev/nolij/zson/Zson.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.*;
import java.util.Map.Entry;

@SuppressWarnings({"deprecation", "UnstableApiUsage"})
@SuppressWarnings({"deprecation", "UnstableApiUsage", "unused"})
public final class Zson {
//region Helper Methods

Expand Down Expand Up @@ -280,8 +280,8 @@ private static boolean shouldInclude(Field field, boolean forDeserialization) {
* @param value the value to set the field to. May be null. If primitive, will be an int or double.
* @param <T> the type of the field.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
private static <T> void setField(Field field, Object object, Object value) {
@SuppressWarnings("unchecked")
Class<T> type = (Class<T>) field.getType();
boolean accessible = field.isAccessible();
if(!accessible) field.setAccessible(true);
Expand All @@ -298,7 +298,11 @@ private static <T> void setField(Field field, Object object, Object value) {
case "char" -> field.setChar(object, (char) value);
}
} else {
field.set(object, type.cast(value));
if(type.isEnum()) {
field.set(object, Enum.valueOf((Class<Enum>) type, (String) value));
} else {
field.set(object, type.cast(value));
}
}
} catch (Exception e) {
throw new AssertionError(
Expand Down Expand Up @@ -337,7 +341,7 @@ public static <T> T parseFile(@NotNull Path path) throws IOException {
@Contract(pure = true)
public static <T> T parseString(@NotNull String serialized) {
try {
return parse(new BufferedReader(new StringReader(serialized)));
return parse(new StringReader(serialized));
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
Expand Down Expand Up @@ -929,6 +933,8 @@ private String value(Object value) {
output.append(indent);

return output.append("]").toString();
} else if(value instanceof Enum<?> enumValue) {
return '"' + enumValue.name() + '"';
}

throw new IllegalArgumentException("Unsupported value type: " + value.getClass().getName());
Expand Down
21 changes: 16 additions & 5 deletions src/test/java/ZsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testRead() {
"inf": Infinity,
"neginf": -Infinity,
"nan": NaN,
"multiline-string": "wow look\
"multiline-string": "wow look\\
a multiline string",
}
""";
Expand Down Expand Up @@ -118,9 +118,8 @@ public void testRead() {
assertNull(map.get("nil").value);
assertEquals(Double.POSITIVE_INFINITY, map.get("inf").value);
assertEquals(Double.NEGATIVE_INFINITY, map.get("neginf").value);
assertTrue(Double.isNaN((Double) map.get("nan").value));
// TODO: re-enable after fixing multi-line strings
// assertEquals("wow look \na multiline string", map.get("multiline-string").value);
assertTrue(Double.isNaN((double) map.get("nan").value));
assertEquals("wow look\n\ta multiline string", map.get("multiline-string").value);
}

@Test
Expand Down Expand Up @@ -171,15 +170,21 @@ public void testObject() {
"such": "amaze",
"very": true,
"constant": "wow",
"testEnum": "ONE",
}""";

assertEquals(expected, new Zson().stringify(json));
String actual = new Zson().stringify(json);

assertEquals(expected, actual);

json = Zson.parseString(actual);

TestObject obj = Zson.map2Obj(json, TestObject.class);
assertEquals(42, obj.wow);
assertEquals("amaze", obj.such);
assertTrue(obj.very);
assertEquals(3.14, obj.pi);
assertEquals(TestEnum.ONE, obj.testEnum);
}

@Test
Expand All @@ -203,5 +208,11 @@ public static class TestObject {

@ZsonField(include = true)
public static final String constant = "wow";

public TestEnum testEnum = TestEnum.ONE;
}

public enum TestEnum {
ONE, TWO, THREE
}
}

0 comments on commit 769d5aa

Please sign in to comment.