use of org.spongepowered.api.data.persistence.DataQuery in project SpongeCommon by SpongePowered.
the class JsonDataFormat method readView.
private static void readView(JsonReader reader, DataView view) throws IOException {
reader.beginObject();
while (reader.hasNext()) {
DataQuery key = of(reader.nextName());
if (reader.peek() == JsonToken.BEGIN_OBJECT) {
// Check this early so we don't need to copy the view
final DataView subView = view.createView(key);
JsonDataFormat.readView(reader, subView);
// handle special array types
subView.getString(DataQuery.of(ARRAYTYPE)).ifPresent(type -> {
view.remove(key);
view.set(key, JsonDataFormat.readArray(type, subView));
});
} else {
view.set(key, JsonDataFormat.read(reader));
}
}
reader.endObject();
}
use of org.spongepowered.api.data.persistence.DataQuery in project SpongeCommon by SpongePowered.
the class SpongeDataStoreBuilder method key.
@Override
@SuppressWarnings("rawtypes")
public <T, V extends Value<T>> SpongeDataStoreBuilder key(final Key<V> key, final BiConsumer<DataView, T> serializer, final Function<DataView, Optional<T>> deserializer) {
if (this.key != null) {
final DataQuery query = DataQuery.of(this.key.namespace(), this.key.value());
final SpongeDataSerializer<T> customSerializer = new SpongeDataSerializer<>(serializer, this.version, query);
final SpongeDataDeserializer<T> customDeserializer = new SpongeDataDeserializer<>(deserializer, this.version, query);
this.serializers.put(key, (Tuple) Tuple.of(customSerializer, customDeserializer));
} else {
this.serializers.put(key, (Tuple) Tuple.of(serializer, deserializer));
}
return this;
}
use of org.spongepowered.api.data.persistence.DataQuery in project SpongeCommon by SpongePowered.
the class SpongeDataStoreBuilder method getDeserializer.
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> BiFunction<DataView, DataQuery, Optional<T>> getDeserializer(final Type elementType) {
final Class<?> rawType = GenericTypeReflector.erase(elementType);
if (DataView.class.isAssignableFrom(rawType)) {
return (view, dataQuery) -> (Optional<T>) view.getView(dataQuery);
}
if (DataSerializable.class.isAssignableFrom(rawType)) {
return (view, dataQuery) -> (Optional<T>) view.getSerializable(dataQuery, (Class<? extends DataSerializable>) rawType);
}
final Optional<RegistryType<Object>> registryTypeForValue = SpongeDataManager.INSTANCE.findRegistryTypeFor(rawType);
if (registryTypeForValue.isPresent()) {
return (view, dataQuery) -> (Optional<T>) registryTypeForValue.flatMap(regType -> view.getRegistryValue(dataQuery, regType));
}
if (ResourceKey.class.isAssignableFrom(rawType)) {
return (view, dataQuery) -> (Optional<T>) view.getString(dataQuery).map(ResourceKey::resolve);
}
if (Sponge.game().dataManager().translator(rawType).isPresent()) {
return (view, dataQuery) -> (Optional<T>) view.getObject(dataQuery, rawType);
}
if (Set.class.isAssignableFrom(rawType)) {
final Type listType = ((ParameterizedType) elementType).getActualTypeArguments()[0];
return (view, dataQuery) -> (Optional<T>) SpongeDataStoreBuilder.deserializeList((Class<?>) listType, view, dataQuery).map(list -> new HashSet(list));
}
if (List.class.isAssignableFrom(rawType)) {
final Type listType = ((ParameterizedType) elementType).getActualTypeArguments()[0];
return (view, dataQuery) -> (Optional<T>) SpongeDataStoreBuilder.deserializeList((Class<?>) listType, view, dataQuery);
}
if (Collection.class.isAssignableFrom(rawType)) {
throw new UnsupportedOperationException("Collection deserialization is not supported. Provide the deserializer for it.");
}
if (Types.isArray(elementType)) {
final Class arrayType = GenericTypeReflector.erase(GenericTypeReflector.getArrayComponentType(elementType));
return (view, dataQuery) -> (Optional<T>) SpongeDataStoreBuilder.deserializeList((Class<?>) arrayType, view, dataQuery).map(list -> this.listToArray(arrayType, list));
}
if (Map.class.isAssignableFrom(rawType)) {
final Type[] parameterTypes = ((ParameterizedType) elementType).getActualTypeArguments();
final Type keyType = parameterTypes[0];
final Type valueType = parameterTypes[1];
if (!(keyType instanceof Class)) {
throw new UnsupportedOperationException("Unsupported map-key type " + keyType);
}
final Function<DataQuery, Optional<?>> keyDeserializer;
final Optional<RegistryType<Object>> registryTypeForKey = SpongeDataManager.INSTANCE.findRegistryTypeFor((Class) keyType);
if (registryTypeForKey.isPresent()) {
keyDeserializer = key -> registryTypeForKey.flatMap(regType -> Sponge.game().findRegistry(regType)).flatMap(r -> r.findValue(ResourceKey.resolve(key.toString())));
} else if (((Class<?>) keyType).isEnum()) {
keyDeserializer = key -> Optional.ofNullable(Enum.valueOf(((Class<? extends Enum>) keyType), key.toString()));
} else if (keyType == String.class) {
keyDeserializer = key -> Optional.of(key.toString());
} else if (keyType == UUID.class) {
keyDeserializer = key -> Optional.of(UUID.fromString(key.toString()));
} else if (keyType == ResourceKey.class) {
keyDeserializer = key -> Optional.of(ResourceKey.resolve(key.toString()));
} else {
throw new UnsupportedOperationException("Unsupported map-key type " + keyType);
}
final BiFunction<DataView, DataQuery, Optional<Object>> valueDeserializer = this.getDeserializer(valueType);
return (view, dataQuery) -> (Optional<T>) view.getView(dataQuery).map(mapView -> {
final Map<Object, Object> resultMap = new HashMap<>();
for (final DataQuery key : mapView.keys(false)) {
final Object mapKey = keyDeserializer.apply(key).orElseThrow(() -> new UnsupportedOperationException("Key not found " + key + " as " + keyType));
final Optional<?> mapValue = valueDeserializer.apply(mapView, key);
resultMap.put(mapKey, mapValue.get());
}
return resultMap;
});
}
return (view, dataQuery) -> (Optional<T>) view.get(dataQuery);
}
use of org.spongepowered.api.data.persistence.DataQuery in project SpongeCommon by SpongePowered.
the class NBTTranslator method containerToCompound.
private static void containerToCompound(final DataView container, final CompoundTag compound) {
// We don't need to get deep values since all nested DataViews will be found
// from the instance of checks.
checkNotNull(container);
checkNotNull(compound);
for (Map.Entry<DataQuery, Object> entry : container.values(false).entrySet()) {
Object value = entry.getValue();
String key = entry.getKey().asString('.');
if (value instanceof DataView) {
CompoundTag inner = new CompoundTag();
NBTTranslator.containerToCompound(container.getView(entry.getKey()).get(), inner);
compound.put(key, inner);
} else if (value instanceof Boolean) {
compound.put(key + NBTTranslator.BOOLEAN_IDENTIFIER, ByteTag.valueOf((Boolean) value));
} else {
compound.put(key, NBTTranslator.getBaseFromObject(value));
}
}
}
use of org.spongepowered.api.data.persistence.DataQuery in project SpongeCommon by SpongePowered.
the class MapTest method blankMapDecorationIds.
private DataView blankMapDecorationIds(final DataView dataView) {
// Blank changes to id since we always change when serializing them to stop conflicts.
final DataQuery decorations = DataQuery.of("MapData", "Decorations");
final List<DataView> newData = dataView.getViewList(decorations).get();
newData.replaceAll(dataView1 -> dataView1.set(DataQuery.of("id"), "fakeid"));
dataView.set(decorations, newData);
return dataView;
}
Aggregations