本文整理汇总了C#中Newtonsoft.Json.Linq.JValue.ToObject方法的典型用法代码示例。如果您正苦于以下问题:C# JValue.ToObject方法的具体用法?C# JValue.ToObject怎么用?C# JValue.ToObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Linq.JValue
的用法示例。
在下文中一共展示了JValue.ToObject方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Transform
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()+ ".");
}
}
示例2: Example
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"
}
示例3: Example
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"
}
示例4: Example
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);
}
示例5: Example
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);
}
示例6: ToObjectWithDefaultSettings
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;
}
}
示例7: EnumTests
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);
}
示例8: Update
/// <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);
}
示例9: EnumTests
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.");
}
示例10: UpdateAccessToken
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;
}