@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 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()])); }
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()); }
/** * Gets the value typed using the appropriate type conversion from {@link Types} * * @param def The default value if no appropriate value is set * @see #getValue() * @return The appropriate type conversion, {@code def} if no appropriate value is available */ default String getString(String def) { return getValue(Types::asString, def); }
/** * Get the current value associated with this node. * If this node has children, this method will recursively unwrap them to construct a List or a Map. * This method will also perform deserialization using the appropriate TypeSerializer for the given type, or casting if no type serializer is found. * * @param type The type to deserialize to * @param <T> the type to get * @return the value if present and of the proper type, else null */ default <T> T getValue(TypeToken<T> type) throws ObjectMappingException { return getValue(type, (T) null); }
/** * Gets the value typed using the appropriate type conversion from {@link Types} * * @param def The default value if no appropriate value is set * @see #getValue() * @return The appropriate type conversion, {@code def} if no appropriate value is available */ default double getDouble(double def) { return getValue(Types::asDouble, def); }
/** * Gets the value typed using the appropriate type conversion from {@link Types} * * @param def The default value if no appropriate value is set * @see #getValue() * @return The appropriate type conversion, {@code def} if no appropriate value is available */ default int getInt(int def) { return getValue(Types::asInt, def); }
/** * Gets the appropriately transformed typed version of this node's value from the provided transformation function * * @param transformer The transformation function * @param <T> The expected type * @return A transformed value of the correct type, or null either if no value is present or the value could not * be converted */ default <T> T getValue(Function<Object, T> transformer) { return getValue(transformer, (T) null); }
/** * Gets the value typed using the appropriate type conversion from {@link Types} * * @param def The default value if no appropriate value is set * @see #getValue() * @return The appropriate type conversion, {@code def} if no appropriate value is available */ default float getFloat(float def) { return getValue(Types::asFloat, def); }
/** * Gets the value typed using the appropriate type conversion from {@link Types} * * @param def The default value if no appropriate value is set * @see #getValue() * @return The appropriate type conversion, {@code def} if no appropriate value is available */ default boolean getBoolean(boolean def) { return getValue(Types::asBoolean, def); }
/** * Gets the value typed using the appropriate type conversion from {@link Types} * * @param def The default value if no appropriate value is set * @see #getValue() * @return The appropriate type conversion, {@code def} if no appropriate value is available */ default double getDouble(double def) { return getValue(Types::asDouble, def); }
/** * Gets the value typed using the appropriate type conversion from {@link Types} * * @param def The default value if no appropriate value is set * @see #getValue() * @return The appropriate type conversion, {@code def} if no appropriate value is available */ default boolean getBoolean(boolean def) { return getValue(Types::asBoolean, def); }
/** * Gets the value typed using the appropriate type conversion from {@link Types} * * @param def The default value if no appropriate value is set * @see #getValue() * @return The appropriate type conversion, {@code def} if no appropriate value is available */ default float getFloat(float def) { return getValue(Types::asFloat, def); }
/** * Gets the value typed using the appropriate type conversion from {@link Types} * * @param def The default value if no appropriate value is set * @see #getValue() * @return The appropriate type conversion, {@code def} if no appropriate value is available */ default double getDouble(double def) { return getValue(Types::asDouble, def); }
/** * Gets the value typed using the appropriate type conversion from {@link Types} * * @param def The default value if no appropriate value is set * @see #getValue() * @return The appropriate type conversion, {@code def} if no appropriate value is available */ default String getString(@Nullable String def) { return getValue(Types::asString, def); }
@Override public CompletableFuture<ContextInheritance> getContextInheritanceInternal() { try { return completedFuture(this.permissionsConfig.getValue(TypeToken.of(MemoryContextInheritance.class))); } catch (ObjectMappingException e) { return Util.failedFuture(e); } }
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 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(); }
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()); }
private void testRoundtripValue(Path tempDir, Object value) throws IOException { final Path tempFile = tempDir.resolve("text2.txt"); ConfigurationLoader<ConfigurationNode> loader = JSONConfigurationLoader.builder().setPath(tempFile).build(); ConfigurationNode start = loader.createEmptyNode(); start.getNode("value").setValue(value); loader.save(start); ConfigurationNode ret = loader.load(); assertEquals(value, ret.getNode("value").getValue()); }