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


C# JsonSerializer.Populate方法代碼示例

本文整理匯總了C#中Newtonsoft.Json.JsonSerializer.Populate方法的典型用法代碼示例。如果您正苦於以下問題:C# JsonSerializer.Populate方法的具體用法?C# JsonSerializer.Populate怎麽用?C# JsonSerializer.Populate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Newtonsoft.Json.JsonSerializer的用法示例。


在下文中一共展示了JsonSerializer.Populate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject json = JObject.Load(reader);

            var config = new AsimovConfig();
            serializer.Populate(json.CreateReader(), config);

            var self = GetSelf(json);
            if (self != null)
            {
                serializer.Populate(self.CreateReader(), config);
            }
            else
            {
                Log.ErrorFormat("Could not find agent specific config / environment for: {0}", _machineName);
            }

            var envConfigFile = Path.Combine(_configDir, string.Format("config.{0}.json", config.Environment));

            if (!File.Exists(envConfigFile))
                return config;

            Log.DebugFormat("Loading config file {0}", envConfigFile);

            using (var envReader = new StreamReader(envConfigFile))
            {
                using (var envJsonReader = new JsonTextReader(envReader))
                {
                    serializer.Populate(envJsonReader, config);
                }
            }

            return config;
        }
開發者ID:Fodsuk,項目名稱:asimov-deploy,代碼行數:34,代碼來源:AsimovConfigConverter.cs

示例2: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var json = JObject.Load(reader);

            var config = new SensuClientConfig();
            serializer.Populate(json.CreateReader(), config);

            if (!Directory.Exists(_configDir)) return config;

            foreach (var configFile in Directory.GetFiles(_configDir, "*.json"))
            {
                try {
                    using (var envReader = new StreamReader(configFile))
                    {
                        using (var envJsonReader = new JsonTextReader(envReader))
                        {
                            serializer.Populate(envJsonReader, config);
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Warn("File {0} has some errors and won't be ignored: {1}", configFile, e);
                }
            }

            return config;
        }
開發者ID:dogtbh,項目名稱:sensu-client,代碼行數:28,代碼來源:SensuClientConfigConverter.cs

示例3: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var json = JObject.Load(reader);

            var config = new AsimovConfig();
            serializer.Populate(json.CreateReader(), config);

            var self = GetSelf(json);
            if (self != null)
                serializer.Populate(self.CreateReader(), config);
            else
                Log.ErrorFormat("Could not find agent specific config / environment for: {0}", _machineName);

            var environments = config.Environment.Split(',');

            foreach (var environment in environments)
            {
                var envConfigFile = Path.Combine(_configDir, $"config.{environment.Trim()}.json");

                if (!File.Exists(envConfigFile))
                    continue;

                Log.DebugFormat("Loading config file {0}", envConfigFile);
				PopulateFromFile(envConfigFile, serializer, config);

				var env = new DeployEnvironment();
				PopulateFromFile(envConfigFile, serializer, env);
				config.Environments.Add(env);
			}

            return config;
        }
開發者ID:mullvaden,項目名稱:asimov-deploy-winagent,代碼行數:32,代碼來源:AsimovConfigConverter.cs

示例4: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            List<ResolvedAction> resolvedActions = new List<ResolvedAction>();

            JArray actionsArray = JArray.Load(reader);
            foreach (JObject jobject in actionsArray)
            {
                ResolvedAction resolvedAction = new ResolvedAction();
                serializer.Populate(jobject.CreateReader(), resolvedAction);
                resolvedAction.BeaconAction = new BeaconAction();
                serializer.Populate(jobject.CreateReader(), resolvedAction.BeaconAction);
                if (jobject["content"] != null)
                {
                    serializer.Populate(jobject["content"]?.CreateReader(), resolvedAction.BeaconAction);
                    resolvedAction.BeaconAction.PayloadString = jobject["content"]["payload"]?.ToString();
                    // create json object for fallback
                    if(!string.IsNullOrEmpty(resolvedAction.BeaconAction.PayloadString))
                    {
                        resolvedAction.BeaconAction.Payload = JsonObject.Parse(resolvedAction.BeaconAction.PayloadString);
                    }
                }
                resolvedActions.Add(resolvedAction);
            }

            return resolvedActions;
        }
開發者ID:sensorberg-dev,項目名稱:windows10-sdk,代碼行數:26,代碼來源:ResolvedActionConverter.cs

示例5: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            serializer.NullValueHandling = NullValueHandling.Ignore;
            if (IsMapType(objectType))
            {
                IDictionary list = new Dictionary<Object, Object>();
                serializer.Populate(reader, list);
                return list.ConvertMap(objectType);
            }
            else if (IsException(objectType))
            {
                Object exceptionObject = Activator.CreateInstance(HelpMethods.ImplementTypeDynamicly(objectType, typeof(IJavaException)));
                serializer.Populate(reader, exceptionObject);
                return exceptionObject;
            }

            Type typeWithIOpenEngSBModel;

            if (alreadyExtendedTypes.ContainsKey(objectType.Name))
            {
                typeWithIOpenEngSBModel = alreadyExtendedTypes[objectType.Name];
            }
            else
            {
                typeWithIOpenEngSBModel = HelpMethods.ImplementTypeDynamicly(objectType, typeof(IOpenEngSBModel));
                alreadyExtendedTypes.Add(objectType.Name, typeWithIOpenEngSBModel);
            }

            Object modelWithOpenEngsbModelTail = Activator.CreateInstance(typeWithIOpenEngSBModel);
            serializer.Populate(reader, modelWithOpenEngsbModelTail);
            return modelWithOpenEngsbModelTail;
        }
開發者ID:openengsb,項目名稱:loom-csharp,代碼行數:32,代碼來源:AbstractJsonMarshaller.cs

示例6: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
            JsonSerializer serializer)
        {
            JObject o = JObject.Load(reader);

            var prop = o.Properties().First();
            var key = prop.Name;
            switch (key)
            {
                case "delete":
                    var deleteItem = new BulkDeleteResponseItem();
                    serializer.Populate(prop.Value.CreateReader(), deleteItem);
                    if (deleteItem != null)
                        deleteItem.Operation = key;
                    return deleteItem;
                case "index":
                    var indexItem = new BulkIndexResponseItem();
                    serializer.Populate(prop.Value.CreateReader(), indexItem);
                    if (indexItem != null)
                        indexItem.Operation = key;
                    return indexItem;
                case "create":
                    var createItem = new BulkCreateResponseItem();
                    serializer.Populate(prop.Value.CreateReader(), createItem);
                    if (createItem != null)
                        createItem.Operation = key;
                    return createItem;
            }
            return null;
        }
開發者ID:Rustemt,項目名稱:NEST,代碼行數:30,代碼來源:BulkOperationResponseItemConverter.cs

示例7: ReadJson

		public override object ReadJson (Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			object instance;
			JContainer container;
			if (reader.TokenType == JsonToken.Null) {
				return null;
			}
			if (reader.TokenType == JsonToken.StartArray) {
				container = JArray.Load (reader);
				if (objectType == typeof(DynamicRow)) {
					var dynamicRow = new DynamicRow ();
					instance = dynamicRow;
					serializer.Populate (container.CreateReader (), dynamicRow.Items);
				}
				else if (objectType == typeof(DynamicItem)) {
					var dynamicTable = new DynamicTable ();
					instance = dynamicTable;
					serializer.Populate (container.CreateReader (), dynamicTable.Rows);
				}
				else throw new EtoException("Invalid object graph");
			} else {
				container = JObject.Load (reader);
				if (container["$type"] == null) {
					if (container["Rows"] != null)
						instance = new DynamicTable ();
					else if (container["Control"] != null)
						instance = new DynamicControl ();
					else
						throw new EtoException("Could not infer the type of object to create");

					serializer.Populate(container.CreateReader(), instance);
				}
				else {
					var type = Type.GetType ((string)container ["$type"]);
					if (!typeof(DynamicItem).IsAssignableFrom (type)) {
						var dynamicControl = new DynamicControl ();
						dynamicControl.Control = serializer.Deserialize (container.CreateReader ()) as Control;
						instance = dynamicControl;
					} else {
						instance = serializer.Deserialize (container.CreateReader ());
					}
				}
			}
			if (objectType == typeof(DynamicRow) && instance.GetType () != typeof(DynamicRow)) {
				var row = new DynamicRow();
				row.Items.Add (instance as DynamicItem);
				return row;
			}

			return instance;
		}
開發者ID:Exe0,項目名稱:Eto,代碼行數:51,代碼來源:DynamicLayoutConverter.cs

示例8: ReadJson

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
										JsonSerializer serializer)
		{
			if (reader.TokenType == JsonToken.StartArray)
			{
				var list = new List<ShardsSegment>();
				serializer.Populate(reader, list);
				return list.First();
			}

			var o = new ShardsSegment();
			serializer.Populate(reader, o);
			return o;
		}
開發者ID:JebteK,項目名稱:elasticsearch-net,代碼行數:14,代碼來源:ShardsSegmentConverter.cs

示例9: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var obj = JObject.Load(reader);
            RegistrationModelBase model;

            var type = obj["Type"];
            if (type == null)
            {
                obj["Type"] = ((IdentityTypes)0).ToString();
                type = obj["Type"];
            }

            string modelType = type.Value<string>();
            IdentityTypes actualType;
            if (!Enum.TryParse<IdentityTypes>(modelType, out actualType))
                throw new NotSupportedException("Invalid AssetType Provided");

            switch (actualType)
            {
                case IdentityTypes.USER:
                    model = new UserRegistrationModel();
                    break;
                case IdentityTypes.ENTERPRISE:
                    model = new EnterpriseUserRegistrationModel();
                    break;
                default:
                    model = new AssetRegistrationModel();
                    break;
            }

            serializer.Populate(obj.CreateReader(), model);
            return model;
        }
開發者ID:NerdCats,項目名稱:TaskCat,代碼行數:33,代碼來源:RegistrationModelConverter.cs

示例10: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            var objectDictionary = new Dictionary<string, object>();
            serializer.Populate(reader, objectDictionary);

            var propertyDictionary = new Dictionary<string, SerializedPropertyInfo>();
            foreach (string key in objectDictionary.Keys)
            {
                Type propertyType = objectDictionary[key].GetType();

                string serializedValue = objectDictionary[key].ToString();
                bool isString = false;

                if (propertyType == typeof(bool))
                {
                    serializedValue = serializedValue.ToLowerInvariant();
                }
                else if (propertyType == typeof(string))
                {
                    serializedValue = '"' + serializedValue + '"';
                    isString = true;
                }

                propertyDictionary.Add(
                    key,
                    new SerializedPropertyInfo(serializedValue, isString));
            }

            return propertyDictionary;
        }
開發者ID:Microsoft,項目名稱:sarif-sdk,代碼行數:35,代碼來源:PropertyBagConverter.cs

示例11: ReadJson

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            object retVal = null;
            var obj = JObject.Load(reader);
            if (objectType == typeof(Member))
            {
                var pt = obj["memberType"];
                if (pt == null)
                {
                    throw new ArgumentException("Missing memberType", "memberType");
                }

                string memberType = pt.Value<string>();
                retVal = _membersFactory.TryCreateMember(memberType);
                if (retVal == null)
                {
                    throw new NotSupportedException("Unknown memberType: " + memberType);
                }

            }
            else if(objectType == typeof(MembersSearchCriteria))
            {
                retVal = _membersFactory.CreateMemberSearchCriteria();
            }
            serializer.Populate(obj.CreateReader(), retVal);
            return retVal;
        }
開發者ID:sameerkattel,項目名稱:vc-community,代碼行數:27,代碼來源:PolymorphicMemberJsonConverter.cs

示例12: 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)
        {
            JObject jobject = JObject.Load(reader);
            object target = null;

            if (jobject != null)
            {
                var typeValue = jobject["type"].Value<string>();
                var type = new AuditBackendType(typeValue);

                if (type == AuditBackendType.File)
                {
                    target = new FileAuditBackend();
                }
                else
                {
                    if (type == AuditBackendType.Syslog)
                    {
                        target = new SyslogAuditBackend();
                    }
                }

                if (target == null)
                {
                    target = new CustomAuditBackend(new AuditBackendType(typeValue));
                }

                serializer.Populate(jobject.CreateReader(), target);
            }

            return target;
        }
開發者ID:rajanadar,項目名稱:VaultSharp,代碼行數:42,代碼來源:AuditBackendJsonConverter.cs

示例13: ReadJson

 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 {
     JObject jObject = JObject.Load(reader);
     object registration = jObject.Property("templateBody") == null ? new Registration() : new TemplateRegistration();
     serializer.Populate(jObject.CreateReader(), registration);
     return registration;
 }
開發者ID:jcookems,項目名稱:azure-mobile-services,代碼行數:7,代碼來源:PushHttpClient.cs

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

示例15: 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)
        {
            JObject jobject = JObject.Load(reader);
            object target = null;

            if (jobject != null)
            {
                var keyType = jobject["key_type"].Value<string>();

                if (string.Equals(keyType, SSHKeyType.otp.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    target = new SSHOTPRoleDefinition();
                }
                else
                {
                    if (string.Equals(keyType, SSHKeyType.dynamic.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        target = new SSHDynamicRoleDefinition();
                    }
                }
            }

            if (target != null)
            {
                serializer.Populate(jobject.CreateReader(), target);
            }

            return target;
        }
開發者ID:rajanadar,項目名稱:VaultSharp,代碼行數:39,代碼來源:SSHRoleDefinitionJsonConverter.cs


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