Skip to content

Commit

Permalink
[Serialization] Code arrange & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
zapadi committed Jan 9, 2024
1 parent d6e24a7 commit 8eda132
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
44 changes: 44 additions & 0 deletions src/redmine-net-api/Serialization/IRedmineSerializer.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Serialization interface that supports serialize and deserialize methods.
/// </summary>
internal interface IRedmineSerializer
{
/// <summary>
/// Gets the application format this serializer supports (e.g. "json", "xml").
/// </summary>
string Format { get; }

/// <summary>
/// Serializes the specified object into a string.
/// </summary>
string Serialize<T>(T obj) where T : class;

/// <summary>
/// Deserializes the string into a PageResult of T object.
/// </summary>
PagedResults<T> DeserializeToPagedResults<T>(string response) where T : class, new();

/// <summary>
/// Deserializes the string into an object.
/// </summary>
T Deserialize<T>(string input) where T : new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>(T entity) where T : class
{
Expand All @@ -155,7 +155,7 @@ public string Serialize<T>(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);
Expand Down
19 changes: 8 additions & 11 deletions src/redmine-net-api/Serialization/Xml/XmlRedmineSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(string response) where T : new()
{
Expand All @@ -53,7 +50,7 @@ public XmlRedmineSerializer(XmlWriterSettings xmlWriterSettings)
throw new RedmineException(ex.GetBaseException().Message, ex);
}
}

public PagedResults<T> DeserializeToPagedResults<T>(string response) where T : class, new()
{
try
Expand Down Expand Up @@ -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>(T entity) where T : class
{
Expand Down Expand Up @@ -153,7 +150,7 @@ private string ToXML<T>(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));

Expand Down

0 comments on commit 8eda132

Please sign in to comment.