-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from jdiamond/dev
Release 1.15.2
- Loading branch information
Showing
4 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters