3 Replies
JavaBot
JavaBot3mo ago
This post has been reserved for your question.
Hey @Asdru! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Asdru
AsdruOP3mo ago
package com.asdru.oopack.internal;

import com.asdru.oopack.util.JsonUtils;
import com.google.gson.JsonObject;

import java.lang.reflect.Constructor;
import java.util.UUID;

public class JsonFileFactory {

// Create instance from name + JsonObject
public static <T extends JsonFile> T fromJson(Class<T> clazz, String name, JsonObject json) {
try {
Constructor<T> constructor = clazz.getConstructor(String.class, JsonObject.class);
return constructor.newInstance(name, json);
} catch (Exception e) {
throw new RuntimeException("Failed to create JsonFile instance", e);
}
}

// Create instance from name + String content (with optional formatting args)
public static <T extends JsonFile> T fromNameAndContent(Class<T> clazz, String name, String content, Object... args) {
try {
String formattedContent = args.length > 0 ? content.formatted(args) : content;
JsonObject json = JsonUtils.toJson(formattedContent);
return fromJson(clazz, name, json);
} catch (Exception e) {
throw new RuntimeException("Failed to create JsonFile instance", e);
}
}

// Create instance from content only (random name, optional args)
public static <T extends JsonFile> T fromContent(Class<T> clazz, String content, Object... args) {
String randomName = UUID.randomUUID().toString().replace("-", "");
return fromNameAndContent(clazz, randomName, content, args);
}
}
package com.asdru.oopack.internal;

import com.asdru.oopack.util.JsonUtils;
import com.google.gson.JsonObject;

import java.lang.reflect.Constructor;
import java.util.UUID;

public class JsonFileFactory {

// Create instance from name + JsonObject
public static <T extends JsonFile> T fromJson(Class<T> clazz, String name, JsonObject json) {
try {
Constructor<T> constructor = clazz.getConstructor(String.class, JsonObject.class);
return constructor.newInstance(name, json);
} catch (Exception e) {
throw new RuntimeException("Failed to create JsonFile instance", e);
}
}

// Create instance from name + String content (with optional formatting args)
public static <T extends JsonFile> T fromNameAndContent(Class<T> clazz, String name, String content, Object... args) {
try {
String formattedContent = args.length > 0 ? content.formatted(args) : content;
JsonObject json = JsonUtils.toJson(formattedContent);
return fromJson(clazz, name, json);
} catch (Exception e) {
throw new RuntimeException("Failed to create JsonFile instance", e);
}
}

// Create instance from content only (random name, optional args)
public static <T extends JsonFile> T fromContent(Class<T> clazz, String content, Object... args) {
String randomName = UUID.randomUUID().toString().replace("-", "");
return fromNameAndContent(clazz, randomName, content, args);
}
}
this is a very chatgpt-d solution and i get what its doing, altho i was hoping to avoid having to use a Class<T> clazz param for clarity reasons. if that's not possible or this is the best solution i can live with it tho... because the syntax would go from LootTable lt1 = new LootTable( "loot1", "{...json...}"); to LootTable lt1 = JsonFileFactory.fromNameAndContent(LootTable.class, "loot1", "{...json...}");
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?