@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 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); } }
private ConfigurationLoader<? extends ConfigurationNode> createLoader(Path file) { return GsonConfigurationLoader.builder() .setPath(file) .setIndent(4) .setLenient(true) .build(); }
@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(); }
@Test public void testLoadingEmptyFile(@TempDirectory.TempDir Path tempDir) throws IOException { final File tempFile = tempDir.resolve("text3.txt").toFile(); tempFile.createNewFile(); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder() .setFile(tempFile) .build(); loader.load(); }
@Test public void testSavingEmptyFile(@TempDirectory.TempDir Path tempDir) throws IOException { final File tempFile = tempDir.resolve("text2.txt").toFile(); tempFile.createNewFile(); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder() .setFile(tempFile) .build(); ConfigurationNode n = SimpleConfigurationNode.root(); loader.save(n); }
@Test public void testLoadingFileWithEmptyObject(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/emptyObject.json"); final Path tempFile = tempDir.resolve("text4.txt"); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream()))) .setSink(AtomicFiles.createAtomicWriterFactory(tempFile, UTF_8)).setLenient(true).build(); ConfigurationNode node = loader.load(loader.getDefaultOptions().setMapFactory(MapFactories.sortedNatural())); assertNull(node.getValue()); assertFalse(node.hasMapChildren()); }
@Test @Disabled("Gson currently makes it rather difficult to get the correct number type") public void testRoundtrippingLong(@TempDirectory.TempDir Path tempDir) throws IOException { final Path tempFile = tempDir.resolve("text5.txt"); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder().setPath(tempFile).build(); ConfigurationNode start = loader.createEmptyNode(); start.getNode("long-num").setValue(TEST_LONG_VAL); loader.save(start); System.out.println(Files.readAllLines(tempFile)); ConfigurationNode ret = loader.load(); System.out.println(ret.getNode("long-num").getValue().getClass()); assertEquals(TEST_LONG_VAL, ret.getNode("long-num").getValue()); } }
@Test public void testSimpleLoading(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example.json"); final Path tempFile = tempDir.resolve("text1.txt"); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream()))) .setSink(AtomicFiles.createAtomicWriterFactory(tempFile, UTF_8)).setLenient(true).build(); ConfigurationNode node = loader.load(loader.getDefaultOptions().setMapFactory(MapFactories.sortedNatural())); assertEquals("unicorn", node.getNode("test", "op-level").getValue()); assertEquals("dragon", node.getNode("other", "op-level").getValue()); assertEquals("dog park", node.getNode("other", "location").getValue()); assertTrue(node.getNode("int-val").getValue() instanceof Integer); assertTrue(node.getNode("double-val").getValue() instanceof Double); loader.save(node); assertEquals(Resources.readLines(url, UTF_8), Files.readAllLines(tempFile, UTF_8)); }