Skip to content

Commit

Permalink
Merge pull request #100 from jdiamond/dev
Browse files Browse the repository at this point in the history
Release 1.15.2
  • Loading branch information
Romanx committed Jul 2, 2015
2 parents 43b749c + 0aa5526 commit bba83ca
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
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

0 comments on commit bba83ca

Please sign in to comment.