From 03c95ce192b8932d96a58c771514402658c1e335 Mon Sep 17 00:00:00 2001 From: Frederik Carlier Date: Tue, 24 May 2016 23:33:34 +0200 Subject: [PATCH] Minor fixes in how property lists are serialized to XML, to maintain compatibility with the Apple format: - NSNumber of "0/real" is serialized as "0.0" - Data values are not indented - Newline at the end of the file - Always use \r as newline --- plist-cil.test/IssueTest.cs | 10 +++ plist-cil.test/plist-cil.test.csproj | 3 + plist-cil.test/test-files/Roundtrip.plist | 80 +++++++++++++++++++++++ plist-cil/NSData.cs | 2 +- plist-cil/NSNumber.cs | 14 +++- plist-cil/NSObject.cs | 7 +- 6 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 plist-cil.test/test-files/Roundtrip.plist diff --git a/plist-cil.test/IssueTest.cs b/plist-cil.test/IssueTest.cs index 3ca6d4f..2bbf1e0 100644 --- a/plist-cil.test/IssueTest.cs +++ b/plist-cil.test/IssueTest.cs @@ -138,6 +138,16 @@ namespace plistcil.test Assert.IsInstanceOf(weight); Assert.AreEqual(10d, (double)weight); } + + [Test] + public static void RoundtripTest() + { + var expected = File.ReadAllText(@"test-files\Roundtrip.plist"); + var value = XmlPropertyListParser.Parse(new FileInfo(@"test-files\Roundtrip.plist")); + var actual = value.ToXmlPropertyList(); + + Assert.AreEqual(expected, actual); + } } } diff --git a/plist-cil.test/plist-cil.test.csproj b/plist-cil.test/plist-cil.test.csproj index 52b15f1..86ebcd3 100644 --- a/plist-cil.test/plist-cil.test.csproj +++ b/plist-cil.test/plist-cil.test.csproj @@ -118,6 +118,9 @@ PreserveNewest + + PreserveNewest + diff --git a/plist-cil.test/test-files/Roundtrip.plist b/plist-cil.test/test-files/Roundtrip.plist new file mode 100644 index 0000000..132aa3a --- /dev/null +++ b/plist-cil.test/test-files/Roundtrip.plist @@ -0,0 +1,80 @@ + + + + + files + + PkgInfo + + n57qDP4tZfLD1rCS43W0B4LQjzE= + + icon.png + + EUOeOW/HpmiAZeEGzJm8j3hE6vo= + + + files2 + + PkgInfo + + n57qDP4tZfLD1rCS43W0B4LQjzE= + + icon.png + + EUOeOW/HpmiAZeEGzJm8j3hE6vo= + + + rules + + .* + + Info.plist + + omit + + weight + 10 + + ResourceRules.plist + + omit + + weight + 100 + + + rules2 + + .* + + Info.plist + + omit + + weight + 10 + + ResourceRules.plist + + omit + + weight + 100 + + ^(Frameworks|SharedFrameworks|Plugins|Plug-ins|XPCServices|Helpers|MacOS)/ + + nested + + weight + 0.0 + + ^[^/]+$ + + top + + weight + 0.0 + + + + diff --git a/plist-cil/NSData.cs b/plist-cil/NSData.cs index 9a4020b..c1c8e06 100644 --- a/plist-cil/NSData.cs +++ b/plist-cil/NSData.cs @@ -156,7 +156,7 @@ namespace Claunia.PropertyList string base64 = GetBase64EncodedData(); foreach (string line in base64.Split('\n')) { - Indent(xml, level + 1); + Indent(xml, level); xml.Append(line); xml.Append(NSObject.NEWLINE); } diff --git a/plist-cil/NSNumber.cs b/plist-cil/NSNumber.cs index 8809d0f..47d601e 100644 --- a/plist-cil/NSNumber.cs +++ b/plist-cil/NSNumber.cs @@ -354,7 +354,19 @@ namespace Claunia.PropertyList case REAL: { xml.Append(""); - xml.Append(ToDouble().ToString(CultureInfo.InvariantCulture)); + + if (doubleValue == 0) + { + // 0 values appear to always roundtrip as 0.0, + // but non-zero values do not include decimals if + // not required (e.g. 10 -> "10") + xml.Append("0.0"); + } + else + { + xml.Append(ToDouble().ToString(CultureInfo.InvariantCulture)); + } + xml.Append(""); break; } diff --git a/plist-cil/NSObject.cs b/plist-cil/NSObject.cs index 515ad69..a602db7 100644 --- a/plist-cil/NSObject.cs +++ b/plist-cil/NSObject.cs @@ -44,10 +44,10 @@ namespace Claunia.PropertyList { /// /// The newline character used for generating the XML output. - /// This constant will be different depending on the operating system on - /// which you use this library. + /// To maintain compatibility with the Apple format, only a newline character + /// is used (as opposed to cr+lf which is normally used on Windows). /// - readonly internal static string NEWLINE = Environment.NewLine; + readonly internal static string NEWLINE = "\n"; /// @@ -100,6 +100,7 @@ namespace Claunia.PropertyList ToXml(xml, 0); xml.Append(NSObject.NEWLINE); xml.Append(""); + xml.Append(NSObject.NEWLINE); return xml.ToString(); }