本文整理汇总了C#中JsonSerializer.Populate方法的典型用法代码示例。如果您正苦于以下问题:C# JsonSerializer.Populate方法的具体用法?C# JsonSerializer.Populate怎么用?C# JsonSerializer.Populate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonSerializer
的用法示例。
在下文中一共展示了JsonSerializer.Populate方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var keyType = objectType.GetGenericArguments()[0];
var keyValueType = keyType.BaseType.GetGenericArguments()[0];
var valueType = objectType.GetGenericArguments()[1];
var intermediateDictionaryType = typeof(Dictionary<,>).MakeGenericType(typeof(string), valueType);
var intermediateDictionary = (IDictionary)Activator.CreateInstance(intermediateDictionaryType);
serializer.Populate(reader, intermediateDictionary);
var valueProperty = keyType.GetProperty("Value");
var finalDictionary = (IDictionary)Activator.CreateInstance(objectType);
foreach (DictionaryEntry pair in intermediateDictionary)
{
object value;
if (keyValueType == typeof(Guid))
value = Guid.Parse(pair.Key.ToString());
else
value = Convert.ChangeType(pair.Key, keyValueType, null);
var key = Activator.CreateInstance(keyType);
valueProperty.SetValue(key, value, null);
finalDictionary.Add(key, pair.Value);
}
return finalDictionary;
}
示例3: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var dict = new Dictionary<string, Dictionary<string, Dictionary<string, IndexSettings>>>();
serializer.Populate(reader, dict);
var response = new IndexSettingsResponse();
if (!dict.HasAny() || !dict.First().Value.HasAny() || !dict.First().Value.First().Value.HasAny())
return response;
response.Nodes = dict.ToDictionary(k => k.Key, v => v.Value.First().Value.First().Value);
return response;
}
示例4: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
reader.Read();
if (reader.TokenType != JsonToken.PropertyName)
return null;
var key = reader.Value as string;
reader.Read();
switch (key)
{
case "delete":
var deleteItem = new BulkDeleteResponseItem();
serializer.Populate(reader, deleteItem);
if (deleteItem != null)
deleteItem.Operation = key;
reader.Read();
return deleteItem;
case "update":
var updateItem = new BulkUpdateResponseItem();
serializer.Populate(reader, updateItem);
if (updateItem != null)
updateItem.Operation = key;
reader.Read();
return updateItem;
case "index":
var indexItem = new BulkIndexResponseItem();
serializer.Populate(reader, indexItem);
if (indexItem != null)
indexItem.Operation = key;
reader.Read();
return indexItem;
case "create":
var createItem = new BulkCreateResponseItem();
serializer.Populate(reader, createItem);
if (createItem != null)
createItem.Operation = key;
reader.Read();
return createItem;
}
return null;
}
示例5: 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)
{
if (reader.TokenType == JsonToken.Null)
return null;
object obj = Container.Resolve(objectType);
if (obj == null)
throw new JsonSerializationException("No object created.");
serializer.Populate(reader, obj);
return obj;
}
示例6: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.StartObject) return null;
reader.Read();
if (reader.TokenType != JsonToken.PropertyName) return null;
var prop = reader.Value;
if ((string) reader.Value != "filter") return null;
reader.Read();
var agg = new FilterAggregator();
serializer.Populate(reader, agg);
return agg;
}
示例7: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartObject)
{
var denormalizedReference = new DenormalizedReference();
serializer.Populate(reader, denormalizedReference);
var referenceInstace = _generator.CreateClassProxyWithTarget(
denormalizedReference.ReferenceInstanceType,
new Type[] { typeof(IDynamicProxy) },
InstanceCreator.Create(denormalizedReference.ReferenceInstanceType),
new ReferenceInterceptor(denormalizedReference.Id, denormalizedReference.ReferenceInstanceType));
return referenceInstace;
}
return null;
}
示例8: 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)
{
if (reader.TokenType == JsonToken.Null)
return null;
using (var scope=Container.BeginScope())
{
object obj = scope.Resolve(objectType);
if (obj == null)
throw new JsonSerializationException("No object created.");
serializer.Populate(reader, obj);
/*RefrenceHacks.SkipRefrences = true;
scope.Scope.InjectUnsetProperties(obj);
RefrenceHacks.SkipRefrences = false;*/
return obj;
}
}
示例9: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject o = JObject.Load(reader);
var result = existingValue as Dictionary<string, AnalyzerBase> ?? new Dictionary<string, AnalyzerBase>();
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();
Language language;
if (Enum.TryParse(typePropertyValue, true, out language))
{
typePropertyValue = "Language";
}
var itemType = Type.GetType("Nest." + typePropertyValue + "Analyzer", false, true);
AnalyzerBase item;
if (itemType == typeof(LanguageAnalyzer))
{
item = new LanguageAnalyzer(language);
serializer.Populate(childProperty.Value.CreateReader(), item);
}
else if (itemType != null)
{
item = serializer.Deserialize(childProperty.Value.CreateReader(), itemType) as AnalyzerBase;
}
else
{
continue;
}
result[propertyName] = item;
}
return result;
}
示例10: PopulateDictionary
public void PopulateDictionary()
{
Dictionary<string, string> p = new Dictionary<string, string>();
JsonSerializer serializer = new JsonSerializer();
serializer.Populate(new StringReader(@"{""Name"":""James""}"), p);
Assert.AreEqual(1, p.Count);
Assert.AreEqual("James", p["Name"]);
}
示例11: PopulateListOfPeople
public void PopulateListOfPeople()
{
List<Person> p = new List<Person>();
JsonSerializer serializer = new JsonSerializer();
serializer.Populate(new StringReader(@"[{""Name"":""James""},{""Name"":""Jim""}]"), p);
Assert.AreEqual(2, p.Count);
Assert.AreEqual("James", p[0].Name);
Assert.AreEqual("Jim", p[1].Name);
}
示例12: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
JObject o = JObject.Load(reader);
var result = new IndexSettings();
var dictionary = new Dictionary<string, object>();
serializer.Populate(o.CreateReader(), dictionary);
result.Settings = dictionary;
result.AsExpando = DynamicDictionary.Create(dictionary);
foreach (var rootProperty in o.Children<JProperty>())
{
if (rootProperty.Name.Equals("analysis", StringComparison.InvariantCultureIgnoreCase))
{
result.Analysis = serializer.Deserialize<AnalysisSettings>(rootProperty.Value.CreateReader());
result.Settings.Remove(rootProperty.Name);
}
else if (rootProperty.Name.Equals("warmers", StringComparison.InvariantCultureIgnoreCase))
{
foreach (var jWarmer in rootProperty.Value.Children<JProperty>())
{
result.Warmers[jWarmer.Name] = serializer.Deserialize<WarmerMapping>(jWarmer.Value.CreateReader());
}
result.Settings.Remove(rootProperty.Name);
}
else if (rootProperty.Name.Equals("similarity", StringComparison.InvariantCultureIgnoreCase))
{
result.Similarity = serializer.Deserialize<SimilaritySettings>(rootProperty.Value.CreateReader());
result.Settings.Remove(rootProperty.Name);
}
}
return result;
}