本文整理汇总了C#中JToken.ToObject方法的典型用法代码示例。如果您正苦于以下问题:C# JToken.ToObject方法的具体用法?C# JToken.ToObject怎么用?C# JToken.ToObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JToken
的用法示例。
在下文中一共展示了JToken.ToObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: decipherAreasJSON
/*
* Gets all the sub-categories (or topics?) of jt, and hangs them under node?
*/
//private static void decipherAreasJSON(JToken jt, Node node)
private static void decipherAreasJSON(JToken jt, Category group)
{
if (jt is JProperty)
{
if (jt.ToObject<JProperty>().Name != IFIXIT_CATEGORY_OBJECT_KEY)
{
// Debug.WriteLine(" " + jt.ToObject<JProperty>().Name);
// since it's not a device, it must be a group
//Node curr = new Node(jt.ToObject<JProperty>().Name, new List<Node>());
Category curr = new Category(jt.ToObject<JProperty>().Name);
//FIXME this is where we add to the 1:M, right?
//group.Categories.Add(curr);
group.AddCategory(curr);
curr.Parent = group;
curr.parentName = group.Name;
if (jt.HasValues)
{
IJEnumerable<JToken> values = jt.Values();
foreach (JToken val in values)
{
decipherAreasJSON(val, curr);
}
}
}
// these are devices
else
{
IJEnumerable<JToken> devs = jt.Values();
foreach (JToken dev in devs)
{
Topic d = new Topic();
d.Name = dev.ToString();
/*
if (group.Devices == null)
{
group.Devices = new List<Topic>();
}
*/
//group.Topics.Add(d);
group.AddTopic(d);
d.Parent = group;
d.parentName = group.Name;
//node.getChildrenList().Add(new Node(dev.ToString(), null));
// Debug.WriteLine(" " + dev.ToString());
}
}
}
else
{
// can currently safely ignore anything in this category
// Debug.WriteLine("\n" + jt.ToString() + " not a property\n");
}
}
示例2: SafeAdd
/// <summary>
/// Write a property to a <see cref="JObject"/> only if it does not already exist and the value is not null.
/// </summary>
/// <param name="jobject">The <see cref="JObject"/> to write to.</param>
/// <param name="propertyName">The name of the property to write.</param>
/// <param name="token">The value of the property to write if it does not exist.</param>
public static void SafeAdd(this JObject jobject, string propertyName, JToken token)
{
if (token != null && token.ToObject<object>() != null)
{
jobject[propertyName] = jobject[propertyName] ?? token;
}
}
示例3: Validate
public ValidationResult Validate(string name, JToken value)
{
if (value.Type != JTokenType.Object)
{
ValidationResult.Fail(ValidationErrorCodes.Schema.UnexpectedType, $"Expected metadata object for property {name}.", name);
}
MetadataDefinition md;
try
{
md = value.ToObject<MetadataDefinition>();
}
catch (JsonException ex)
{
return ValidationResult.Fail(ValidationErrorCodes.Schema.BadSchema, $"Bad metadata definition: {ex.Message}", name);
}
switch (md.Type)
{
case "string":
case "integer":
case "float":
case "boolean":
break;
default:
return ValidationResult.Fail(ValidationErrorCodes.Schema.UnexpectedType, $"Expected metadata object for property {name}.type.", name + ".type");
}
if (md.IsQueryable)
{
if (string.IsNullOrWhiteSpace(md.QueryName))
{
return ValidationResult.Fail(ValidationErrorCodes.Schema.BadSchema, $"Expected metadata object for property {name}.query_name.", name + ".query_name");
}
}
return ValidationResult.Success;
}
示例4: GetDictionaryFromJsonData
private Dictionary<DateTime, Ticket> GetDictionaryFromJsonData(JToken jtoken)
{
var result = jtoken.ToObject<Result<Ticket>>();
return result.IsSuccessful && jtoken["data"] != null
? JsonConvert.DeserializeObject<Dictionary<DateTime, Ticket>>(jtoken["data"].ToString())
: default(Dictionary<DateTime, Ticket>);
}
示例5: ConvertJToken
public override object ConvertJToken(JToken jToken)
{
// we need to decide what kind of profile this is
if (jToken is JValue)
{
// it's just a string -
return jToken.ToString();
}
// for now, this is the only other IProfile implementation
return jToken.ToObject<ImageServiceProfile>();
}
示例6: Set
public override void Set(JToken jToken)
{
Person person = jToken.ToObject<Person>();
this.FirstName = person.FirstName;
this.LastName = person.LastName;
this.Age = person.Age;
}
示例7: ConvertGepJsonTokenToObject
public static Object ConvertGepJsonTokenToObject(JToken fieldToken, Field.FieldType fieldType)
{
switch (fieldType)
{
case ESRI.ArcGIS.Client.Field.FieldType.Integer:
case ESRI.ArcGIS.Client.Field.FieldType.OID:
return fieldToken.ToObject<Int32?>();
case ESRI.ArcGIS.Client.Field.FieldType.SmallInteger:
return fieldToken.ToObject<Int16?>();
case ESRI.ArcGIS.Client.Field.FieldType.Double:
return fieldToken.ToObject<Double?>();
case ESRI.ArcGIS.Client.Field.FieldType.Single:
return fieldToken.ToObject<Single?>();
case ESRI.ArcGIS.Client.Field.FieldType.String:
case ESRI.ArcGIS.Client.Field.FieldType.GlobalID:
case ESRI.ArcGIS.Client.Field.FieldType.GUID:
case ESRI.ArcGIS.Client.Field.FieldType.XML:
return fieldToken.ToObject<String>();
case ESRI.ArcGIS.Client.Field.FieldType.Date:
return UnixTimeToDateTime((long?)fieldToken);
case ESRI.ArcGIS.Client.Field.FieldType.Geometry:
case ESRI.ArcGIS.Client.Field.FieldType.Blob:
case ESRI.ArcGIS.Client.Field.FieldType.Raster:
case ESRI.ArcGIS.Client.Field.FieldType.Unknown:
return fieldToken.ToObject<Object>();
default:
return fieldToken.ToObject<Object>();
}
}
示例8: ChangeSet
public ChangeSet(string sha, string workspace, string bucket, JToken author, IReadOnlyList<ChangeItem> changes)
{
Sha = sha;
Workspace = workspace;
Bucket = bucket;
Author = author == null
? null
: author.Type == JTokenType.String ? author.ToString() : author.ToObject<Author>().Email;
Changes = changes;
}
示例9: ConvertValue
private object ConvertValue(JToken value)
{
switch (value.Type)
{
case JTokenType.Integer:
return value.ToObject<int>();
case JTokenType.Float:
return value.ToObject<double>();
case JTokenType.Boolean:
return value.ToObject<bool>();
case JTokenType.Date:
return value.ToObject<DateTime>();
case JTokenType.Guid:
return value.ToObject<Guid>();
case JTokenType.TimeSpan:
return value.ToObject<TimeSpan>();
default:
return value.ToString();
}
}
示例10: SafeAdd
/// <summary>
/// Write a property to a <see cref="JObject"/> only if it does not already exist and the value is not null.
/// </summary>
/// <param name="jobject">The <see cref="JObject"/> to write to.</param>
/// <param name="propertyName">The name of the property to write.</param>
/// <param name="token">The value of the property to write if it does not exist.</param>
public static void SafeAdd(this JObject jobject, string propertyName, JToken token)
{
if (String.IsNullOrWhiteSpace(propertyName))
{
Log.Warn("Asked to set a property named null on a JSON object!");
return;
}
if (token != null && (token.HasValues || token.ToObject(typeof(object)) != null))
{
jobject[propertyName] = jobject[propertyName] ?? token;
}
}
示例11: SetAttribute
internal void SetAttribute(string attributeName, JToken value)
{
object targetObject;
PropertyInfo targetPropertyInfo;
if (this.GetAttributeTarget(attributeName, out targetObject, out targetPropertyInfo))
{
targetPropertyInfo.SetValue(targetObject, value.ToObject(targetPropertyInfo.PropertyType));
}
else
{
throw new AutomationException("Could not access attribute {0}.", attributeName);
}
}
示例12: GetValue
private object GetValue(JToken token)
{
if (token is JObject)
return new JObjectDataItem((JObject)token);
if (token is JArray)
{
var jArray = (JArray)token;
var result = new object[jArray.Count];
for (var index = 0; index < jArray.Count; ++index)
result[index] = GetValue(jArray[index]);
return result;
}
return token.ToObject<object>();
}
示例13: OnReceived
protected override void OnReceived(JToken message)
{
var invocation = message.ToObject<HubInvocation>();
HubProxy hubProxy;
if (_hubs.TryGetValue(invocation.Hub, out hubProxy))
{
if (invocation.State != null)
{
foreach (var state in invocation.State)
{
hubProxy[state.Key] = state.Value;
}
}
hubProxy.InvokeEvent(invocation.Method, invocation.Args);
}
base.OnReceived(message);
}
示例14: OnReceived
protected override void OnReceived(JToken message)
{
var _invocation = message.ToObject<HubInvocation>();
HubProxy _hubProxy;
if (m_hubs.TryGetValue(_invocation.Hub, out _hubProxy))
{
if (_invocation.State != null)
{
foreach (var state in _invocation.State)
{
_hubProxy[state.Key] = state.Value;
}
}
Console.WriteLine("BER: " + _invocation.Method);
_hubProxy.InvokeEvent(_invocation.Method, _invocation.Args);
}
base.OnReceived(message);
}
示例15: GetListFromJsonData
private IEnumerable<Ticket> GetListFromJsonData(string origin, JToken jtoken)
{
var result = jtoken.ToObject<Result<Ticket>>();
if (result.IsSuccessful)
{
if (jtoken["data"] != null)
{
var objs = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<int, Ticket>>>(jtoken["data"].ToString());
foreach (var element in objs)
{
foreach (var item in element.Value)
{
item.Value.Origin = origin;
item.Value.Destination = element.Key;
yield return item.Value;
}
}
}
}
}