Adding IJsonConverter service

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2013-01-31 21:55:58 -08:00
parent e3fa12bcc7
commit a5763400f3
7 changed files with 395 additions and 3 deletions
@@ -262,6 +262,7 @@
<Compile Include="Mvc\Routes\UrlPrefixTests.cs" />
<Compile Include="Records\BigRecord.cs" />
<Compile Include="Security\DefaultEncryptionServiceTests.cs" />
<Compile Include="Services\JsonConverterTests.cs" />
<Compile Include="Storage\FileSystemStorageProviderTests.cs" />
<Compile Include="Stubs\InMemoryWebSiteFolder.cs" />
<Compile Include="Stubs\StubHttpContextAccessor.cs" />
@@ -0,0 +1,47 @@
using System;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using Orchard.Services;
namespace Orchard.Tests.Services {
[TestFixture]
public class JsonConverterTests {
[Test]
public void ShouldConvertPrimitiveTypesToJSon() {
var converter = new DefaultJsonConverter();
Assert.That(converter.Serialize(12), Is.EqualTo("12"));
Assert.That(converter.Serialize(true), Is.EqualTo("true"));
Assert.That(converter.Serialize(12.345), Is.EqualTo("12.345"));
Assert.That(converter.Serialize("string"), Is.EqualTo("\"string\""));
Assert.That(converter.Serialize(new DateTime(2013, 1, 31, 0, 0, 0, DateTimeKind.Utc)), Is.EqualTo("\"2013-01-31T00:00:00Z\""));
}
[Test]
public void ShouldConvertAnonymousTypeToJSon() {
dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");
Assert.That((int)d.number, Is.EqualTo(1000));
Assert.That((int)d["number"], Is.EqualTo(1000));
Assert.That((int)d.array.Count, Is.EqualTo(6));
}
[Test]
public void ShouldConvertWellKnownTypeToJSon() {
var converter = new DefaultJsonConverter();
string result = converter.Serialize(new Animal { Age = 12, Name = "Milou" });
var o = converter.Deserialize<Animal>(result);
Assert.That(o.Age, Is.EqualTo(12));
Assert.That(o.Name, Is.EqualTo("Milou"));
}
public class Animal {
public int Age { get; set; }
public string Name { get; set; }
}
}
}
+2
View File
@@ -273,6 +273,8 @@
<Compile Include="Security\CurrentUserWorkContext.cs" />
<Compile Include="Security\Providers\DefaultEncryptionService.cs" />
<Compile Include="Services\Clock.cs" />
<Compile Include="Services\DefaultJsonConverter.cs" />
<Compile Include="Services\IJsonConverter.cs" />
<Compile Include="Settings\CurrentSiteWorkContext.cs" />
<Compile Include="Settings\ResourceDebugMode.cs" />
<Compile Include="Themes\CurrentThemeWorkContext.cs" />
@@ -0,0 +1,26 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Orchard.Services {
/// <summary>
/// An implementation of <see cref="IJsonConverter"/> using the Newtonsoft.Json library
/// </summary>
public class DefaultJsonConverter : IJsonConverter {
public string Serialize(object o) {
return JsonConvert.SerializeObject(o);
}
public string Serialize(object o, JsonFormat format) {
return JsonConvert.SerializeObject(o, format == JsonFormat.Indented ? Formatting.Indented : Formatting.None);
}
public dynamic Deserialize(string json) {
dynamic result = JObject.Parse(json);
return result;
}
public T Deserialize<T>(string json) {
return JsonConvert.DeserializeObject<T>(json);
}
}
}
+42
View File
@@ -0,0 +1,42 @@
namespace Orchard.Services {
/// <summary>
/// Provides services to serialize and deserialize objects to and from
/// Json documents.
/// </summary>
public interface IJsonConverter : IDependency {
/// <summary>
/// Serializes an object to Json.
/// </summary>
/// <param name="o">The object to serialize.</param>
/// <returns>A string representing the object as Json.</returns>
string Serialize(object o);
/// <summary>
/// Serializes an object to Json using an optional indentation.
/// </summary>
/// <param name="o">The object to serialize.</param>
/// <param name="format">Whether the document should be indented.</param>
/// <returns>A string representing the object as Json.</returns>
string Serialize(object o, JsonFormat format);
/// <summary>
/// Deserializes a Json document to a dynamic object.
/// </summary>
/// <param name="json">The Json document to deserialize.</param>
/// <returns>The deserialized object.</returns>
dynamic Deserialize(string json);
/// <summary>
/// Deserializes a Json document to a specific object.
/// </summary>
/// <typeparam name="T">The type of the object to deserialize.</typeparam>
/// <param name="json">The Json document to deserialize.</param>
/// <returns>The deserialized object.</returns>
T Deserialize<T>(string json);
}
public enum JsonFormat {
None,
Indented
}
}