@Override public void serialize(TypeToken<?> type, Text obj, ConfigurationNode value) throws ObjectMappingException { String json = (String) obj.toContainer().get(Queries.JSON).get(); GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder() .setSource(() -> new BufferedReader(new StringReader(json))) .build(); try { value.setValue(gsonLoader.load()); } catch (IOException e) { throw new ObjectMappingException(e); } }
@Override public Text deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { StringWriter writer = new StringWriter(); GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder() .setIndent(0) .setSink(() -> new BufferedWriter(writer)) .setHeaderMode(HeaderMode.NONE) .build(); try { gsonLoader.save(value); } catch (IOException e) { throw new ObjectMappingException(e); } return Sponge.getDataManager().deserialize(Text.class, DataContainer.createNew().set(Queries.JSON, writer.toString())).get(); }
@Override public TextFormat deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { TextColor color = TextColors.NONE; GameRegistry registry = Sponge.getRegistry(); String colorId = value.getNode(NODE_COLOR).getString(); if (colorId != null) { color = registry.getType(TextColor.class, colorId).orElseThrow(() -> new ObjectMappingException("Color not found: " + colorId)); } TextStyle style = TextStyles.NONE; ConfigurationNode styleNode = value.getNode(NODE_STYLE); for (TextStyle.Base component : registry.getAllOf(TextStyle.Base.class)) { if (styleNode.getNode(component.getId().toLowerCase()).getBoolean()) { style = style.and(component); } } return TextFormat.NONE.color(color).style(style); }
@Override @SuppressWarnings("unchecked") public <T> ObjectMapper<T> getMapper(Class<T> type) throws ObjectMappingException { Preconditions.checkNotNull(type, "type"); try { return (ObjectMapper<T>) mapperCache.get(type); } catch (ExecutionException e) { if (e.getCause() instanceof ObjectMappingException) { throw (ObjectMappingException) e.getCause(); } else { throw new ObjectMappingException(e); } } }
@Override public Pattern deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { try { return Pattern.compile(value.getString()); } catch (PatternSyntaxException ex) { throw new ObjectMappingException(ex); } }
@Override public UUID deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { try { return UUID.fromString(value.getString()); } catch (IllegalArgumentException ex) { throw new ObjectMappingException("Value not a UUID", ex); } }
@Override public URI deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String plainUri = value.getString(); if (plainUri == null) { throw new ObjectMappingException("No value present in node " + value); } URI uri; try { uri = new URI(plainUri); } catch (URISyntaxException e) { throw new ObjectMappingException("Invalid URI string provided for " + value.getKey() + ": got " + plainUri); } return uri; }
@Override public URL deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String plainUrl = value.getString(); if (plainUrl == null) { throw new ObjectMappingException("No value present in node " + value); } URL url; try { url = new URL(plainUrl); } catch (MalformedURLException e) { throw new ObjectMappingException("Invalid URL string provided for " + value.getKey() + ": got " + plainUrl); } return url; }
@Override @SuppressWarnings("unchecked") // i continue to hate generics public Enum deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String enumConstant = value.getString(); if (enumConstant == null) { throw new ObjectMappingException("No value present in node " + value); } Optional<Enum> ret = (Optional) EnumLookup.lookupEnum(type.getRawType().asSubclass(Enum.class), enumConstant); // XXX: intellij says this cast is optional but it isnt if (!ret.isPresent()) { throw new ObjectMappingException("Invalid enum constant provided for " + value.getKey() + ": " + "Expected a value of enum " + type + ", got " + enumConstant); } return ret.get(); }
@Override public URI deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { String plainUri = value.getString(); if (plainUri == null) { throw new ObjectMappingException("No value present in node " + value); } URI uri; try { uri = new URI(plainUri); } catch (URISyntaxException e) { throw new ObjectMappingException("Invalid URI string provided for " + value.getKey() + ": got " + plainUri); } return uri; }
@Override public URI deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { String plainUri = value.getString(); if (plainUri == null) { throw new ObjectMappingException("No value present in node " + value); } URI uri; try { uri = new URI(plainUri); } catch (URISyntaxException e) { throw new ObjectMappingException("Invalid URI string provided for " + value.getKey() + ": got " + plainUri); } return uri; }
@Override public DataStore deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String dataStoreType = value.getNode("type").getString(value.getKey().toString()); Optional<DataStoreFactory> factory = DataStoreFactories.get(dataStoreType); if (!factory.isPresent()) { throw new ObjectMappingException("Unknown DataStore type " + dataStoreType); } try { return factory.get().createDataStore(value.getKey().toString(), value); } catch (PermissionsLoadingException e) { throw new ObjectMappingException(e); } }
@Override public UUID deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { try { return UUID.fromString(value.getString()); } catch (IllegalArgumentException ex) { throw new ObjectMappingException("Value not a UUID", ex); } }
@Override public UUID deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { try { return UUID.fromString(value.getString()); } catch (IllegalArgumentException ex) { throw new ObjectMappingException("Value not a UUID", ex); } }
@Override public Pattern deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { try { return Pattern.compile(value.getString()); } catch (PatternSyntaxException ex) { throw new ObjectMappingException(ex); } }
@Override public Pattern deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { try { return Pattern.compile(value.getString()); } catch (PatternSyntaxException ex) { throw new ObjectMappingException(ex); } }
@Override public void serialize(TypeToken<?> type, DataStore obj, ConfigurationNode value) throws ObjectMappingException { try { value.getNode("type").setValue(obj.serialize(value)); } catch (PermissionsLoadingException e) { throw new ObjectMappingException(e); } } }
@Override public void serialize(TypeToken<?> type, Text obj, ConfigurationNode value) throws ObjectMappingException { String json = (String) obj.toContainer().get(Queries.JSON).get(); GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder() .setSource(() -> new BufferedReader(new StringReader(json))) .build(); try { value.setValue(gsonLoader.load()); } catch (IOException e) { throw new ObjectMappingException(e); } }
@Override public void serialize(TypeToken<?> type, Set<?> obj, ConfigurationNode value) throws ObjectMappingException { TypeToken<?> entryType = type.resolveType(Set.class.getTypeParameters()[0]); TypeSerializer entrySerial = value.getOptions().getSerializers().get(entryType); if (entrySerial == null) { throw new ObjectMappingException("No applicable type serializer for type " + entryType); } value.setValue(ImmutableSet.of()); for (Object ent : obj) { entrySerial.serialize(entryType, ent, value.getAppendedNode()); } } }
@Override public Text deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { StringWriter writer = new StringWriter(); GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder() .setIndent(0) .setSink(() -> new BufferedWriter(writer)) .setHeaderMode(HeaderMode.NONE) .build(); try { gsonLoader.save(value); } catch (IOException e) { throw new ObjectMappingException(e); } return Sponge.getDataManager().deserialize(Text.class, new MemoryDataContainer().set(Queries.JSON, writer.toString())).get(); }