Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/j value #99

Merged
merged 8 commits into from
Jul 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Nustache.Core.Tests/Describe_Render.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO;
using System.Data;
using System.IO;
using Newtonsoft.Json.Linq;
using NUnit.Framework;

namespace Nustache.Core.Tests
Expand Down Expand Up @@ -92,5 +94,15 @@ public void It_ignores_Newtonsoft_IEnumerable_results_with_no_values()

Assert.AreEqual(@"<a href=""https://github.com/jdiamond/Nustache/"" class=""nustache--logo"">Nustache Main</a>", result);
}

[TestCase("true", Result = "Hello World!")]
[TestCase("false", Result = "")]
public string It_identify_correctly_bool_from_Newtonsoft_JValue_object(string isExpectedMessage)
{
const string template = @"{{#if ABoolean}}{{AMessage}}{{/if}}";
var json = string.Format(@"{{""ABoolean"": {0},""AMessage"": ""Hello World!""}}", isExpectedMessage);

return Render.StringToString(template, JObject.Parse(json));
}
}
}
81 changes: 81 additions & 0 deletions Nustache.Core/JValueIdentifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma warning disable 1584,1711,1572,1581,1580

using System;

namespace Nustache.Core
{
/// <summary>
/// Service resonsible for identifying whether a value is a <see cref="Newtonsoft.Json.Linq.JValue" />, determining its
/// type and returns its value.
/// </summary>
public static class JValueIdentifier
{
#region Public Members

/// <summary>
/// Determines whether the <paramref name="obj" /> is a <see cref="Newtonsoft.Json.Linq.JValue" />.
/// </summary>
/// <param name="obj">
/// The <see cref="object" /> retrieved from the model.
/// </param>
/// <returns>
/// <see langword="True" /> whether the <paramref name="obj" /> is a <see cref="Newtonsoft.Json.Linq.JValue" /> object
/// otherwise <see langword="False" />.
/// </returns>
public static bool IsJValue(object obj)
{
return obj.GetType().ToString().Equals("Newtonsoft.Json.Linq.JValue");
}

/// <summary>
/// Returns the value of the <see cref="Newtonsoft.Json.Linq.JValue" /> object.
/// </summary>
/// <param name="jValue">
/// The <see cref="object" /> retrieved from the model.
/// </param>
/// <returns>
/// A native value determined by the <see cref="Type"/> returned by the property Type of <see cref="Newtonsoft.Json.Linq.JValue" />.
/// </returns>
public static object GetValue(object jValue)
{
var jValueType = jValue.GetType();
var typeOfValue = jValueType.GetProperty("Type").GetValue(jValue, null).ToString();

if (typeOfValue == "Boolean") // JTokenType.Boolean
{
return GetValue<bool>(jValueType, jValue);
}

if (typeOfValue == "String") // JTokenType.String
{
return GetValue<string>(jValueType, jValue);
}

if (typeOfValue == "Integer") // JTokenType.Integer
{
return GetValue<long>(jValueType, jValueType);
}

if (typeOfValue == "Float") // JTokenType.Float
{
return GetValue<double>(jValueType, jValueType);
}

return null;
}

#endregion

#region Private Members

private static T GetValue<T>(Type type, object obj)
{
var value = type.GetProperty("Value").GetValue(obj, null);
var valid = value is T;

return valid ? (T) value : default(T);
}

#endregion
}
}
1 change: 1 addition & 0 deletions Nustache.Core/Nustache.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Compile Include="FileSystemTemplateLocator.cs" />
<Compile Include="GenericDictionaryUtil.cs" />
<Compile Include="Helpers.cs" />
<Compile Include="JValueIdentifier.cs" />
<Compile Include="NustacheDataContextMissException.cs" />
<Compile Include="NustacheEmptyStringException.cs" />
<Compile Include="PartVisitor.cs" />
Expand Down
3 changes: 2 additions & 1 deletion Nustache.Core/ValueGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ internal PropertyDescriptorValueGetter(object target, PropertyDescriptor propert

public override object GetValue()
{
return _propertyDescriptor.GetValue(_target);
var value = _propertyDescriptor.GetValue(_target);
return JValueIdentifier.IsJValue(value) ? JValueIdentifier.GetValue(value) : value;
}
}

Expand Down