public static bool TryConvert(this IJsonValue value, out object propertyValue)
 {
     propertyValue = null;
     if (!value.IsNull())
     {
         switch (value.ValueType)
         {
             case JsonValueType.Boolean:
                 propertyValue = value.AsBool().Value;
                 break;
             case JsonValueType.Number:
                 propertyValue = value.AsNumber().Value;
                 break;
             case JsonValueType.String:
                 propertyValue = value.AsString();
                 break;
             case JsonValueType.Null:
                 break;
             case JsonValueType.Object:
             case JsonValueType.Array:
             default:
                 return false;
         }
     }
     return true;
 }
Пример #2
0
        public static bool? AsNullableBool(this string inputValue)
        {
            if (string.IsNullOrWhiteSpace(inputValue))
            {
                return null;
            }

            return inputValue.AsBool();
        }
 /// <summary>
 /// Try to convert a value to a CLR object.
 /// </summary>
 /// <param name="value">The value to convert.</param>
 /// <param name="propertyValue">The converted value.</param>
 /// <returns>
 /// A value indicating whether the conversion was successful.
 /// </returns>
 public static bool TryConvert(this JToken value, out object propertyValue)
 {
     propertyValue = null;
     if (!value.IsNull())
     {
         switch (value.Type)
         {
             case JTokenType.Boolean:
                 propertyValue = value.AsBool().Value;
                 break;
             case JTokenType.Float:
             case JTokenType.Integer:
                 propertyValue = value.AsNumber().Value;
                 break;
             case JTokenType.String:
                 propertyValue = value.AsString();
                 break;
             case JTokenType.Date:
                 propertyValue = value.ToObject<DateTime>().ToRoundtripDateString(); //win8 store json data serialization doesn't have Date type, and is always returning string type, we should do the same
                 break;
             case JTokenType.Null:
                 break;
             case JTokenType.Object:
             case JTokenType.Array:
             default:
                 return false;
         }
     }
     return true;
 }
 /// <summary>
 /// Try to convert a value to a CLR object.
 /// </summary>
 /// <param name="value">The value to convert.</param>
 /// <param name="propertyValue">The converted value.</param>
 /// <returns>
 /// A value indicating whether the conversion was successful.
 /// </returns>
 public static bool TryConvert(this JToken value, out object propertyValue)
 {
     propertyValue = null;
     if (!value.IsNull())
     {
         switch (value.Type)
         {
             case JTokenType.Boolean:
                 propertyValue = value.AsBool().Value;
                 break;
             case JTokenType.Float:
             case JTokenType.Integer:
                 propertyValue = value.AsNumber().Value;
                 break;
             case JTokenType.String:
                 propertyValue = value.AsString();
                 break;
             case JTokenType.Date:
                 propertyValue = value.ToObject<DateTime>(); // AsDateTime() doesn't make sense since DateTime is a reference not object type.  (Can't use 'as' operator)
                 break;
             case JTokenType.Null:
                 break;
             case JTokenType.Object:
             case JTokenType.Array:
             default:
                 return false;
         }
     }
     return true;
 }