Java Code Examples for org.simpleframework.xml.Serializer#write()
The following examples show how to use
org.simpleframework.xml.Serializer#write() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnionEmptyListBugTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testListBug() throws Exception { Serializer serializer = new Persister(); ElementListUnionBug element = new ElementListUnionBug(); StringWriter writer = new StringWriter(); serializer.write(element, writer); String text = writer.toString(); assertElementExists(text, "/elementListUnionBug"); assertElementDoesNotExist(text, "/elementListUnionBug/string"); assertElementDoesNotExist(text, "/elementListUnionBug/integer"); writer = new StringWriter(); element.values.add("A"); element.values.add(111); serializer.write(element, writer); text = writer.toString(); System.out.println(text); assertElementExists(text, "/elementListUnionBug/string"); assertElementHasValue(text, "/elementListUnionBug/string", "A"); assertElementExists(text, "/elementListUnionBug/integer"); assertElementHasValue(text, "/elementListUnionBug/integer", "111"); }
Example 2
Source File: ProviderInformationTest.java From simplexml with Apache License 2.0 | 6 votes |
public String toString() { Serializer serializer = new Persister(); StringWriter xmlWriter = new StringWriter(); try { serializer.write( itsSolutionPackageDeployment, xmlWriter ); } catch (Exception exception) { final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); exception.printStackTrace(printWriter); System.out.println( "serializer.write exception " + exception.getMessage() ); System.out.println( result.toString() ); xmlWriter.append( "serializer.write exception " + exception.getMessage() ); } return xmlWriter.toString(); }
Example 3
Source File: OrderTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testSerializationOrder() throws Exception { Serializer serializer = new Persister(); OrderExample example = new OrderExample("first", "second", "third", "fourth", 1, 2, 3.0); StringWriter writer = new StringWriter(); serializer.write(example, writer); validate(example, serializer); String text = writer.toString(); assertTrue(text.indexOf("first") < text.indexOf("second")); assertTrue(text.indexOf("second") < text.indexOf("third")); assertTrue(text.indexOf("third") < text.indexOf("fourth")); assertTrue(text.indexOf("one") < text.indexOf("two")); assertTrue(text.indexOf("two") < text.indexOf("three")); example = new OrderExample("1st", "2nd", "3rd", "4th", 10, 20, 30.0); validate(example, serializer); }
Example 4
Source File: UARTActivity.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Saves the given configuration in the database. */ private void saveConfiguration() { final UartConfiguration configuration = this.configuration; try { final Format format = new Format(new HyphenStyle()); final Strategy strategy = new VisitorStrategy(new CommentVisitor()); final Serializer serializer = new Persister(strategy, format); final StringWriter writer = new StringWriter(); serializer.write(configuration, writer); final String xml = writer.toString(); databaseHelper.updateConfiguration(configuration.getName(), xml); wearableSynchronizer.onConfigurationAddedOrEdited(preferences.getLong(PREFS_CONFIGURATION, 0), configuration); } catch (final Exception e) { Log.e(TAG, "Error while creating a new configuration", e); } }
Example 5
Source File: HideEnclosingConverterTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testWrapper() throws Exception{ Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy); Entry entry = new Entry("name", "value"); EntryHolder holder = new EntryHolder(entry, "test", 10); StringWriter writer = new StringWriter(); serializer.write(holder, writer); System.out.println(writer.toString()); serializer.read(EntryHolder.class, writer.toString()); System.err.println(writer.toString()); String sourceXml = writer.toString(); assertElementExists(sourceXml, "/entryHolder"); assertElementHasAttribute(sourceXml, "/entryHolder", "code", "10"); assertElementExists(sourceXml, "/entryHolder/entry"); assertElementExists(sourceXml, "/entryHolder/entry/name"); assertElementHasValue(sourceXml, "/entryHolder/entry/name", "name"); assertElementExists(sourceXml, "/entryHolder/entry/value"); assertElementHasValue(sourceXml, "/entryHolder/entry/value", "value"); assertElementExists(sourceXml, "/entryHolder/name"); assertElementHasValue(sourceXml, "/entryHolder/name", "test"); }
Example 6
Source File: Test2Test.java From simplexml with Apache License 2.0 | 6 votes |
public void testException() throws Exception{ boolean failure = false; try { Serializer s = new Persister(); StringWriter sw = new StringWriter(); s.write(new Test2(new MyElementA(), new MyElementB()), sw); String serializedForm = sw.toString(); System.out.println(serializedForm); System.out.println(); Test2 o = s.read(Test2.class, serializedForm); sw.getBuffer().setLength(0); s.write(o, sw); System.out.println(sw.toString()); System.out.println(); }catch(Exception e) { e.printStackTrace(); failure = true; } assertTrue("Annotations are invalid as they use the same name", failure); }
Example 7
Source File: EmptyMapEntryTest.java From simplexml with Apache License 2.0 | 6 votes |
/** * @param args the command line arguments */ public void testEmptyMapEntry() throws Exception { Strategy resolver = new CycleStrategy("id", "ref"); Serializer s = new Persister(resolver); StringWriter w = new StringWriter(); SimpleBug1 bug1 = new SimpleBug1(); assertEquals(bug1.test1.data.get("key1"), "value1"); assertEquals(bug1.test1.data.get("key2"), "value1"); assertEquals(bug1.test1.data.get("key3"), ""); assertEquals(bug1.test1.data.get(""), ""); s.write(bug1, w); System.err.println(w.toString()); SimpleBug1 bug2 = s.read(SimpleBug1.class, w.toString()); assertEquals(bug1.test1.data.get("key1"), bug2.test1.data.get("key1")); assertEquals(bug1.test1.data.get("key2"), bug2.test1.data.get("key2")); assertEquals(bug2.test1.data.get("key1"), "value1"); assertEquals(bug2.test1.data.get("key2"), "value1"); assertNull(bug2.test1.data.get("key3")); assertNull(bug2.test1.data.get(null)); validate(s, bug1); }
Example 8
Source File: UARTActivity.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void onRenameConfiguration(final String newName) { final boolean exists = databaseHelper.configurationExists(newName); if (exists) { Toast.makeText(this, R.string.uart_configuration_name_already_taken, Toast.LENGTH_LONG).show(); return; } final String oldName = configuration.getName(); configuration.setName(newName); try { final Format format = new Format(new HyphenStyle()); final Strategy strategy = new VisitorStrategy(new CommentVisitor()); final Serializer serializer = new Persister(strategy, format); final StringWriter writer = new StringWriter(); serializer.write(configuration, writer); final String xml = writer.toString(); databaseHelper.renameConfiguration(oldName, newName, xml); wearableSynchronizer.onConfigurationAddedOrEdited(preferences.getLong(PREFS_CONFIGURATION, 0), configuration); refreshConfigurations(); } catch (final Exception e) { Log.e(TAG, "Error while renaming configuration", e); } }
Example 9
Source File: UARTActivity.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void onNewConfiguration(final String name, final boolean duplicate) { final boolean exists = databaseHelper.configurationExists(name); if (exists) { Toast.makeText(this, R.string.uart_configuration_name_already_taken, Toast.LENGTH_LONG).show(); return; } UartConfiguration configuration = this.configuration; if (!duplicate) configuration = new UartConfiguration(); configuration.setName(name); try { final Format format = new Format(new HyphenStyle()); final Strategy strategy = new VisitorStrategy(new CommentVisitor()); final Serializer serializer = new Persister(strategy, format); final StringWriter writer = new StringWriter(); serializer.write(configuration, writer); final String xml = writer.toString(); final long id = databaseHelper.addConfiguration(name, xml); wearableSynchronizer.onConfigurationAddedOrEdited(id, configuration); refreshConfigurations(); selectConfiguration(configurationsAdapter.getItemPosition(id)); } catch (final Exception e) { Log.e(TAG, "Error while creating a new configuration", e); } }
Example 10
Source File: DataTagAddressTest.java From c2mon with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void deserializeAndSerializeWithSimpleXml() { try { //setup Data HashMap<String, String> map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); DataTagAddress address = new DataTagAddress(map); address.setTimeToLive(100); Serializer serializer = new Persister(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // serialization serializer.write(address, baos); String content = new String(baos.toByteArray(), "UTF-8"); DataTagAddress address2 = serializer.read(DataTagAddress.class, content); assertEquals(address2, address); } catch (Exception e) { e.printStackTrace(); } }
Example 11
Source File: Test1Test.java From simplexml with Apache License 2.0 | 5 votes |
public void testParameterMismatch() throws Exception{ Serializer s = new Persister(); StringWriter sw = new StringWriter(); s.write(new Test1("a", "b"), sw); String serializedForm = sw.toString(); System.out.println(serializedForm); Test1 o = s.read(Test1.class, serializedForm); }
Example 12
Source File: Test6Test.java From simplexml with Apache License 2.0 | 5 votes |
public void testSerialize() throws Exception{ Serializer s = new Persister(); StringWriter sw = new StringWriter(); s.write(new Test6(new MyElementA(), new MyElementB()), sw); String serializedForm = sw.toString(); System.out.println(serializedForm); System.out.println(); Test6 o = s.read(Test6.class, serializedForm); assertTrue(o.isListConstructorUsed()); sw.getBuffer().setLength(0); s.write(o, sw); System.out.println(sw.toString()); System.out.println(); }
Example 13
Source File: VersionTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testVersion2() throws Exception { Serializer persister = new Persister(); Example example = persister.read(Example2.class, VERSION_2); assertTrue(example instanceof Example2); assertEquals(example.getValue(), "text value"); assertEquals(example.version, 2.0); persister.write(example, System.out); assertEquals(example.getValue(), "text value"); assertEquals(example.version, 2.0); }
Example 14
Source File: MatcherTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testEnumMatcher() throws Exception { Matcher matcher = new ExampleEnumMatcher(); Serializer serializer = new Persister(matcher); ExampleEnum value = new ExampleEnum(MyEnum.A_1); StringWriter writer = new StringWriter(); serializer.write(value, writer); assertElementHasAttribute(writer.toString(), "/exampleEnum", "value", "A-1"); System.out.println(writer.toString()); validate(serializer, value); }
Example 15
Source File: StrategyTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testExampleStrategy() throws Exception { ExampleStrategy strategy = new ExampleStrategy(this); Serializer persister = new Persister(strategy); Example example = persister.read(Example.class, ELEMENT); assertTrue(example instanceof ExampleExample); assertEquals(example.getValue(), "attribute-example-text"); assertEquals(example.getKey(), "attribute-example-key"); persister.write(example, System.err); }
Example 16
Source File: ContactEntryTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testContact() throws Exception { Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy); EntryList list = new EntryList("Other", "Value"); StringWriter writer = new StringWriter(); list.getList().add(new Entry("a", "A")); list.getList().add(new Entry("b", "B")); list.getList().add(new Entry("c", "C")); list.getOtherList().add(new Entry("1", "ONE")); list.getOtherList().add(new Entry("2", "TWO")); list.getOtherList().add(new Entry("3", "THREE")); serializer.write(list, writer); String text = writer.toString(); EntryList copy = serializer.read(EntryList.class, text); assertEquals(copy.getList().get(0).getName(), list.getList().get(0).getName()); assertEquals(copy.getList().get(0).getValue(), list.getList().get(0).getValue()); assertEquals(copy.getList().get(1).getName(), list.getList().get(1).getName()); assertEquals(copy.getList().get(1).getValue(), list.getList().get(1).getValue()); assertEquals(copy.getList().get(2).getName(), list.getList().get(2).getName()); assertEquals(copy.getList().get(2).getValue(), list.getList().get(2).getValue()); assertEquals(copy.getOtherList().get(0).getName(), list.getOtherList().get(0).getName()); assertEquals(copy.getOtherList().get(0).getValue(), list.getOtherList().get(0).getValue()); assertEquals(copy.getOtherList().get(1).getName(), list.getOtherList().get(1).getName()); assertEquals(copy.getOtherList().get(1).getValue(), list.getOtherList().get(1).getValue()); assertEquals(copy.getOtherList().get(2).getName(), list.getOtherList().get(2).getName()); assertEquals(copy.getOtherList().get(2).getValue(), list.getOtherList().get(2).getValue()); System.out.println(text); }
Example 17
Source File: AnnotationStrategyTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testAnnotationStrategy() throws Exception { Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy); StringWriter writer = new StringWriter(); FarmExample example = serializer.read(FarmExample.class, SOURCE); example.getTime().add(10); example.getTime().add(11); example.getTime().add(12); assertEquals(example.getCow().getName(), "Bull"); assertEquals(example.getCow().getAge(), 4); assertEquals(example.getCow().getLegs(), 4); assertEquals(example.getChicken().getName(), "Hen"); assertEquals(example.getChicken().getAge(), 1); assertEquals(example.getChicken().getLegs(), 2); serializer.write(example, System.out); serializer.write(example, writer); String text = writer.toString(); assertElementExists(text, "/farmExample/chicken"); assertElementExists(text, "/farmExample/cow"); assertElementHasNamespace(text, "/farmExample/chicken", "http://www.domain.com/test"); assertElementDoesNotHaveNamespace(text, "/farmExample/cow", "http://www.domain.com/test"); assertElementHasAttribute(text, "/farmExample/chicken", "name", "Hen"); assertElementHasAttribute(text, "/farmExample/chicken", "age", "1"); assertElementHasAttribute(text, "/farmExample/chicken", "legs", "2"); assertElementHasAttribute(text, "/farmExample/cow", "name", "Bull"); assertElementHasAttribute(text, "/farmExample/cow", "age", "4"); assertElementHasAttribute(text, "/farmExample/cow", "legs", "4"); }
Example 18
Source File: MapNullTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testNullValue() throws Exception { Serializer serializer = new Persister(); PrimitiveMap primitiveMap = new PrimitiveMap(); primitiveMap.map.put("a", new BigDecimal(1)); primitiveMap.map.put("b", new BigDecimal(2)); primitiveMap.map.put("c", null); primitiveMap.map.put(null, new BigDecimal(4)); StringWriter out = new StringWriter(); serializer.write(primitiveMap, out); primitiveMap = serializer.read(PrimitiveMap.class, out.toString()); assertEquals(primitiveMap.map.get(null), new BigDecimal(4)); assertEquals(primitiveMap.map.get("c"), null); assertEquals(primitiveMap.map.get("a"), new BigDecimal(1)); assertEquals(primitiveMap.map.get("b"), new BigDecimal(2)); validate(primitiveMap, serializer); ComplexMap complexMap = new ComplexMap(); complexMap.map.put(new CompositeKey("name.1", "address.1"), new MapEntry("1", "1")); complexMap.map.put(new CompositeKey("name.2", "address.2"), new MapEntry("2", "2")); complexMap.map.put(null, new MapEntry("3", "3")); complexMap.map.put(new CompositeKey("name.4", "address.4"), null); validate(complexMap, serializer); ComplexMap emptyNull = serializer.read(ComplexMap.class, EMPTY_AS_NULL); assertEquals(emptyNull.getValue(new CompositeKey("name.1", "address.1")), "1"); assertEquals(emptyNull.getValue(new CompositeKey("name.2", "address.2")), "2"); assertEquals(emptyNull.getValue(null), "3"); assertEquals(emptyNull.getValue(new CompositeKey("name.4", "address.4")), null); validate(emptyNull, serializer); }
Example 19
Source File: UARTActivity.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Converts the old configuration, stored in preferences, into the first XML configuration and saves it to the database. * If there is already any configuration in the database this method does nothing. */ private void ensureFirstConfiguration(@NonNull final DatabaseHelper databaseHelper) { // This method ensures that the "old", single configuration has been saved to the database. if (databaseHelper.getConfigurationsCount() == 0) { final UartConfiguration configuration = new UartConfiguration(); configuration.setName("First configuration"); final Command[] commands = configuration.getCommands(); for (int i = 0; i < 9; ++i) { final String cmd = preferences.getString(PREFS_BUTTON_COMMAND + i, null); if (cmd != null) { final Command command = new Command(); command.setCommand(cmd); command.setActive(preferences.getBoolean(PREFS_BUTTON_ENABLED + i, false)); command.setEol(0); // default one command.setIconIndex(preferences.getInt(PREFS_BUTTON_ICON + i, 0)); commands[i] = command; } } try { final Format format = new Format(new HyphenStyle()); final Strategy strategy = new VisitorStrategy(new CommentVisitor()); final Serializer serializer = new Persister(strategy, format); final StringWriter writer = new StringWriter(); serializer.write(configuration, writer); final String xml = writer.toString(); databaseHelper.addConfiguration(configuration.getName(), xml); } catch (final Exception e) { Log.e(TAG, "Error while creating default configuration", e); } } }
Example 20
Source File: Test4Test.java From simplexml with Apache License 2.0 | 4 votes |
public void testSerialization() throws Exception{ Serializer s = new Persister(); StringWriter sw = new StringWriter(); s.write(new Test4(new MyElement(), new MyElementA(), new MyElementB()), sw); String serializedForm = sw.toString(); System.out.println(serializedForm); System.out.println(); Test4 o = s.read(Test4.class, serializedForm); sw.getBuffer().setLength(0); s.write(o, sw); System.out.println(sw.toString()); System.out.println(); sw.getBuffer().setLength(0); serializedForm = "<test4>\n" + " <single-element class=\"org.simpleframework.xml.core.Test4Test$MyElementC\"/>\n" + " <elements>\n" + " <elementA/>\n" + " <elementB/>\n" + " </elements>\n" + "</test4>"; //FIXME read ignores the class statement System.out.println(serializedForm); System.out.println(); o = s.read(Test4.class, serializedForm); sw.getBuffer().setLength(0); s.write(o, sw); System.out.println(sw.toString()); System.out.println(); sw.getBuffer().setLength(0); serializedForm = "<test4>\n" + " <single-element/>\n" + " <elements>\n" + " <element class=\"org.simpleframework.xml.core.Test4Test$MyElementC\"/>\n" + " <elementB/>\n" + " </elements>\n" + "</test4>"; //FIXME read uses the class statement and deserializes as expected System.out.println(serializedForm); System.out.println(); o = s.read(Test4.class, serializedForm); sw.getBuffer().setLength(0); s.write(o, sw); System.out.println(sw.toString()); System.out.println(); sw.getBuffer().setLength(0); }