Migrate to v1 GsonHolder

This commit is contained in:
Johannes Frohnmeyer 2022-09-25 12:33:51 +02:00
parent a45dd04faa
commit f6ef4544a8
Signed by: Johannes
GPG Key ID: E76429612C2929F4
7 changed files with 30 additions and 35 deletions

View File

@ -15,7 +15,7 @@ curseforge_id=482600
curseforge_optional_dependencies=fabric-api curseforge_optional_dependencies=fabric-api
fabric_version=0.60.0+1.19.2 fabric_version=0.60.0+1.19.2
commons_version=2022.9.15+12-51-21 commons_version=2022.9.25+10-22-41
modmenu_version=4.0.6 modmenu_version=4.0.6
asm_version=9.3 asm_version=9.3

View File

@ -1,25 +1,22 @@
package io.gitlab.jfronny.libjf; package io.gitlab.jfronny.libjf;
import io.gitlab.jfronny.commons.serialize.gson.api.*; import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
import java.io.IOException; import java.io.*;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.URI; import java.net.*;
import java.net.URISyntaxException; import java.net.http.*;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
/**
* @deprecated User the commons HttpUtils instead
*/
@Deprecated
public class HttpUtils { public class HttpUtils {
private static final HttpClient CLIENT = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build(); private static final HttpClient CLIENT = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build();
@ -86,7 +83,7 @@ public class HttpUtils {
public Request bodyJson(Object object) { public Request bodyJson(Object object) {
builder.header("Content-Type", "application/json"); builder.header("Content-Type", "application/json");
builder.method(method.name(), HttpRequest.BodyPublishers.ofString(GsonHolder.getGson().toJson(object))); builder.method(method.name(), HttpRequest.BodyPublishers.ofString(GsonHolders.API.getGson().toJson(object)));
method = null; method = null;
return this; return this;
@ -144,7 +141,7 @@ public class HttpUtils {
public <T> T sendJson(Type type) throws IOException { public <T> T sendJson(Type type) throws IOException {
InputStream in = _send("application/json", HttpResponse.BodyHandlers.ofInputStream()); InputStream in = _send("application/json", HttpResponse.BodyHandlers.ofInputStream());
return in == null ? null : GsonHolder.getGson().fromJson(new InputStreamReader(in), type); return in == null ? null : GsonHolders.API.getGson().fromJson(new InputStreamReader(in), type);
} }
private String getString(Object a) throws IOException { private String getString(Object a) throws IOException {

View File

@ -1,26 +1,29 @@
package io.gitlab.jfronny.libjf; package io.gitlab.jfronny.libjf;
import io.gitlab.jfronny.commons.log.*; import io.gitlab.jfronny.commons.log.*;
import io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder; import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
import io.gitlab.jfronny.libjf.gson.GsonAdapter; import io.gitlab.jfronny.libjf.gson.GsonAdapter;
import io.gitlab.jfronny.libjf.gson.HiddenAnnotationExclusionStrategy; import io.gitlab.jfronny.libjf.gson.HiddenAnnotationExclusionStrategy;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.entrypoint.EntrypointContainer;
import net.fabricmc.loader.api.metadata.ModMetadata;
public class LibJf implements ModInitializer { public class LibJf implements ModInitializer {
public static final String MOD_ID = "libjf"; public static final String MOD_ID = "libjf";
public static final Logger LOGGER = Logger.forName(MOD_ID); public static final Logger LOGGER = Logger.forName(MOD_ID);
static { static {
GsonHolder.modifyBuilder(builder -> { //TODO remove all uses of this from dependents, then remove this
builder.serializeSpecialFloatingPointValues(); GsonHolders.modifyBuilder(builder -> {
builder.setOmitQuotes(); for (EntrypointContainer<GsonAdapter> container : FabricLoader.getInstance().getEntrypointContainers(MOD_ID + ":gson_adapter", GsonAdapter.class)) {
for (GsonAdapter adapter : FabricLoader.getInstance().getEntrypoints(MOD_ID + ":gson_adapter", GsonAdapter.class)) { ModMetadata meta = container.getProvider().getMetadata();
adapter.apply(builder); LOGGER.warn("Mod " + meta.getName() + " (" + meta.getId() + ") uses the now deprecated libjf:gson_adapter. Please update!");
container.getEntrypoint().apply(builder);
} }
}); });
HiddenAnnotationExclusionStrategy.register(); HiddenAnnotationExclusionStrategy.register();
GsonHolder.register(); GsonHolders.registerSerializer();
} }
@Override @Override

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.libjf.gson; package io.gitlab.jfronny.libjf.gson;
import io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder; import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
import io.gitlab.jfronny.gson.ExclusionStrategy; import io.gitlab.jfronny.gson.ExclusionStrategy;
import io.gitlab.jfronny.gson.FieldAttributes; import io.gitlab.jfronny.gson.FieldAttributes;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
@ -17,6 +17,6 @@ public class HiddenAnnotationExclusionStrategy implements ExclusionStrategy {
} }
public static void register() { public static void register() {
GsonHolder.modifyBuilder(builder -> builder.setExclusionStrategies(new HiddenAnnotationExclusionStrategy())); GsonHolders.modifyBuilder(builder -> builder.setExclusionStrategies(new HiddenAnnotationExclusionStrategy()));
} }
} }

View File

@ -1,7 +1,6 @@
package io.gitlab.jfronny.libjf.config.impl; package io.gitlab.jfronny.libjf.config.impl;
import io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder; import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.config.api.v1.Category; import io.gitlab.jfronny.libjf.config.api.v1.Category;
import io.gitlab.jfronny.libjf.config.api.v1.JfConfig; import io.gitlab.jfronny.libjf.config.api.v1.JfConfig;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder; import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder;
@ -44,7 +43,7 @@ public class AuxiliaryMetadata {
cv = cv.getAsObject().get("config"); cv = cv.getAsObject().get("config");
} }
} }
if (cv != null) metaRef.meta = GsonHolder.getGson().fromJson(FabricLoaderGsonGenerator.toGson(cv), AuxiliaryMetadata.class); if (cv != null) metaRef.meta = GsonHolders.API.getGson().fromJson(FabricLoaderGsonGenerator.toGson(cv), AuxiliaryMetadata.class);
}); });
return metaRef.meta; return metaRef.meta;
} }

View File

@ -1,11 +1,10 @@
package io.gitlab.jfronny.libjf.config.impl.dsl; package io.gitlab.jfronny.libjf.config.impl.dsl;
import io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder; import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
import io.gitlab.jfronny.gson.*; import io.gitlab.jfronny.gson.*;
import io.gitlab.jfronny.gson.stream.JsonWriter; import io.gitlab.jfronny.gson.stream.JsonWriter;
import io.gitlab.jfronny.libjf.LibJf; import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.config.api.v1.*; import io.gitlab.jfronny.libjf.config.api.v1.*;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.ConfigBuilder; import io.gitlab.jfronny.libjf.config.api.v1.dsl.ConfigBuilder;
import io.gitlab.jfronny.libjf.config.impl.JfConfigWatchService; import io.gitlab.jfronny.libjf.config.impl.JfConfigWatchService;
import io.gitlab.jfronny.libjf.config.impl.entrypoint.JfConfigSafe; import io.gitlab.jfronny.libjf.config.impl.entrypoint.JfConfigSafe;
@ -15,10 +14,7 @@ import java.io.*;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class ConfigBuilderImpl extends CategoryBuilderImpl<ConfigBuilderImpl> implements ConfigBuilder<ConfigBuilderImpl> { public class ConfigBuilderImpl extends CategoryBuilderImpl<ConfigBuilderImpl> implements ConfigBuilder<ConfigBuilderImpl> {
public DslConfigInstance built; public DslConfigInstance built;
@ -45,7 +41,7 @@ public class ConfigBuilderImpl extends CategoryBuilderImpl<ConfigBuilderImpl> im
write = c -> { write = c -> {
c.getFilePath().ifPresent(path -> JfConfigWatchService.lock(path, () -> { c.getFilePath().ifPresent(path -> JfConfigWatchService.lock(path, () -> {
try (BufferedWriter bw = Files.newBufferedWriter(path); try (BufferedWriter bw = Files.newBufferedWriter(path);
JsonWriter jw = GsonHolder.getGson().newJsonWriter(bw)) { JsonWriter jw = GsonHolders.CONFIG.getGson().newJsonWriter(bw)) {
writeTo(jw, c); writeTo(jw, c);
} catch (Exception e) { } catch (Exception e) {
LibJf.LOGGER.error("Could not write config for " + id, e); LibJf.LOGGER.error("Could not write config for " + id, e);

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.libjf.config.impl.dsl; package io.gitlab.jfronny.libjf.config.impl.dsl;
import io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder; import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
import io.gitlab.jfronny.commons.throwable.ThrowingConsumer; import io.gitlab.jfronny.commons.throwable.ThrowingConsumer;
import io.gitlab.jfronny.commons.throwable.ThrowingSupplier; import io.gitlab.jfronny.commons.throwable.ThrowingSupplier;
import io.gitlab.jfronny.gson.JsonElement; import io.gitlab.jfronny.gson.JsonElement;
@ -159,7 +159,7 @@ public class DslEntryInfo<T> implements EntryInfo<T> {
setUnchecked(e.optionForString(element.getAsString())); setUnchecked(e.optionForString(element.getAsString()));
} }
} else { } else {
setValue(GsonHolder.getGson().fromJson(element, type.asClass())); setValue(GsonHolders.CONFIG.getGson().fromJson(element, type.asClass()));
} }
} }
@ -180,7 +180,7 @@ public class DslEntryInfo<T> implements EntryInfo<T> {
writer.comment("Valid: [" + Arrays.stream(((Type.TEnum<T>)type).options()).map(Objects::toString).collect(Collectors.joining(", ")) + "]"); writer.comment("Valid: [" + Arrays.stream(((Type.TEnum<T>)type).options()).map(Objects::toString).collect(Collectors.joining(", ")) + "]");
} }
writer.name(name); writer.name(name);
GsonHolder.getGson().toJson(value, Objects.requireNonNullElse(type.asClass(), String.class), writer); GsonHolders.CONFIG.getGson().toJson(value, Objects.requireNonNullElse(type.asClass(), String.class), writer);
} }
@Override @Override