Skip to content

Commit

Permalink
Merge pull request #8 from rhysdh540/master
Browse files Browse the repository at this point in the history
fix casting for primitive number fields
  • Loading branch information
Nolij authored Jul 10, 2024
2 parents 769d5aa + e0d905f commit ae3155d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/main/java/dev/nolij/zson/Zson.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ private static <T> void setField(Field field, Object object, Object value) {
if(type.isPrimitive()) {
switch (type.getName()) {
case "boolean" -> field.setBoolean(object, (boolean) value);
case "short" -> field.setShort(object, (short) (int) value);
case "int" -> field.setInt(object, (int) value);
case "float" -> field.setFloat(object, (float) (double) value);
case "double" -> field.setDouble(object, (double) value);
case "long" -> field.setLong(object, (long) value);
case "byte" -> field.setByte(object, (byte) (int) value);
case "short" -> field.setShort(object, ((Number) value).shortValue());
case "int" -> field.setInt(object, ((Number) value).intValue());
case "float" -> field.setFloat(object, ((Number) value).floatValue());
case "double" -> field.setDouble(object, ((Number) value).doubleValue());
case "long" -> field.setLong(object, ((Number) value).longValue());
case "byte" -> field.setByte(object, ((Number) value).byteValue());
case "char" -> field.setChar(object, (char) value);
}
} else {
Expand Down
53 changes: 41 additions & 12 deletions src/test/java/ZsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static dev.nolij.zson.Zson.*;

@SuppressWarnings({"unused", "DataFlowIssue", "FieldMayBeFinal"})
public class ZsonTest {
@Test
public void testReadWrite() {
Expand All @@ -21,14 +23,14 @@ public void testReadWrite() {
zsonMap.put("name", new ZsonValue("The name of the person\nlook, a second line!", "John Doe"));
zsonMap.put("age", new ZsonValue("The age of the person", 30));
zsonMap.put("address", new ZsonValue("The address of the person", Zson.object(
Zson.entry("street", "The street of the address", "123 Main St"),
Zson.entry("city", "The city of the address", "Springfield"),
Zson.entry("state", "The state of the address", "IL"),
Zson.entry("zip", "The zip code of the address", 62701)
entry("street", "The street of the address", "123 Main St"),
entry("city", "The city of the address", "Springfield"),
entry("state", "The state of the address", "IL"),
entry("zip", "The zip code of the address", 62701)
)));
zsonMap.put("phoneNumbers", new ZsonValue("The phone numbers of the person", Zson.object(
Zson.entry("home", "217-555-1234"),
Zson.entry("cell", "217-555-5678")
entry("home", "217-555-1234"),
entry("cell", "217-555-5678")
)));
String json = writer.stringify(zsonMap);

Expand Down Expand Up @@ -102,12 +104,12 @@ public void testRead() {
assertEquals(2, arr.get(1));
assertEquals(3, arr.get(2));

Map<String, ZsonValue> obj = Zson.object(
Zson.entry("a", 1),
Zson.entry("b", "2"),
Zson.entry("c", Zson.object(
Zson.entry("d", 3),
Zson.entry("e", Zson.array(4, 5, 6))
Map<String, ZsonValue> obj = object(
entry("a", 1),
entry("b", "2"),
entry("c", object(
entry("d", 3),
entry("e", array(4, 5, 6))
))
);
assertEquals(obj, map.get("obj").value);
Expand Down Expand Up @@ -195,6 +197,20 @@ public void testNonexistentFieldInMap() {
assertEquals(42, obj.wow);
}

@Test
public void testPrimitiveConversion() {
Map<String, ZsonValue> json = Zson.parseString("""
{
"f": 1,
"d": 2,
}""");
assertEquals(1, json.get("f").value);
assertEquals(2, json.get("d").value);
AllTypes obj = Zson.map2Obj(json, AllTypes.class);
assertEquals(1, obj.f);
assertEquals(2, obj.d);
}

public static class TestObject {
@ZsonField(comment = "look a comment")
public int wow = 42;
Expand All @@ -212,6 +228,19 @@ public static class TestObject {
public TestEnum testEnum = TestEnum.ONE;
}

public static class AllTypes {
public boolean bool;
public byte b;
public short s;
public int i;
public long l;
public float f;
public double d;
public char c;
public String str;
public TestEnum e;
}

public enum TestEnum {
ONE, TWO, THREE
}
Expand Down

0 comments on commit ae3155d

Please sign in to comment.