當前位置: 首頁>>代碼示例>>C#>>正文


C# Json.JsonReader類代碼示例

本文整理匯總了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;
        }
開發者ID:aduggleby,項目名稱:dragon,代碼行數:29,代碼來源:MailAddressSerializerConverter.cs

示例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;
        }
開發者ID:ytokas,項目名稱:appacitive-dotnet-sdk,代碼行數:33,代碼來源:FindConnectedObjectsResponseConverter.cs

示例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;
 }
開發者ID:dinomight,項目名稱:evelib,代碼行數:15,代碼來源:EmdItemOrdersJsonConverter.cs

示例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;
        }
開發者ID:Microsoft,項目名稱:sarif-sdk,代碼行數:28,代碼來源:FlagsEnumConverter.cs

示例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;
        }
開發者ID:skipme,項目名稱:simple-mongodb,代碼行數:7,代碼來源:SimoReferenceJsonConverter.cs

示例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;
 }
開發者ID:BenFradet,項目名稱:RiotSharp,代碼行數:33,代碼來源:TagStaticListConverter.cs

示例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;
        }
開發者ID:babelshift,項目名稱:SourceSchemaParser,代碼行數:33,代碼來源:SchemaItemsToDotaLeaguesJsonConverter.cs

示例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);
        }
開發者ID:nuub666,項目名稱:framework,代碼行數:7,代碼來源:JsonConverters.cs

示例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;
        }
開發者ID:Microsoft,項目名稱:RTVS,代碼行數:7,代碼來源:SecureStringJsonConverter.cs

示例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;
		}
開發者ID:BiYiTuan,項目名稱:elasticsearch-net,代碼行數:28,代碼來源:TokenFilterCollectionConverter.cs

示例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;
        }
開發者ID:TylerGarlick,項目名稱:FlitBit.Dto.WebApi,代碼行數:33,代碼來源:DefaultJsonConverter.cs

示例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;
        }
開發者ID:skjohansen,項目名稱:TictailSharp,代碼行數:7,代碼來源:LowercaseConverter.cs

示例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;
 }
開發者ID:ecomfi,項目名稱:halclient,代碼行數:29,代碼來源:HalResourceConverter.cs

示例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;
        }
開發者ID:NerdCats,項目名稱:TaskCat,代碼行數:27,代碼來源:VendorConverter.cs

示例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


注:本文中的Newtonsoft.Json.JsonReader類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。