本文整理汇总了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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}