本文整理汇总了C#中Newtonsoft.Json.JsonReader类的典型用法代码示例。如果您正苦于以下问题:C# JsonReader类的具体用法?C# JsonReader怎么用?C# JsonReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonReader类属于Newtonsoft.Json命名空间,在下文中一共展示了JsonReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
string displayName = string.Empty;
string address = string.Empty;
while (reader.Read())
{
var tokenType = reader.TokenType;
if (reader.TokenType == JsonToken.PropertyName)
{
var val = (reader.Value as string )?? string.Empty;
if (val == "DisplayName")
{
displayName = reader.ReadAsString();
}
if (val == "Address")
{
address = reader.ReadAsString();
}
}
if (reader.TokenType == JsonToken.EndObject)
{
break;
}
}
var mailAddress = new MailAddress(address, displayName);
return mailAddress;
}
示例2: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken value = null;
var response = new FindConnectedObjectsResponse();
// status
var json = JObject.ReadFrom(reader) as JObject;
json.TryGetValue("status", out value);
response.Status = serializer.Deserialize<Status>(value.CreateReader());
if (response.Status.IsSuccessful == false)
return response;
json.Remove("status");
// paging info
// Extract paging info
json.TryGetValue("paginginfo", out value);
response.PagingInfo = serializer.Deserialize<PagingInfo>(value.CreateReader());
json.Remove("paginginfo");
// extract parent label
json.TryGetValue("parent", out value);
var parentLabel = value.ToString();
// Extract graph node.
json.TryGetValue("nodes", out value);
if (value.Type != JTokenType.Null)
{
var nodes = value.Values<JObject>();
ParseNodes(response, parentLabel, nodes, serializer);
}
else
response.Nodes = new List<GraphNode>();
return response;
}
示例3: ReadJson
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing value of object being read.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer) {
var result = new ItemOrders();
serializer.Converters.Add(new EmdRowCollectionJsonConverter<ItemOrders.ItemOrderEntry>());
result.Orders = serializer.Deserialize<EmdRowCollection<ItemOrders.ItemOrderEntry>>(reader);
return result;
}
示例4: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
int result = 0;
// What's happening in this code? We express [Flags] enums in JSON as arrays of
// strings. On deserialization, we walk the array, locate each string,
// and convert it to its equivalent enum value. Because we don't have a strong
// sense of the destination type, we simply treat the enum values as numbers
// and OR them together. This number will eventually be unboxed and assigned
// to the target enum property.
// Read start of array
reader.Read();
while (reader.TokenType == JsonToken.String)
{
string enumName = EnumConverter.ConvertToPascalCase((string)reader.Value);
result |= (int)Enum.Parse(objectType, enumName);
reader.Read();
}
return result;
}
示例5: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var reference = SupportedTypes[objectType].Invoke();
serializer.Populate(reader, reference);
return reference;
}
示例6: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue
, JsonSerializer serializer)
{
var token = JToken.Load(reader);
if (token.Values<string>() == null) return null;
var list = token.Values<string>();
var tags = new List<TagStatic>();
foreach (var str in list)
{
switch (str)
{
case "Fighter":
tags.Add(TagStatic.Fighter);
break;
case "Tank":
tags.Add(TagStatic.Tank);
break;
case "Mage":
tags.Add(TagStatic.Mage);
break;
case "Assassin":
tags.Add(TagStatic.Assassin);
break;
case "Support":
tags.Add(TagStatic.Support);
break;
case "Marksman":
tags.Add(TagStatic.Marksman);
break;
}
}
return tags;
}
示例7: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
List<DotaSchemaItem> leagues = new List<DotaSchemaItem>();
JToken t = JToken.Load(reader);
var properties = t.Children<JProperty>();
foreach (var item in properties)
{
JObject o = (JObject)item.Value;
bool isLeague = o["prefab"] != null && o["prefab"].ToString() == "league";
bool isAdmin =
o["tool"] != null
&& o["tool"]["usage"] != null
&& o["tool"]["usage"]["admin"] != null
&& o["tool"]["usage"]["admin"].ToString() == "1";
if (isLeague && !isAdmin)
{
var league = JsonConvert.DeserializeObject<DotaSchemaItem>(item.Value.ToString());
league.DefIndex = item.Name;
leagues.Add(league);
}
}
return leagues;
}
示例8: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jsonObject = JObject.Load(reader);
string liteKey = (string)jsonObject["Key"];
return Lite.Parse(liteKey);
}
示例9: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
if (typeof(SecureString) == objectType) {
return ((string)reader.Value).ToSecureString();
}
return reader.Value;
}
示例10: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject o = JObject.Load(reader);
var result = existingValue as Dictionary<string, TokenFilterBase> ?? new Dictionary<string, TokenFilterBase>();
foreach (var childProperty in o.Children<JProperty>())
{
var propertyName = childProperty.Name;
var typeProperty = ((JObject)childProperty.Value).Property("type");
typeProperty.Remove();
var typePropertyValue = typeProperty.Value.ToString().Replace("_", string.Empty);
TokenFilterBase item;
var itemType = Type.GetType("Nest." + typePropertyValue + "TokenFilter", false, true);
if (itemType != null)
{
item = serializer.Deserialize(childProperty.Value.CreateReader(), itemType) as TokenFilterBase;
}
else
{
continue;
}
result[propertyName] = item;
}
return result;
}
示例11: ReadJson
/// <summary>
/// Creates a new object via the Factory. Loads the instance via the serializer.
/// </summary>
/// <param name="reader">The JsonReader</param>
/// <param name="objectType">The Type of DTO</param>
/// <param name="existingValue">The DTO</param>
/// <param name="serializer">JsonSerializer</param>
/// <returns></returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JsonReader objectReader = null;
var instance = _container.CreateInstance(objectType);
if (reader.TokenType == JsonToken.StartObject || reader.TokenType == JsonToken.StartArray)
{
if (reader.TokenType == JsonToken.StartObject)
{
var jObject = JObject.Load(reader);
objectReader = jObject.CreateReader();
}
if (reader.TokenType == JsonToken.StartArray)
{
var jArray = JArray.Load(reader);
objectReader = jArray.CreateReader();
}
if(objectReader != null)
serializer.Populate(objectReader, instance);
}
return instance;
}
示例12: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
ProductStatus productStatus;
ProductStatus.TryParse(reader.Value.ToString(), true, out productStatus);
return productStatus;
}
示例13: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var obj = JToken.ReadFrom(reader);
var ret = JsonConvert.DeserializeObject(obj.ToString(), ObjectType ?? objectType, new JsonConverter[] { });
if (obj["_embedded"] != null && obj["_embedded"].HasValues) {
var enumerator = ((JObject) obj["_embedded"]).GetEnumerator();
while (enumerator.MoveNext()) {
var rel = enumerator.Current.Key;
foreach (var property in objectType.GetProperties()) {
var attribute = property.GetCustomAttributes(true).FirstOrDefault(attr => attr is HalEmbeddedAttribute &&
((HalEmbeddedAttribute) attr).Rel == rel);
if (attribute != null) {
var type = (attribute as HalEmbeddedAttribute).Type ?? property.PropertyType;
property.SetValue(ret,
JsonConvert.DeserializeObject(enumerator.Current.Value.ToString(), type,
new JsonConverter[] {new HalResourceConverter((attribute as HalEmbeddedAttribute).CollectionMemberType)}), null);
}
}
}
}
if (obj["_links"] != null && obj["_links"].HasValues && typeof (IHalResource).IsAssignableFrom(objectType)) {
((HalResource) ret).Links = JsonConvert.DeserializeObject<HalLinkCollection>(obj["_links"].ToString(),
new JsonConverter[]
{new HalLinkCollectionConverter()});
}
if (ret is IHalResource)
((IHalResource) ret).IsNew = false;
return ret;
}
示例14: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var obj = JObject.Load(reader);
ProfitSharingStrategy strategy;
var method = obj["Method"];
if (method == null)
{
throw new ArgumentNullException("Type", "Order type is null");
}
var profitSharingMethod = (ProfitSharingMethod)Enum.Parse(typeof(ProfitSharingMethod), method.Value<string>());
switch (profitSharingMethod)
{
case ProfitSharingMethod.FLAT_RATE:
strategy = new FlatRateStrategy();
break;
case ProfitSharingMethod.PRICE_PERCENTAGE:
strategy = new PricePercentageStrategy();
break;
default:
throw new NotImplementedException($"{profitSharingMethod.ToString()} is invalid/not supported");
}
serializer.Populate(obj.CreateReader(), strategy);
return strategy;
}
示例15: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var tokenArray = JToken.ReadFrom(reader);
var children = new List<IChartOfAccountChild>();
foreach (var token in tokenArray)
{
string type = token["type"].Value<string>();
if (type == AccountGroup.typeName)
{
var accountGroup = JsonConvert.DeserializeObject<AccountGroup>(token.ToString());
children.Add(accountGroup);
}
if (type == AccountIntervalGroup.typeName)
{
var accountIntervalGroup = JsonConvert.DeserializeObject<AccountIntervalGroup>(token.ToString());
children.Add(accountIntervalGroup);
}
}
return children;
}
开发者ID:foretagsplatsen,项目名称:Foretagsplatsen-DotNet-API,代码行数:25,代码来源:ChartOfAccountChildJsonConverter.cs