Skip to content

Commit

Permalink
add missing primitives and rename reflection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed May 17, 2024
1 parent b304064 commit 0d746de
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/java/dev/nolij/zson/Zson.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static String escape(String string, char escapeQuotes) {
return result.toString();
}

public static Map<String, ZsonValue> fromObject(Object object) {
public static Map<String, ZsonValue> obj2Map(Object object) {
Map<String, ZsonValue> map = Zson.object();
for (Field field : object.getClass().getDeclaredFields()) {
if(Modifier.isStatic(field.getModifiers())) continue;
Expand All @@ -144,7 +144,7 @@ public static Map<String, ZsonValue> fromObject(Object object) {
return map;
}

public static <T> T toObject(Map<String, ZsonValue> map, Class<T> type) {
public static <T> T map2Obj(Map<String, ZsonValue> map, Class<T> type) {
try {
T object = type.getDeclaredConstructor().newInstance();
for (Field field : type.getDeclaredFields()) {
Expand All @@ -168,6 +168,9 @@ private static <T> void setField(Field field, Object object, Object 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 "char" -> field.setChar(object, (char) value);
}
} else {
field.set(object, type.cast(value));
Expand Down

0 comments on commit 0d746de

Please sign in to comment.