本文整理汇总了C#中JToken.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# JToken.GetType方法的具体用法?C# JToken.GetType怎么用?C# JToken.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JToken
的用法示例。
在下文中一共展示了JToken.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConvertJToken
private object ConvertJToken(JToken tok)
{
if (tok is JObject)
{
JObject job = (JObject)tok;
Dictionary<string, object> dob = new Dictionary<string, object>();
foreach (JProperty prop in job.Properties())
{
dob[prop.Name] = ConvertJToken(prop.Value);
}
return dob;
}
else if (tok is JArray)
{
JArray jar = (JArray)tok;
List<object> lst = new List<object>();
JToken jt = jar.First;
while (jt != null)
{
lst.Add(ConvertJToken(jt));
jt = jt.Next;
}
return lst;
}
else if (tok is JValue)
{
return ((JValue)tok).Value;
}
else
{
throw new Exception("Unhandled token type: " + tok.GetType());
}
}
示例2: AddToBackingStore
private static void AddToBackingStore(Dictionary<string, object> backingStore, string prefix, JToken value)
{
if (value is JObject)
{
var jObject = value as JObject;
foreach (JProperty jProperty in jObject.Properties())
{
AddToBackingStore(backingStore, MakePropertyKey(prefix, jProperty.Name), jProperty.Value);
}
}
else if (value is JArray)
{
var jArray = value as JArray;
for (int i = 0; i < jArray.Count; i++)
{
AddToBackingStore(backingStore, MakeArrayKey(prefix, i), jArray[i]);
}
}
else if (value is JValue)
{
var jValue = value as JValue;
if (jValue.Value == null)
backingStore[prefix] = null;
else
backingStore[prefix] = jValue.Value.ToString(); //ToString added by Andres in order to allow Int64 to Decimal conversions
}
else
{
throw new Exception(string.Format("JToken is of unsupported type {0}", value.GetType()));
}
}
示例3: ConvertJTokenToDynamic
public static dynamic ConvertJTokenToDynamic(JToken token)
{
if (token is JValue)
{
return ((JValue) token).Value;
}
if (token is JObject)
{
ExpandoObject expando = new ExpandoObject();
(from childToken in token where childToken is JProperty select childToken as JProperty)
.ToList()
.ForEach(
property =>
{
((IDictionary<string, object>) expando).Add(property.Name,
ConvertJTokenToDynamic(property.Value));
});
return expando;
}
if (token is JArray)
{
object[] array = new object[((JArray) token).Count];
int index = 0;
foreach (JToken arrayItem in ((JArray) token))
{
array[index] = ConvertJTokenToDynamic(arrayItem);
index++;
}
return array;
}
throw new ArgumentException(
string.Format(CultureInfo.InvariantCulture, "Unknown token type '{0}'", token.GetType()), "token");
}
示例4: CleanObject
private static void CleanObject(JToken token)
{
var list = new Dictionary<JToken, JProperty>();
if (token.GetType() != typeof(JObject))
return;
// remove the results nodes
var childrenWithResults = token.Children<JProperty>()
.Where(c => c.Children<JObject>()["results"].Count() > 0).ToList();
foreach(var child in childrenWithResults)
{
var resultsProperty = child.Children()["results"];
var newProperty = new JProperty(child.Name, resultsProperty.Children());
child.Replace(newProperty);
}
// remove __deferred properties
var deferredChildren = token.Children<JProperty>()
.Where(c => c.Children<JObject>()["__deferred"].Count() > 0).ToList();
foreach(var deferred in deferredChildren)
{
deferred.Remove();
}
}
示例5: ValidateToken
internal override void ValidateToken(JToken o)
{
ValidationUtils.ArgumentNotNull(o, "o");
if (o.Type != JsonTokenType.Property)
throw new Exception("Can not add {0} to {1}".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
}
示例6: JsonToDynamic
static dynamic JsonToDynamic(JToken token)
{
if (token is JValue) return ((JValue)token).Value;
if (token is JObject)
{
var expando = new ExpandoObject();
foreach (var childToken in token.OfType<JProperty>())
{
((IDictionary<string, object>)expando).Add(childToken.Name, JsonToDynamic(childToken.Value));
}
return expando;
}
if (token is JArray)
{
var items = new List<ExpandoObject>();
foreach (var arrayItem in ((JArray)token))
{
items.Add(JsonToDynamic(arrayItem));
}
return items;
}
throw new ArgumentException(string.Format("Unknown token type {0}", token.GetType()), "token");
}
示例7: GetTokenPropertyType
private static Type GetTokenPropertyType(JToken token)
{
if (token is JValue)
{
JValue v = (JValue) token;
return (v.Value != null) ? v.Value.GetType() : typeof (object);
}
return token.GetType();
}
示例8: ResolveParameter
/// <summary>
/// Resolves a parameter value based on the provided object.
/// </summary>
/// <param name="descriptor">Parameter descriptor.</param>
/// <param name="value">Value to resolve the parameter value from.</param>
/// <returns>The parameter value.</returns>
public virtual object ResolveParameter(ParameterDescriptor descriptor, JToken value)
{
if (value.GetType() == descriptor.Type)
{
return value;
}
// A non generic implementation of ToObject<T> on JToken
using (var jsonReader = new JTokenReader(value))
{
var serializer = new JsonSerializer();
return serializer.Deserialize(jsonReader, descriptor.Type);
}
}
示例9: GetTokenIndex
protected static JToken GetTokenIndex(JToken t, bool errorWhenNoMatch, int index)
{
JArray a = t as JArray;
JConstructor c = t as JConstructor;
if (a != null)
{
if (a.Count <= index)
{
if (errorWhenNoMatch)
{
throw new JsonException("Index {0} outside the bounds of JArray.".FormatWith(CultureInfo.InvariantCulture, index));
}
return null;
}
return a[index];
}
else if (c != null)
{
if (c.Count <= index)
{
if (errorWhenNoMatch)
{
throw new JsonException("Index {0} outside the bounds of JConstructor.".FormatWith(CultureInfo.InvariantCulture, index));
}
return null;
}
return c[index];
}
else
{
if (errorWhenNoMatch)
{
throw new JsonException("Index {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, index, t.GetType().Name));
}
return null;
}
}
示例10: ConvertNode
private static Data ConvertNode(string name, JToken jtoken)
{
Contract.Requires(name != null);
Contract.Requires(jtoken != null);
if (jtoken is JValue)
{
return CreateAttribute(name, (JValue)jtoken);
}
if (jtoken is JObject)
{
var jobject = (JObject) jtoken;
return new DataNode(name, jobject.Cast<KeyValuePair<string, JToken>>().Select(j => ConvertNode(j.Key, j.Value)));
}
if (jtoken is JArray)
{
var jarray = (JArray)jtoken;
return new DataNode(name, jarray.Select(j => ConvertNode("Item", j)));
}
throw new Exception("Unexpected type: " + jtoken.GetType());
}
示例11: GetTokenIndex
// Token: 0x060001F7 RID: 503
// RVA: 0x0002C750 File Offset: 0x0002A950
protected static JToken GetTokenIndex(JToken t, bool errorWhenNoMatch, int index)
{
JArray jArray = t as JArray;
JConstructor jConstructor = t as JConstructor;
if (jArray != null)
{
if (jArray.Count > index)
{
return jArray[index];
}
if (errorWhenNoMatch)
{
throw new JsonException(StringUtils.FormatWith("Index {0} outside the bounds of JArray.", CultureInfo.InvariantCulture, index));
}
return null;
}
else if (jConstructor != null)
{
if (jConstructor.Count > index)
{
return jConstructor[index];
}
if (errorWhenNoMatch)
{
throw new JsonException(StringUtils.FormatWith("Index {0} outside the bounds of JConstructor.", CultureInfo.InvariantCulture, index));
}
return null;
}
else
{
if (errorWhenNoMatch)
{
throw new JsonException(StringUtils.FormatWith("Index {0} not valid on {1}.", CultureInfo.InvariantCulture, index, t.GetType().Name));
}
return null;
}
}
示例12: AssertJsonAreEqual
private static void AssertJsonAreEqual(JToken expectedJson, JToken actualJson, string path)
{
Assert.AreEqual(expectedJson.GetType(), actualJson.GetType(), "Type mismatch at " + path);
if (expectedJson is JObject)
{
AssertJsonAreEqual((JObject)expectedJson, (JObject)actualJson, path);
} else if (expectedJson is JObject)
{
AssertJsonAreEqual((JArray)expectedJson, (JArray)actualJson, path);
} else if (expectedJson is JValue)
{
AssertJsonAreEqual((JValue)expectedJson, (JValue)actualJson, path);
} else
{
throw new Exception("I don't know how to handle " + expectedJson.GetType().ToString());
}
}
示例13: Process
protected void Process( JToken jObject )
{
typeProcessor[jObject.GetType()]( jObject );
}
示例14: BuildPaths
private IEnumerable<IPath> BuildPaths(JToken data, Stack<Tuple<JProperty, bool>> propertyStack, JToken root)
{
var paths = new List<IPath>();
if (propertyStack.Count == 0 && data.IsEnumerable())
{
//
// Handle raw array of values
//
paths.Add(new JsonPath(JsonPath.EnumerableSymbol + JsonPath.SeperatorSymbol,
JsonPath.EnumerableSymbol + JsonPath.SeperatorSymbol, data.ToString()));
}
if (propertyStack.Count == 0 && data.IsPrimitive())
{
//
// Handle if the poco mapper is used to map to a raw primitive
//
paths.Add(new JsonPath(JsonPath.SeperatorSymbol, JsonPath.SeperatorSymbol, data.ToString()));
}
else if (data.IsObject())
{
var dataAsJObject = data as JObject;
if (dataAsJObject == null)
{
throw new Exception(string.Format("Data of type '{0}' expected, data of type '{1}' received.",
typeof (JObject), data.GetType()));
}
IList<JProperty> dataProperties = dataAsJObject.Properties().ToList();
foreach (
JProperty property in dataProperties.Where(p => p.IsPrimitive() || p.IsEnumerableOfPrimitives()))
{
JToken propertyData;
try
{
propertyData = property.Value;
}
catch (Exception ex)
{
Dev2Logger.Log.Error(ex);
propertyData = null;
}
if (propertyData != null)
{
paths.Add(BuildPath(propertyStack, property, root));
}
}
foreach (
JProperty property in dataProperties.Where(p => !p.IsPrimitive() && !p.IsEnumerableOfPrimitives()))
{
JContainer propertyData;
try
{
propertyData = property.Value as JContainer;
}
catch (Exception ex)
{
Dev2Logger.Log.Error(ex);
propertyData = null;
//TODO When an exception is encountered stop discovery for this path and write to log
}
if (propertyData != null)
{
if (property.IsEnumerable())
{
// ReSharper disable RedundantCast
var enumerableData = propertyData as IEnumerable;
// ReSharper restore RedundantCast
// ReSharper disable ConditionIsAlwaysTrueOrFalse
if (enumerableData != null)
// ReSharper restore ConditionIsAlwaysTrueOrFalse
{
IEnumerator enumerator = enumerableData.GetEnumerator();
enumerator.Reset();
if (enumerator.MoveNext())
{
propertyData = enumerator.Current as JContainer;
if (propertyData != null)
{
propertyStack.Push(new Tuple<JProperty, bool>(property, true));
paths.AddRange(BuildPaths(propertyData, propertyStack, root));
propertyStack.Pop();
}
}
}
}
else
{
propertyStack.Push(new Tuple<JProperty, bool>(property, true));
paths.AddRange(BuildPaths(propertyData, propertyStack, root));
//.........这里部分代码省略.........
示例15: parseValue
private static float parseValue(JToken v)
{
switch (v.GetType().Name)
{
case "JValue":
return (float)v;
case "JArray":
JArray vArray = (JArray)v;
float min = (float)vArray[0], max = (float)vArray[1];
if (min > max)
{
float temp = min;
min = max;
max = temp;
}
return (float)(min + (random.NextDouble() * (max - min)));
}
return 0f;
}