public static IExpressionConstant Transform(JValue obj) { // TODO: make this do something //var val = obj.Value; if (obj.Type.Equals(JTokenType.Integer)) { return NumericValue.Create(obj.ToObject<int>()); } else if (obj.Type.Equals(JTokenType.Float)) { return NumericValue.Create(obj.ToObject<decimal>()); } else if (obj.Type.Equals(JTokenType.String)) { return new StringValue(obj.ToObject<String>()); } else if (obj.Type.Equals(JTokenType.Boolean)) { return new BooleanValue(obj.ToObject<bool>()); } else if (obj.Type.Equals(JTokenType.Null)) { //throw new ApplicationException("NULL Not implemented yet"); return null; // TODO: Change this to an option } else { throw new Exception("Don't know how to transform a " +obj.GetType()+ "."); } }
public void Example() { JValue v1 = new JValue(true); bool b = (bool)v1.ToObject(typeof(bool)); Console.WriteLine(b); // true int i = (int)v1.ToObject(typeof(int)); Console.WriteLine(i); // 1 string s = (string)v1.ToObject(typeof(string)); Console.WriteLine(s); // "True" }
public void Example() { JValue v1 = new JValue(true); bool b = v1.ToObject<bool>(); Console.WriteLine(b); // true int i = v1.ToObject<int>(); Console.WriteLine(i); // 1 string s = v1.ToObject<string>(); Console.WriteLine(s); // "True" }
public void Example() { #region Usage JValue v1 = new JValue(true); bool b = (bool)v1.ToObject(typeof(bool)); Console.WriteLine(b); // true int i = (int)v1.ToObject(typeof(int)); Console.WriteLine(i); // 1 string s = (string)v1.ToObject(typeof(string)); Console.WriteLine(s); // "True" #endregion Assert.AreEqual("True", s); }
public void Example() { #region Usage JValue v1 = new JValue(true); bool b = v1.ToObject<bool>(); Console.WriteLine(b); // true int i = v1.ToObject<int>(); Console.WriteLine(i); // 1 string s = v1.ToObject<string>(); Console.WriteLine(s); // "True" #endregion Assert.AreEqual("True", s); }
public void ToObjectWithDefaultSettings() { try { JsonConvert.DefaultSettings = () => { return new JsonSerializerSettings { Converters = { new MetroStringConverter() } }; }; JValue v = new JValue(":::STRING:::"); string s = v.ToObject<string>(); Assert.AreEqual("string", s); } finally { JsonConvert.DefaultSettings = null; } }
public void EnumTests() { JValue v = new JValue(StringComparison.Ordinal); Assert.AreEqual(JTokenType.Integer, v.Type); string s = v.ToString(); Assert.AreEqual("Ordinal", s); StringComparison e = v.ToObject<StringComparison>(); Assert.AreEqual(StringComparison.Ordinal, e); dynamic d = new JValue(StringComparison.CurrentCultureIgnoreCase); StringComparison e2 = (StringComparison)d; Assert.AreEqual(StringComparison.CurrentCultureIgnoreCase, e2); string s1 = d.ToString(); Assert.AreEqual("CurrentCultureIgnoreCase", s1); string s2 = (string)d; Assert.AreEqual("CurrentCultureIgnoreCase", s2); d = new JValue("OrdinalIgnoreCase"); StringComparison e3 = (StringComparison)d; Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e3); v = new JValue("ORDINAL"); d = v; StringComparison e4 = (StringComparison)d; Assert.AreEqual(StringComparison.Ordinal, e4); StringComparison e5 = v.ToObject<StringComparison>(); Assert.AreEqual(StringComparison.Ordinal, e5); v = new JValue((int)StringComparison.OrdinalIgnoreCase); Assert.AreEqual(JTokenType.Integer, v.Type); StringComparison e6 = v.ToObject<StringComparison>(); Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e6); }
/// <summary> /// Initiates a remote interface property value update for the given <see cref="XNode"/>. /// </summary> /// <param name="node"></param> /// <param name="interfaceName"></param> /// <param name="propertyName"></param> /// <param name="jvalue"></param> public static void Update(XNode node, string interfaceName, string propertyName, JValue jvalue) { Contract.Requires<ArgumentNullException>(node != null); Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(interfaceName)); Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(propertyName)); Contract.Requires<ArgumentNullException>(jvalue != null); var remote = GetRemoteDescriptor(node, interfaceName); if (remote == null) throw new InvalidOperationException(); var property = remote.GetProperty(propertyName); if (property == null) throw new InvalidOperationException(); // properties must be readable and writable if (!property.CanWrite || !property.CanRead) throw new InvalidOperationException(); // extract incoming value and convert to appropriate property value type var type = property.PropertyType; var value = jvalue != null ? jvalue.ToObject(type) : null; var oldValue = property.GetValue(remote.Target); // if value has been changed, apply change to remote if (!object.Equals(oldValue, value)) property.SetValue(remote.Target, value); }
public void EnumTests() { JValue v = new JValue(StringComparison.Ordinal); Assert.AreEqual(JTokenType.Integer, v.Type); string s = v.ToString(); Assert.AreEqual("Ordinal", s); StringComparison e = v.ToObject<StringComparison>(); Assert.AreEqual(StringComparison.Ordinal, e); dynamic d = new JValue(StringComparison.CurrentCultureIgnoreCase); StringComparison e2 = (StringComparison)d; Assert.AreEqual(StringComparison.CurrentCultureIgnoreCase, e2); string s1 = d.ToString(); Assert.AreEqual("CurrentCultureIgnoreCase", s1); string s2 = (string)d; Assert.AreEqual("CurrentCultureIgnoreCase", s2); d = new JValue("OrdinalIgnoreCase"); StringComparison e3 = (StringComparison)d; Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e3); v = new JValue("ORDINAL"); d = v; StringComparison e4 = (StringComparison)d; Assert.AreEqual(StringComparison.Ordinal, e4); StringComparison e5 = v.ToObject<StringComparison>(); Assert.AreEqual(StringComparison.Ordinal, e5); v = new JValue((int)StringComparison.OrdinalIgnoreCase); Assert.AreEqual(JTokenType.Integer, v.Type); StringComparison e6 = v.ToObject<StringComparison>(); Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e6); // does not support EnumMember. breaking change to add ExceptionAssert.Throws<ArgumentException>(() => { d = new JValue("value_a"); EnumA e7 = (EnumA)d; Assert.AreEqual(EnumA.ValueA, e7); }, "Requested value 'value_a' was not found."); }
void UpdateAccessToken() { var http = new HttpClient (); var param = new Dictionary<string, string> (); param.Add ("grant_type", "client_credentials"); param.Add ("scope", "messaging:push"); param.Add ("client_id", admSettings.ClientId); param.Add ("client_secret", admSettings.ClientSecret); var result = http.PostAsync (admSettings.AdmAuthUrl, new FormUrlEncodedContent (param)).Result; var data = result.Content.ReadAsStringAsync().Result; var json = JObject.Parse (data); this.AccessToken = json ["access_token"].ToString (); JToken expiresJson = new JValue(3540); if (json.TryGetValue("expires_in", out expiresJson)) Expires = DateTime.UtcNow.AddSeconds(expiresJson.ToObject<int>() - 60); else Expires = DateTime.UtcNow.AddSeconds(3540); if (result.Headers.Contains ("X-Amzn-RequestId")) this.LastAmazonRequestId = string.Join("; ", result.Headers.GetValues("X-Amzn-RequestId")); LastRequest = DateTime.UtcNow; }