Skip to content

Commit

Permalink
Merge branch 'feature/CustomObjectSerialization' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
peschkaj committed Feb 25, 2012
2 parents c2d524a + 64b546c commit c6d55b4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions CorrugatedIron.Tests/CorrugatedIron.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<AssemblyOriginatorKeyFile>..\Metadata\CorrugatedIron.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=4.0.8.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
Expand Down
40 changes: 38 additions & 2 deletions CorrugatedIron.Tests/Json/RiakObjectConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using CorrugatedIron.Tests.Extensions;
using CorrugatedIron.Util;
using NUnit.Framework;
using Newtonsoft.Json;

namespace CorrugatedIron.Tests.Json.RiakObjectConversionTests
{
Expand Down Expand Up @@ -70,8 +71,7 @@ public void NonJsonObjectsCantBeDeserialisedFromJson()
}

[Test]
[Ignore("Only run this if you're interested in some perf stats for Json conversio" +
"n")]
[Ignore("Only run this if you're interested in some perf stats for Json conversion")]
public void JsonConversionTimerTest()
{
var testPerson = new Person
Expand Down Expand Up @@ -116,5 +116,41 @@ public void JsonConversionTimerTest()
Console.WriteLine("De" +
"serialisation took a total of {0} - {1} per iteration", sw.Elapsed, new TimeSpan(sw.ElapsedTicks / iterations));
}

[Test]
public void CustomSerializerWillSerializeJson()
{
var testPerson = new Person
{
DateOfBirth = new DateTime(1978, 12, 5, 0, 0, 0, DateTimeKind.Utc),
Email = "oj@buffered.io",
Name = new Name
{
FirstName = "OJ",
Surname = "Reeves"
},
PhoneNumbers = new List<PhoneNumber>
{
new PhoneNumber
{
Number = "12345678",
NumberType = PhoneNumberType.Home
}
}
};

var sots = new SerializeObjectToString<Person>(JsonConvert.SerializeObject);

var obj = new RiakObject("bucket", "key");
obj.SetObject(testPerson, RiakConstants.ContentTypes.ApplicationJson, sots);
obj.Value.ShouldNotBeNull();
obj.ContentType.ShouldEqual(RiakConstants.ContentTypes.ApplicationJson);

var json = obj.Value.FromRiakString();
json.ShouldEqual("{\"Name\":{\"FirstName\":\"OJ\",\"Surname\":\"Reeves\"},\"PhoneNumbers\":[{\"Number\":\"12345678\",\"NumberType\":1}],\"DateOfBirth\":\"\\/Date(281664000000)\\/\",\"Email\":\"oj@buffered.io\"}");

var deserialisedPerson = obj.GetObject<Person>();
deserialisedPerson.ShouldEqual(testPerson);
}
}
}
51 changes: 51 additions & 0 deletions CorrugatedIron/Models/RiakObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@

namespace CorrugatedIron.Models
{
public delegate string SerializeObjectToString<in T>(T theObject);

public delegate byte[] SerializeObjectToByteArray<in T>(T theObject);

public delegate T DeserializeObject<out T>(byte[] theObject, string contentType);

public class RiakObject
{
private List<string> _vtags;
Expand Down Expand Up @@ -348,6 +354,41 @@ private int CalculateHashCode()
}
}

public void SetObject<T>(T value, string contentType, SerializeObjectToString<T> serializeObject)
where T : class
{
if (string.IsNullOrEmpty(contentType))
{
throw new ArgumentException("contentType must be a valid MIME type");
}

if (serializeObject == null)
{
throw new ArgumentException("serializeObject cannot be null");
}

ContentType = contentType;

Value = serializeObject(value).ToRiakString();
}

public void SetObject<T>(T value, string contentType, SerializeObjectToByteArray<T> serializeObject)
{
if (string.IsNullOrEmpty(contentType))
{
throw new ArgumentException("contentType must be a valid MIME type");
}

if (serializeObject == null)
{
throw new ArgumentException("serializeObject cannot be null");
}

ContentType = contentType;

Value = serializeObject(value);
}

// setting content type of SetObject changes content type
public void SetObject<T>(T value, string contentType = null)
where T : class
Expand Down Expand Up @@ -393,6 +434,16 @@ public void SetObject<T>(T value, string contentType = null)
throw new NotSupportedException(string.Format("Your current ContentType ({0}), is not supported.", ContentType));
}

public T GetObject<T>(DeserializeObject<T> deserializeObject)
{
if (deserializeObject == null)
{
throw new ArgumentException("deserializeObject must not be null");
}

return deserializeObject(Value, ContentType);
}

public T GetObject<T>()
{
if(ContentType == RiakConstants.ContentTypes.ApplicationJson)
Expand Down

0 comments on commit c6d55b4

Please sign in to comment.