Skip to content

Commit

Permalink
Added serialization support
Browse files Browse the repository at this point in the history
  • Loading branch information
shravan2x committed Dec 29, 2016
1 parent 8ffbea8 commit 1fd4d4f
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Gameloop.Vdf/VObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public void RemoveAt(string key)
_children.RemoveAll(x => x.Key == key);
}

public override void WriteTo(VdfWriter writer)
{
writer.WriteObjectStart();

foreach (VProperty child in _children)
child.WriteTo(writer);

writer.WriteObjectEnd();
}

#region ICustomTypeDescriptor Methods

public PropertyDescriptorCollection GetProperties()
Expand Down
6 changes: 6 additions & 0 deletions Gameloop.Vdf/VProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ public VProperty(string key, VToken value)
Key = key;
Value = value;
}

public override void WriteTo(VdfWriter writer)
{
writer.WriteKey(Key);
Value.WriteTo(writer);
}
}
}
18 changes: 17 additions & 1 deletion Gameloop.Vdf/VToken.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
namespace Gameloop.Vdf
using System.Globalization;
using System.IO;

namespace Gameloop.Vdf
{
public abstract class VToken
{
public VToken Parent { get; set; }
public VToken Previous { get; set; }
public VToken Next { get; set; }

public abstract void WriteTo(VdfWriter writer);

public override string ToString()
{
using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
{
VdfTextWriter vdfTextWriter = new VdfTextWriter(stringWriter);
WriteTo(vdfTextWriter);

return stringWriter.ToString();
}
}
}
}
5 changes: 5 additions & 0 deletions Gameloop.Vdf/VValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public VValue(object value)
Value = value;
}

public override void WriteTo(VdfWriter writer)
{
writer.WriteValue(this);
}

public override string ToString()
{
return Value.ToString();
Expand Down
14 changes: 14 additions & 0 deletions Gameloop.Vdf/VdfConvert.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
using System;
using System.Globalization;
using System.IO;
using System.Text;

namespace Gameloop.Vdf
{
public static class VdfConvert
{
public static string Serialize(VToken value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));

StringBuilder stringBuilder = new StringBuilder(256);
StringWriter stringWriter = new StringWriter(stringBuilder, CultureInfo.InvariantCulture);
(new VdfSerializer()).Serialize(stringWriter, value);

return stringWriter.ToString();
}

public static VProperty Deserialize(string value)
{
return Deserialize(value, VdfSerializerSettings.Default);
Expand Down
8 changes: 8 additions & 0 deletions Gameloop.Vdf/VdfSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ public class VdfSerializer
{
private readonly VdfSerializerSettings _settings;

public VdfSerializer() : this(VdfSerializerSettings.Default) { }

public VdfSerializer(VdfSerializerSettings settings)
{
_settings = settings;
}

public void Serialize(TextWriter textWriter, VToken value)
{
using (VdfWriter vdfWriter = new VdfTextWriter(textWriter))
value.WriteTo(vdfWriter);
}

public VProperty Deserialize(TextReader textReader)
{
using (VdfReader vdfReader = new VdfTextReader(textReader, _settings))
Expand Down
2 changes: 1 addition & 1 deletion Gameloop.Vdf/VdfStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public static class VdfStructure
{
// Format
public const char Quote = '"', Escape = '\\', Comment = '/';
public const char Quote = '"', Escape = '\\', Comment = '/', Assign = ' ', Indent = '\t';
public const char ConditionalStart = '[', ConditionalEnd = ']';
public const char ObjectStart = '{', ObjectEnd = '}';

Expand Down
77 changes: 77 additions & 0 deletions Gameloop.Vdf/VdfTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,87 @@ namespace Gameloop.Vdf
{
public class VdfTextWriter : VdfWriter
{
private readonly TextWriter _writer;
private int _indentationLevel;

public VdfTextWriter(TextWriter writer)
{
if (writer == null)
throw new ArgumentNullException(nameof(writer));

_writer = writer;
_indentationLevel = 0;
}

public override void WriteKey(string key)
{
AutoComplete(State.Key);
_writer.Write(VdfStructure.Quote);
_writer.Write(key);
_writer.Write(VdfStructure.Quote);
}

public override void WriteValue(VValue value)
{
AutoComplete(State.Value);
_writer.Write(VdfStructure.Quote);
_writer.Write(value);
_writer.Write(VdfStructure.Quote);
}

public override void WriteObjectStart()
{
AutoComplete(State.ObjectStart);
_writer.Write(VdfStructure.ObjectStart);

_indentationLevel++;
}

public override void WriteObjectEnd()
{
_indentationLevel--;

AutoComplete(State.ObjectEnd);
_writer.Write(VdfStructure.ObjectEnd);

if (_indentationLevel == 0)
AutoComplete(State.Finished);
}

private void AutoComplete(State next)
{
if (CurrentState == State.Start)
{
CurrentState = next;
return;
}

switch (next)
{
case State.Value:
_writer.Write(VdfStructure.Assign);
break;

case State.Key:
case State.ObjectStart:
case State.ObjectEnd:
_writer.WriteLine();
_writer.Write(new string(VdfStructure.Indent, _indentationLevel));
break;

case State.Finished:
_writer.WriteLine();
break;
}

CurrentState = next;
}

public override void Close()
{
base.Close();
if (CloseOutput)
_writer.Close();
}
}
}
40 changes: 38 additions & 2 deletions Gameloop.Vdf/VdfWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,45 @@ namespace Gameloop.Vdf
{
public abstract class VdfWriter : IDisposable
{
public void Dispose()
public bool CloseOutput { get; set; }
protected internal State CurrentState { get; protected set; }

protected VdfWriter()
{
CurrentState = State.Start;
CloseOutput = true;
}

public abstract void WriteObjectStart();

public abstract void WriteObjectEnd();

public abstract void WriteKey(string key);

public abstract void WriteValue(VValue value);

void IDisposable.Dispose()
{
if (CurrentState == State.Closed)
return;

Close();
}

public virtual void Close()
{
CurrentState = State.Closed;
}

protected internal enum State
{
throw new NotImplementedException();
Start,
Key,
Value,
ObjectStart,
ObjectEnd,
Finished,
Closed
}
}
}

0 comments on commit 1fd4d4f

Please sign in to comment.