private boolean isArgDefined(String argName) { return !this.root.getNode(NODE_ARGS, argName).isVirtual(); } }
@Override public TextTemplate deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { this.root = value; this.openArg = value.getNode(NODE_OPEN_ARG).getString(TextTemplate.DEFAULT_OPEN_ARG); this.closeArg = value.getNode(NODE_CLOSE_ARG).getString(TextTemplate.DEFAULT_CLOSE_ARG); Text content = value.getNode(NODE_CONTENT).getValue(TypeToken.of(Text.class)); List<Object> elements = new ArrayList<>(); parse(content, elements); return TextTemplate.of(elements.toArray(new Object[elements.size()])); }
@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, 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, 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 BookView deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { BookView.Builder builder = BookView.builder(); builder.author(value.getNode(NODE_AUTHOR).getValue(TOKEN_TEXT)); builder.title(value.getNode(NODE_TITLE).getValue(TOKEN_TEXT)); builder.addPages(value.getNode(NODE_PAGES).getValue(TOKEN_TEXT_LIST)); return builder.build(); }
@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); }
private void parseArg(LiteralText source, List<Object> into) throws ObjectMappingException { String name = unwrap(source.getContent()); boolean optional = this.root.getNode(NODE_ARGS, name, NODE_OPT).getBoolean(); Text defaultValue = this.root.getNode(NODE_ARGS, name, NODE_DEF_VAL).getValue(TypeToken.of(Text.class)); TextFormat format = source.getFormat(); into.add(TextTemplate.arg(name).format(format).optional(optional).defaultValue(defaultValue).build()); }
@Override public boolean isReplaceParticles() { return rootNode.getNode("replace-particles").getBoolean(false); } });
private static Set<Entry<String, String>> contextsFrom(ConfigurationNode node) { Set<Entry<String, String>> contexts = Collections.emptySet(); ConfigurationNode contextsNode = node.getNode(KEY_CONTEXTS); if (contextsNode.hasMapChildren()) { contexts = ImmutableSet.copyOf(Collections2.transform(contextsNode.getChildrenMap().entrySet(), ent -> { return Maps.immutableEntry(ent.getKey().toString(), String.valueOf(ent.getValue().getValue())); })); } return contexts; }
@Override @SuppressWarnings("unchecked") public Set<String> getAllIdentifiers(String type) { return (Set) getSubjectsNode().getNode(type).getChildrenMap().keySet(); }
@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 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 CompletableFuture<ImmutableSubjectData> getDataInternal(String type, String identifier) { try { return completedFuture(FileSubjectData.fromNode(getSubjectsNode().getNode(type, identifier))); } catch (PermissionsLoadingException e) { return Util.failedFuture(e); } catch (ObjectMappingException e) { return Util.failedFuture(new PermissionsLoadingException(t("While deserializing subject data for %s:", identifier), e)); } }
@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")); }
@Override public Object deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { Class<?> clazz = getInstantiableType(type, value.getNode("__class__").getString()); return value.getOptions().getObjectMapperFactory().getMapper(clazz).bindToNew().populate(value); }
@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); }