Skip to content

Commit

Permalink
Added comments to the code
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswalpen committed Dec 8, 2021
1 parent d744389 commit f8a15e7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 25 deletions.
29 changes: 29 additions & 0 deletions src/YamlMap/Serialization/SerializerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Linq;

namespace YamlMap.Serialization
{
internal static class SerializerExtensions
{
private static readonly char[] SpecialChars = new[] { ':', '[', ']' };

/// <summary>
/// Convert a object to a string that is used for serialization
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ToSerializeableString(this object value)
{
if (value == null)
{
return null;
}

if (value is string s && SpecialChars.Any(c => s.Contains(c)))
{
value = $"'{s}'";
}

return value.ToString();
}
}
}
21 changes: 19 additions & 2 deletions src/YamlMap/Serialization/TokenDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,37 @@

namespace YamlMap.Serialization
{
/// <summary>
/// Deserializer
/// </summary>
public class TokenDeserializer : ITokenDeserializer
{
//private readonly PropertyMapper _mapper;
private readonly object _item;
private readonly IToken _token;
private readonly Type _type;

/// <summary>
/// Creates a new deserializer
/// </summary>
/// <param name="type"></param>
/// <param name="token"></param>
public TokenDeserializer(Type type, IToken token)
{
//_mapper = new PropertyMapper(type);
_item = type.CreateInstance(token);
_token = token;
_type = type;
}

/// <summary>
/// Gets the instance that is created
/// </summary>
public object Node => _item;

/// <summary>
/// Deserialize a <see cref="IToken"/>
/// </summary>
/// <param name="token"></param>
/// <exception cref="InvalidConfigurationException"></exception>
public void Deserialize(IToken token)
{
var mapper = MapperFactory.GetObjectMapper(Node, _type);
Expand Down Expand Up @@ -54,6 +68,9 @@ public void Deserialize(IToken token)
property.SetValue(Node, child.Node, null);
}

/// <summary>
/// Deserialize all child tokens
/// </summary>
public void DeserializeChildren()
{
// refactor the line to be parsed as property
Expand Down
31 changes: 9 additions & 22 deletions src/YamlMap/Serialization/TokenSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@

namespace YamlMap.Serialization
{
/// <summary>
/// Serializer that creates tokens of objects
/// </summary>
public class TokenSerializer
{


/// <summary>
/// Serialize the item to a string
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="item"></param>
/// <returns></returns>
public string Serialize<T>(T item)
{
var sb = new StringBuilder();
Expand Down Expand Up @@ -87,24 +94,4 @@ private void SerializeNode<T>(T item, StringBuilder sb, int indentation)
}
}
}

internal static class SerializerExtensions
{
private static char[] SpecialChars = new[] { ':', '[', ']' };

public static string ToSerializeableString(this object value)
{
if (value == null)
{
return null;
}

if (value is string s && SpecialChars.Any(c => s.Contains(c)))
{
value = $"'{s}'";
}

return value.ToString();
}
}
}
17 changes: 16 additions & 1 deletion src/YamlMap/Serialization/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@

namespace YamlMap.Serialization
{
public class TypeConverter
/// <summary>
/// Converter that converts a string value to a defined type
/// </summary>
public static class TypeConverter
{
/// <summary>
/// Convert the string to a given type
/// </summary>
/// <param name="type"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="FormatException"></exception>
public static object Convert(Type type, string value)
{
if (type == typeof(string))
Expand Down Expand Up @@ -115,6 +125,11 @@ public static object Convert(Type type, string value)
return null;
}

/// <summary>
/// Parser a string to a boolean
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool ParseBoolean(object value)
{
if (value == null || value == DBNull.Value)
Expand Down

0 comments on commit f8a15e7

Please sign in to comment.