@Override public void serialize(TypeToken<?> type, TextTemplate obj, ConfigurationNode value) throws ObjectMappingException { value.getNode(NODE_OPTIONS, NODE_OPEN_ARG).setValue(obj.getOpenArgString()); value.getNode(NODE_OPTIONS, NODE_CLOSE_ARG).setValue(obj.getCloseArgString()); value.getNode(NODE_ARGS).setValue(TOKEN_ARGS, obj.getArguments()); value.getNode(NODE_CONTENT).setValue(TOKEN_TEXT, obj.toText()); }
@Override public void serialize(TypeToken<?> type, BookView bookView, ConfigurationNode value) throws ObjectMappingException { value.getNode(NODE_AUTHOR).setValue(TOKEN_TEXT, bookView.getAuthor()); value.getNode(NODE_TITLE).setValue(TOKEN_TEXT, bookView.getTitle()); value.getNode(NODE_PAGES).setValue(TOKEN_TEXT_LIST, bookView.getPages()); } }
@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, TextFormat obj, ConfigurationNode value) throws ObjectMappingException { value.getNode(NODE_COLOR).setValue(obj.getColor().getId()); ConfigurationNode styleNode = value.getNode(NODE_STYLE); TextStyle composite = obj.getStyle(); Sponge.getRegistry().getAllOf(TextStyle.Base.class) .forEach(style -> styleNode.getNode(style.getId().toLowerCase()).setValue(composite.contains(style))); }
@Override public void serialize(TypeToken<?> type, Number obj, ConfigurationNode value) { value.setValue(obj); } }
@Override public void serialize(TypeToken<?> type, Enum obj, ConfigurationNode value) throws ObjectMappingException { value.setValue(obj.name()); } }
@Override public void serialize(TypeToken<?> type, SpongeBlockFilter obj, ConfigurationNode value) { value.setValue(obj.getRule()); } }
@Override public CompletableFuture<ContextInheritance> setContextInheritanceInternal(final ContextInheritance inheritance) { final MemoryContextInheritance realInheritance = MemoryContextInheritance.fromExistingContextInheritance(inheritance); try { this.permissionsConfig.setValue(TypeToken.of(MemoryContextInheritance.class), realInheritance); } catch (ObjectMappingException e) { throw new RuntimeException(e); } dirty.set(true); return save().thenApply(none -> realInheritance); }
@Override public CompletableFuture<RankLadder> setRankLadderInternal(String identifier, RankLadder ladder) { ConfigurationNode childNode = getRankLaddersNode().getNode(identifier.toLowerCase()); childNode.setValue(null); if (ladder != null) { for (Map.Entry<String, String> rank : ladder.getRanks()) { childNode.getAppendedNode().setValue(Util.subjectToString(rank)); } } dirty.set(true); return save().thenApply(none -> ladder); }
@Override public void serialize(@NonNull TypeToken<?> type, @Nullable URL obj, @NonNull ConfigurationNode value) throws ObjectMappingException { value.setValue(obj.toString()); } }
@Override public void serialize(@NonNull TypeToken<?> type, @Nullable URL obj, @NonNull ConfigurationNode value) throws ObjectMappingException { value.setValue(obj.toString()); } }
@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(@NonNull TypeToken<?> type, @Nullable Number obj, @NonNull ConfigurationNode value) { value.setValue(obj); } }
@Override public void serialize(@NonNull TypeToken<?> type, @Nullable Boolean obj, @NonNull ConfigurationNode value) { value.setValue(Types.asBoolean(obj)); } }
@Override public void serialize(@NonNull TypeToken<?> type, @Nullable Boolean obj, @NonNull ConfigurationNode value) { value.setValue(Types.asBoolean(obj)); } }
@Override public void onDisable() { super.onDisable(); try { config.getNode("variables").setValue(new TypeToken<Map<String, Map<String, String>>>() {}, variableStore); } catch (ObjectMappingException e) { e.printStackTrace(); } if (variableCommandMapping != null) { Sponge.getCommandManager().removeMapping(variableCommandMapping); } }
@Test public void testGetChildrenMap() { ConfigurationNode root = SimpleConfigurationNode.root(); ConfigurationNode a = root.getNode("a").setValue("one"); ConfigurationNode b = root.getNode("b").setValue("two"); assertEquals(ImmutableMap.<Object, ConfigurationNode>of("a", a, "b", b), root.getChildrenMap()); }
@Test public void testSettingNullRemoves() { ConfigurationNode root = SimpleConfigurationNode.root(); ConfigurationNode child = root.getNode("child").setValue("a"); assertFalse(child.isVirtual()); assertSame(child, root.getNode("child")); child.setValue(null); assertTrue(child.isVirtual()); assertNotSame(child, root.getNode("child")); }
@Test public void testCreateFromNode() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final ConfigurationNode source = SimpleConfigurationNode.root(); source.getNode("test-key").setValue("some are born great, some achieve greatness, and some have greatness thrust upon them"); final TestObject obj = mapper.bindToNew().populate(source); assertEquals("some are born great, some achieve greatness, and some have greatness thrust upon them", obj.stringVal); }
@Test public void testLoadExistingObject() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final ConfigurationNode source = SimpleConfigurationNode.root(); final TestObject instance = new TestObject(); source.getNode("test-key").setValue("boom"); mapper.bind(instance).populate(source); assertEquals("boom", instance.stringVal); }