diff --git a/src/redmine-net-api/Serialization/IRedmineSerializer.cs b/src/redmine-net-api/Serialization/IRedmineSerializer.cs new file mode 100644 index 00000000..1c2e5eec --- /dev/null +++ b/src/redmine-net-api/Serialization/IRedmineSerializer.cs @@ -0,0 +1,44 @@ +/* + Copyright 2011 - 2023 Adrian Popescu + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +namespace Redmine.Net.Api.Serialization +{ + /// + /// Serialization interface that supports serialize and deserialize methods. + /// + internal interface IRedmineSerializer + { + /// + /// Gets the application format this serializer supports (e.g. "json", "xml"). + /// + string Format { get; } + + /// + /// Serializes the specified object into a string. + /// + string Serialize(T obj) where T : class; + + /// + /// Deserializes the string into a PageResult of T object. + /// + PagedResults DeserializeToPagedResults(string response) where T : class, new(); + + /// + /// Deserializes the string into an object. + /// + T Deserialize(string input) where T : new(); + } +} \ No newline at end of file diff --git a/src/redmine-net-api/Serialization/Json/JsonRedmineSerializer.cs b/src/redmine-net-api/Serialization/Json/JsonRedmineSerializer.cs index 43cf4e8a..42807e3f 100644 --- a/src/redmine-net-api/Serialization/Json/JsonRedmineSerializer.cs +++ b/src/redmine-net-api/Serialization/Json/JsonRedmineSerializer.cs @@ -135,7 +135,7 @@ internal sealed class JsonRedmineSerializer : IRedmineSerializer } #pragma warning restore CA1822 - public string Type { get; } = "json"; + public string Format { get; } = "json"; public string Serialize(T entity) where T : class { @@ -155,7 +155,7 @@ public string Serialize(T entity) where T : class { using (var writer = new JsonTextWriter(sw)) { - writer.Formatting = Newtonsoft.Json.Formatting.Indented; + writer.Formatting = Formatting.Indented; writer.DateFormatHandling = DateFormatHandling.IsoDateFormat; jsonSerializable.WriteJson(writer); diff --git a/src/redmine-net-api/Serialization/Xml/XmlRedmineSerializer.cs b/src/redmine-net-api/Serialization/Xml/XmlRedmineSerializer.cs index be1764a0..179eaec0 100644 --- a/src/redmine-net-api/Serialization/Xml/XmlRedmineSerializer.cs +++ b/src/redmine-net-api/Serialization/Xml/XmlRedmineSerializer.cs @@ -27,20 +27,17 @@ namespace Redmine.Net.Api.Serialization internal sealed class XmlRedmineSerializer : IRedmineSerializer { - public XmlRedmineSerializer() + public XmlRedmineSerializer(): this(new XmlWriterSettings { - xmlWriterSettings = new XmlWriterSettings - { - OmitXmlDeclaration = true - }; - } + OmitXmlDeclaration = true + }) { } public XmlRedmineSerializer(XmlWriterSettings xmlWriterSettings) { - this.xmlWriterSettings = xmlWriterSettings; + this._xmlWriterSettings = xmlWriterSettings; } - private readonly XmlWriterSettings xmlWriterSettings; + private readonly XmlWriterSettings _xmlWriterSettings; public T Deserialize(string response) where T : new() { @@ -53,7 +50,7 @@ public XmlRedmineSerializer(XmlWriterSettings xmlWriterSettings) throw new RedmineException(ex.GetBaseException().Message, ex); } } - + public PagedResults DeserializeToPagedResults(string response) where T : class, new() { try @@ -81,7 +78,7 @@ public XmlRedmineSerializer(XmlWriterSettings xmlWriterSettings) } #pragma warning restore CA1822 - public string Type { get; } = "xml"; + public string Format => RedmineConstants.XML; public string Serialize(T entity) where T : class { @@ -153,7 +150,7 @@ private string ToXML(T entity) where T : class using (var stringWriter = new StringWriter()) { - using (var xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings)) + using (var xmlWriter = XmlWriter.Create(stringWriter, _xmlWriterSettings)) { var serializer = new XmlSerializer(typeof(T));