@Inject //We will place more than one config there (i.e. H2/SQLite database) Settings(Logger logger, @ConfigDir(sharedRoot = false) Path dataFolder) { this.logger = logger; this.dataFolder = dataFolder; try { configMapper = options.getObjectMapperFactory().getMapper(General.class).bindToNew(); textMapper = options.getObjectMapperFactory().getMapper(TextConfig.class).bindToNew(); } catch (ObjectMappingException objMappingExc) { logger.error("Invalid plugin structure", objMappingExc); } }
@Override public DataStore createDataStore(String identifier, ConfigurationNode config) throws PermissionsLoadingException { try { return mapper.bindToNew().populate(config); } catch (ObjectMappingException e) { throw new PermissionsLoadingException(t("Error while deserializing backend %s", identifier), e); } } }
mapper = ObjectMapper.forClass(configClass).bindToNew(); } catch (ObjectMappingException e) { SneakyThrow.throwException(e);
/** * Loads the given configuration file. */ public static void load(Path path) { System.out.println("Loading config from " + path.toString()); try { Files.createDirectories(path.getParent()); if (Files.notExists(path)) { Files.createFile(path); } loader = HoconConfigurationLoader.builder().setPath(path).build(); configMapper = ObjectMapper.forClass(ConfigBase.class).bindToNew(); node = loader.load(ConfigurationOptions.defaults().setHeader(HEADER)); config = configMapper.populate(node); configMapper.serialize(node); loader.save(node); } catch (Exception e) { System.err.println("Error loading configuration:"); e.printStackTrace(); } }
static FileSubjectData fromNode(ConfigurationNode node) throws ObjectMappingException, PermissionsLoadingException { ImmutableMap.Builder<Set<Entry<String, String>>, DataEntry> map = ImmutableMap.builder(); if (node.hasListChildren()) { for (ConfigurationNode child : node.getChildrenList()) { if (!child.hasMapChildren()) { throw new PermissionsLoadingException(t("Each context section must be of map type! Check that no duplicate nesting has occurred.")); } Set<Entry<String, String>> contexts = contextsFrom(child); DataEntry value = MAPPER.bindToNew().populate(child); map.put(contexts, value); } } return new FileSubjectData(map.build()); }
@Override public Object deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { Class<?> clazz = getInstantiableType(type, value.getNode("__class__").getString()); return value.getOptions().getObjectMapperFactory().getMapper(clazz).bindToNew().populate(value); }
@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 testCreateGuiceObjectMapper() throws ObjectMappingException { Injector injector = Guice.createInjector(new TestModule()); GuiceObjectMapperFactory factory = injector.getInstance(GuiceObjectMapperFactory.class); ObjectMapper<ConfigClass> mapper = factory.getMapper(ConfigClass.class); assertTrue(mapper.canCreateInstances()); assertNotNull(mapper.bindToNew().getInstance()); } }
@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 testNoArglessConstructor() throws ObjectMappingException { Assertions.assertTrue(Assertions.assertThrows(ObjectMappingException.class, () -> { ObjectMapper<NonZeroArgConstructorObject> mapper = ObjectMapper.forClass(NonZeroArgConstructorObject.class); assertFalse(mapper.canCreateInstances()); mapper.bindToNew(); }).getMessage().startsWith("No zero-arg constructor")); }
@Test public void testNullsPreserved() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final TestObject obj = mapper.bindToNew().populate(SimpleConfigurationNode.root()); assertNull(obj.stringVal); }
@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 testKeyFromFieldName() throws ObjectMappingException { final ObjectMapper<FieldNameObject> mapper = ObjectMapper.forClass(FieldNameObject.class); final ConfigurationNode node = SimpleConfigurationNode.root(); node.getNode("loads").setValue(true); FieldNameObject obj = mapper.bindToNew().populate(node); assertTrue(obj.loads); }
@Test public void testCommentsApplied() throws ObjectMappingException { CommentedConfigurationNode node = SimpleCommentedConfigurationNode.root(); ObjectMapper<CommentedObject>.BoundInstance mapper = ObjectMapper.forClass(CommentedObject.class).bindToNew(); CommentedObject obj = mapper.populate(node); obj.color = "fuchsia"; obj.politician = "All of them"; mapper.serialize(node); assertEquals("You look nice today", node.getNode("commented-key").getComment().orElse(null)); assertEquals("fuchsia", node.getNode("commented-key").getString()); assertFalse(node.getNode("no-comment").getComment().isPresent()); }
@Test public void testSuperclassFieldsIncluded() throws ObjectMappingException { final ObjectMapper<TestObjectChild> mapper = ObjectMapper.forClass(TestObjectChild.class); ConfigurationNode node = SimpleConfigurationNode.root(); node.getNode("child-setting").setValue(true); node.getNode("test-key").setValue("Parents get populated too!"); TestObjectChild instance = mapper.bindToNew().populate(node); assertEquals(true, instance.childSetting); assertEquals("Parents get populated too!", instance.stringVal); }
mapper.bind(containingObject).serialize(node); final ContainingObject newContainingObject = mapper.bindToNew().populate(node);