Skip to content

Commit

Permalink
Refactoring out statics
Browse files Browse the repository at this point in the history
  • Loading branch information
Dale Reidy committed Jan 12, 2018
1 parent 18295d7 commit 5c88b13
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Hatchet/HatchetConvert.Serialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static string Serialize(object input, SerializeOptions serializeOptions)
var prettyPrinter = new PrettyPrinter(stringBuilder);
var serializer = new Serializer(prettyPrinter, stringBuilder, serializeOptions);

Serializer.Serialize(input, serializer);
serializer.StaticSerialize(input);
return stringBuilder.ToString();
}

Expand Down
57 changes: 27 additions & 30 deletions src/Hatchet/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ public Serializer(
_metObjects = new List<object>();
}

internal static void Serialize(
object input,
Serializer serializer,
bool forceClassName = false)
internal void StaticSerialize(object input, bool forceClassName = false)
{
serializer.PushObjectRef(input);
PushObjectRef(input);

var context = new SerializationContext(input, serializer, forceClassName);
var context = new SerializationContext(input, this, forceClassName);

var prettyPrinter = context.Serializer.PrettyPrinter;

Expand Down Expand Up @@ -79,7 +76,7 @@ internal static void Serialize(
throw new HatchetException($"Could not serialize {input} of type {input.GetType()}");
}

serializer.PopObjectRef(input);
PopObjectRef(input);
}

private static bool IsSimpleValue(Type inputType)
Expand All @@ -90,7 +87,7 @@ private static bool IsSimpleValue(Type inputType)
|| inputType == typeof(Guid);
}

private static void SerializeClassOrStruct(object input, SerializationContext context)
private void SerializeClassOrStruct(object input, SerializationContext context)
{
var prettyPrinter = context.Serializer;

Expand All @@ -116,19 +113,19 @@ private static void SerializeClassOrStruct(object input, SerializationContext co
prettyPrinter.AppendCloseBlock();
}

private static void SerializeFieldsAndProperties(SerializationContext context)
private void SerializeFieldsAndProperties(SerializationContext context)
{
var propertiesAndFields = GetPropertiesAndFields(context.Input);

foreach (var member in propertiesAndFields)
{
SerializeMember(context.Serializer, member);
SerializeMember(member);
}
}

private static void SerializeMember(Serializer serializer, ISerializableMember member)
private void SerializeMember(ISerializableMember member)
{
SerializeKeyValue(serializer, member.Name, member.Value, member.IsValueAbstract);
SerializeKeyValue(member.Name, member.Value, member.IsValueAbstract);
}

private static void WriteClassName(Serializer serializer, Type inputType)
Expand All @@ -139,7 +136,7 @@ private static void WriteClassName(Serializer serializer, Type inputType)
serializer.Append(LineEnding);
}

private static void SerializeKeyValue(Serializer serializer, string key,
private void SerializeKeyValue(string key,
object value, bool forceClassName = false)
{
if (value == null)
Expand All @@ -156,23 +153,23 @@ private static void SerializeKeyValue(Serializer serializer, string key,
if (type.IsValueType)
{
var comparable = Activator.CreateInstance(type);
if (value.Equals(comparable) && !serializer.SerializeOptions.IncludeDefaultValues)
if (value.Equals(comparable) && !SerializeOptions.IncludeDefaultValues)
return;
}

serializer.Append(' ', serializer.IndentLevel * IndentCount);
serializer.Append(' ', IndentCount);
serializer.Append(key);
serializer.Append(' ');
IndentAndSerialize(serializer, value, forceClassName);
serializer.Append(LineEnding);
Append(' ', IndentLevel * IndentCount);
Append(' ', IndentCount);
Append(key);
Append(' ');
IndentAndSerialize(value, forceClassName);
Append(LineEnding);
}

private static void IndentAndSerialize(Serializer serializer, object value, bool forceClassName)
private void IndentAndSerialize(object value, bool forceClassName)
{
serializer.Indent();
Serialize(value, serializer, forceClassName);
serializer.Deindent();
Indent();
StaticSerialize(value, forceClassName);
Deindent();
}

private static IEnumerable<ISerializableMember> GetPropertiesAndFields(object input)
Expand Down Expand Up @@ -200,7 +197,7 @@ private static void SerializeSimpleValue(object input, PrettyPrinter prettyPrint
prettyPrinter.Append(input);
}

private static void SerializeGenericEnumerable(object input, SerializationContext context)
private void SerializeGenericEnumerable(object input, SerializationContext context)
{
var prettyPrinter = context.Serializer;
var forceClassName = context.ForceClassName;
Expand All @@ -226,14 +223,14 @@ private static void SerializeGenericEnumerable(object input, SerializationContex
addSpace = true;

var element = enumerator.Current;
IndentAndSerialize(prettyPrinter, element, forceClassName);
IndentAndSerialize(element, forceClassName);
}
}

prettyPrinter.Append("]");
}

private static void SerializeDictionary(IDictionary input, SerializationContext context)
private void SerializeDictionary(IDictionary input, SerializationContext context)
{
var serializer = context.Serializer;
var prettyPrinter = serializer.PrettyPrinter;
Expand All @@ -247,18 +244,18 @@ private static void SerializeDictionary(IDictionary input, SerializationContext
prettyPrinter.AppendOpenBlock();
foreach (var key in input.Keys)
{
SerializeKeyValue(serializer, key.ToString(), input[key]);
SerializeKeyValue(key.ToString(), input[key]);
}
prettyPrinter.AppendCloseBlock();
}

private static void SerializeCollection(IEnumerable collectionInput, SerializationContext context)
private void SerializeCollection(IEnumerable collectionInput, SerializationContext context)
{
var forceClassName = context.ForceClassName;

foreach (var item in collectionInput)
{
IndentAndSerialize(context.Serializer, item, forceClassName);
IndentAndSerialize(item, forceClassName);
}
}

Expand Down

0 comments on commit 5c88b13

Please sign in to comment.