
org.spongepowered.configurate.gson.JsonElementSerializer Maven / Gradle / Ivy
/*
* Configurate
* Copyright (C) zml and Configurate contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spongepowered.configurate.gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
final class JsonElementSerializer implements TypeSerializer {
static final JsonElementSerializer INSTANCE = new JsonElementSerializer();
private JsonElementSerializer() {
}
@Override
public JsonElement deserialize(final Type type, final ConfigurationNode node) throws SerializationException {
if (type.equals(JsonArray.class) && !node.isList()) {
throw new SerializationException(node, type, "Expected node to be a list, but it was not!");
}
if (type.equals(JsonObject.class) && !node.isMap()) {
throw new SerializationException(node, type, "Expected node to be a map, but it was not!");
}
if (node.isList()) { // Become a JSON array
final JsonArray ret = new JsonArray();
for (final ConfigurationNode child : node.childrenList()) {
ret.add(child.get(JsonElement.class));
}
return ret;
} else if (node.isMap()) {
final JsonObject ret = new JsonObject();
for (final Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy