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

com.yungnickyoung.minecraft.yungsapi.io.JSON Maven / Gradle / Ivy

The newest version!
package com.yungnickyoung.minecraft.yungsapi.io;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yungnickyoung.minecraft.yungsapi.json.BlockStateRandomizerAdapter;
import com.yungnickyoung.minecraft.yungsapi.json.BlockStateAdapter;
import com.yungnickyoung.minecraft.yungsapi.json.ItemAdapter;
import com.yungnickyoung.minecraft.yungsapi.json.ItemRandomizerAdapter;
import com.yungnickyoung.minecraft.yungsapi.api.world.randomize.BlockStateRandomizer;
import com.yungnickyoung.minecraft.yungsapi.api.world.randomize.ItemRandomizer;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import net.minecraft.class_1792;
import net.minecraft.class_2680;

/**
 * JSON (de)serialization helper.
 * Contains a static Gson instance for use throughout the mod.
 * Useful for conveniently serializing and deserializing objects for custom configs.
 */
public class JSON {
    private JSON() {}

    public static Gson gson;

    // One-time gson initialization
    static  {
        GsonBuilder gsonBuilder = newGsonBuilder();
        gsonBuilder.registerTypeHierarchyAdapter(class_2680.class, new BlockStateAdapter());
        gsonBuilder.registerTypeHierarchyAdapter(BlockStateRandomizer.class, new BlockStateRandomizerAdapter());
        gsonBuilder.registerTypeHierarchyAdapter(class_1792.class, new ItemAdapter());
        gsonBuilder.registerTypeHierarchyAdapter(ItemRandomizer.class, new ItemRandomizerAdapter());
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.disableHtmlEscaping();
        gson = gsonBuilder.create();
    }

    public static String toJsonString(Object object) {
        return toJsonString(object, gson);
    }

    public static String toJsonString(Object object, Gson gson) {
        return gson.toJson(object);
    }

    public static void createJsonFileFromObject(Path path, Object object) throws IOException {
        createJsonFileFromObject(path, object, gson);
    }

    public static void createJsonFileFromObject(Path path, Object object, Gson gson) throws IOException {
        String jsonString = gson.toJson(object);
        Files.write(path, jsonString.getBytes());
    }

    public static  T loadObjectFromJsonFile(Path path, Class objectClass) throws IOException {
        return loadObjectFromJsonFile(path, objectClass, gson);
    }

    public static  T loadObjectFromJsonFile(Path path, Class objectClass, Gson gson) throws IOException {
        Reader reader = Files.newBufferedReader(path);
        T jsonObject = gson.fromJson(reader, objectClass);
        reader.close();
        return jsonObject;
    }

    public static GsonBuilder newGsonBuilder() {
        return new GsonBuilder();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy