/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package lc.mecha.util;

import com.google.gson.*;

import java.io.Reader;

public final class JsonUtil {
    private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();

    public static String pretty(final String ugly) {
        final JsonElement je = JsonParser.parseString(ugly);
        return gson.toJson(je);
    }

    public static JsonObject parseJson(final Reader reader) {
        final GsonBuilder gsonBuilder = new GsonBuilder();
        // Register a trivial custom deserializer to obtain the jsonObject
        gsonBuilder.registerTypeAdapter(JsonElement.class,
                (JsonDeserializer<JsonElement>) (json, type, context) -> json);
        return gsonBuilder.create().fromJson(reader,
                JsonElement.class).getAsJsonObject();
    }
}
