diff --git a/Nustache.Core.Tests/Describe_Render.cs b/Nustache.Core.Tests/Describe_Render.cs index 7b7699d..d043441 100644 --- a/Nustache.Core.Tests/Describe_Render.cs +++ b/Nustache.Core.Tests/Describe_Render.cs @@ -1,4 +1,6 @@ -using System.IO; +using System.Data; +using System.IO; +using Newtonsoft.Json.Linq; using NUnit.Framework; namespace Nustache.Core.Tests @@ -92,5 +94,15 @@ public void It_ignores_Newtonsoft_IEnumerable_results_with_no_values() Assert.AreEqual(@"Nustache Main", 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)); + } } } \ No newline at end of file diff --git a/Nustache.Core/JValueIdentifier.cs b/Nustache.Core/JValueIdentifier.cs new file mode 100644 index 0000000..69f2890 --- /dev/null +++ b/Nustache.Core/JValueIdentifier.cs @@ -0,0 +1,81 @@ +#pragma warning disable 1584,1711,1572,1581,1580 + +using System; + +namespace Nustache.Core +{ + /// + /// Service resonsible for identifying whether a value is a , determining its + /// type and returns its value. + /// + public static class JValueIdentifier + { + #region Public Members + + /// + /// Determines whether the is a . + /// + /// + /// The retrieved from the model. + /// + /// + /// whether the is a object + /// otherwise . + /// + public static bool IsJValue(object obj) + { + return obj.GetType().ToString().Equals("Newtonsoft.Json.Linq.JValue"); + } + + /// + /// Returns the value of the object. + /// + /// + /// The retrieved from the model. + /// + /// + /// A native value determined by the returned by the property Type of . + /// + 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(jValueType, jValue); + } + + if (typeOfValue == "String") // JTokenType.String + { + return GetValue(jValueType, jValue); + } + + if (typeOfValue == "Integer") // JTokenType.Integer + { + return GetValue(jValueType, jValueType); + } + + if (typeOfValue == "Float") // JTokenType.Float + { + return GetValue(jValueType, jValueType); + } + + return null; + } + + #endregion + + #region Private Members + + private static T GetValue(Type type, object obj) + { + var value = type.GetProperty("Value").GetValue(obj, null); + var valid = value is T; + + return valid ? (T) value : default(T); + } + + #endregion + } +} \ No newline at end of file diff --git a/Nustache.Core/Nustache.Core.csproj b/Nustache.Core/Nustache.Core.csproj index d4fd04e..a9fbd31 100644 --- a/Nustache.Core/Nustache.Core.csproj +++ b/Nustache.Core/Nustache.Core.csproj @@ -54,6 +54,7 @@ + diff --git a/Nustache.Core/ValueGetter.cs b/Nustache.Core/ValueGetter.cs index 9c234f0..edbc1fd 100644 --- a/Nustache.Core/ValueGetter.cs +++ b/Nustache.Core/ValueGetter.cs @@ -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; } }