/// <summary>
 /// Populates result with the value for the key, if possible.
 /// </summary>
 /// <typeparam name="T">The desired type for the value.</typeparam>
 /// <param name="key">The key to retrieve a value for.</param>
 /// <param name="result">The value for the given key, converted to the
 /// requested type, or null if unsuccessful.</param>
 /// <returns>true if the lookup and conversion succeeded, otherwise false.</returns>
 public bool TryGetValue <T>(string key, out T result)
 {
     if (this.properties.ContainsKey(key))
     {
         try
         {
             var temp = ConversionHelpers.DowncastValue <T>(this.properties[key]);
             result = temp;
             return(true);
         }
         catch
         {
             // Could not convert, do nothing
         }
     }
     result = default(T);
     return(false);
 }
 /// <summary>
 /// Gets a value for the key of a particular type.
 /// </summary>
 /// <typeparam name="T">The type to convert the value to. Supported types are
 /// ParseObject and its descendents, Parse types such as ParseRelation and ParseGeopoint,
 /// primitive types,IList&lt;T&gt;, IDictionary&lt;string, T&gt; and strings.</typeparam>
 /// <param name="key">The key of the element to get.</param>
 /// <exception cref="System.Collections.Generic.KeyNotFoundException">The property is retrieved
 /// and <paramref name="key"/> is not found.</exception>
 /// <exception cref="System.FormatException">The property under this <paramref name="key"/>
 /// key was found, but of a different type.</exception>
 public T Get <T>(string key)
 {
     return(ConversionHelpers.DowncastValue <T>(this.properties[key]));
 }
Beispiel #3
0
 public void TestToWithConstructedNullableNonPrimitive() => Assert.ThrowsException <InvalidCastException>(() => ConversionHelpers.DowncastValue <DummyValueTypeA?>(new DummyValueTypeB {
 }));
Beispiel #4
0
        public object Decode(object data)
        {
            if (data is null)
            {
                return(null);
            }

            if (data is IDictionary <string, object> dict)
            {
                if (dict.ContainsKey("__op"))
                {
                    return(ParseFieldOperations.Decode(dict));
                }

                dict.TryGetValue("__type", out object type);

                switch (type)
                {
                case "Date":
                    return(ParseDate(dict["iso"] as string));

                case "Bytes":
                    return(Convert.FromBase64String(dict["base64"] as string));

                case "Pointer":
                    return(DecodePointer(dict["className"] as string, dict["objectId"] as string));

                case "File":
                    return(new ParseFile(dict["name"] as string, new Uri(dict["url"] as string)));

                case "GeoPoint":
                    return(new ParseGeoPoint(ConversionHelpers.DowncastValue <double>(dict["latitude"]),
                                             ConversionHelpers.DowncastValue <double>(dict["longitude"])));

                case "Object":
                    IObjectState state = ParseObjectCoder.Instance.Decode(dict, this);
                    return(ParseObject.FromState <ParseObject>(state, dict["className"] as string));

                case "Relation":
                    return(ParseRelationBase.CreateRelation(null, null, dict["className"] as string));

                case object anything when !(anything is string):
                    Dictionary <string, object> newDict = new Dictionary <string, object> {
                    };
                    foreach (KeyValuePair <string, object> pair in dict)
                    {
                        newDict[pair.Key] = Decode(pair.Value);
                    }
                    return(newDict);
                }

                Dictionary <string, object> converted = new Dictionary <string, object>();
                foreach (KeyValuePair <string, object> pair in dict)
                {
                    converted[pair.Key] = Decode(pair.Value);
                }

                return(converted);
            }

            return(data is IList <object> list ? (from item in list select Decode(item)).ToList() : data);
        }
Beispiel #5
0
 public void TestToWithConstructedNullablePrimitive() => Assert.IsTrue(ConversionHelpers.DowncastValue <int?>((double)4) is int?);