All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.chains_project.maven_lockfile.JsonUtils Maven / Gradle / Ivy

Go to download

This plugin is a state-of-the-art solution that can be used to validate the integrity of a maven repository. It does this by generating a lock file that contains the checksums of all the artifacts in the repository. The lock file can then be used to validate the integrity of the repository. This guards the supply chain against malicious actors that might tamper with the artifacts in the repository.

The newest version!
package io.github.chains_project.maven_lockfile;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import io.github.chains_project.maven_lockfile.data.ArtifactId;
import io.github.chains_project.maven_lockfile.data.Classifier;
import io.github.chains_project.maven_lockfile.data.GroupId;
import io.github.chains_project.maven_lockfile.data.MavenScope;
import io.github.chains_project.maven_lockfile.data.VersionNumber;
import io.github.chains_project.maven_lockfile.graph.NodeId;

public class JsonUtils {
    private JsonUtils() {}

    private static Gson getGson() {
        return new GsonBuilder()
                .setPrettyPrinting()
                .registerTypeAdapter(VersionNumber.class, (JsonSerializer)
                        (it, type, ignore) -> new JsonPrimitive(it.getValue()))
                .registerTypeAdapter(VersionNumber.class, (JsonDeserializer)
                        (it, type, ignore) -> VersionNumber.of(it.getAsString()))
                .registerTypeAdapter(ArtifactId.class, (JsonSerializer)
                        (it, type, ignore) -> new JsonPrimitive(it.getValue()))
                .registerTypeAdapter(ArtifactId.class, (JsonDeserializer)
                        (it, type, ignore) -> ArtifactId.of(it.getAsString()))
                .registerTypeAdapter(Classifier.class, (JsonSerializer)
                        (it, type, ignore) -> new JsonPrimitive(it.getValue()))
                .registerTypeAdapter(Classifier.class, (JsonDeserializer)
                        (it, type, ignore) -> Classifier.of(it.getAsString()))
                .registerTypeAdapter(
                        GroupId.class, (JsonSerializer) (it, type, ignore) -> new JsonPrimitive(it.getValue()))
                .registerTypeAdapter(
                        GroupId.class, (JsonDeserializer) (it, type, ignore) -> GroupId.of(it.getAsString()))
                .registerTypeAdapter(
                        NodeId.class, (JsonSerializer) (it, type, ignore) -> new JsonPrimitive(it.toString()))
                .registerTypeAdapter(NodeId.class, (JsonDeserializer)
                        (it, type, ignore) -> NodeId.fromValue(it.getAsString()))
                .registerTypeAdapter(MavenScope.class, (JsonSerializer)
                        (it, type, ignore) -> new JsonPrimitive(it.getValue()))
                .registerTypeAdapter(MavenScope.class, (JsonDeserializer) (it, type, ignore) -> {
                    return MavenScope.fromString(it.getAsString());
                })
                .setLenient()
                // .registerTypeAdapter(LockFileDependency.class, new LockFileDependencyAdapter())
                .create();
    }

    public static String toJson(Object it) {
        return getGson().toJson(it);
    }

    public static  T fromJson(String it, Class type) {
        return getGson().fromJson(it, type);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy