本文整理汇总了C#中Raven.Imports.Newtonsoft.Json.JsonReader类的典型用法代码示例。如果您正苦于以下问题:C# JsonReader类的具体用法?C# JsonReader怎么用?C# JsonReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonReader类属于Raven.Imports.Newtonsoft.Json命名空间,在下文中一共展示了JsonReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadJson
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="T:Raven.Imports.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 enumType = Nullable.GetUnderlyingType(objectType) ?? objectType;
if (enumType != objectType && reader.TokenType == JsonToken.Null)//nullable
return null;
return Enum.Parse(enumType, reader.Value.ToString(), true);
}
示例2: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
reader.Read();
Debug.Assert(reader.TokenType == JsonToken.PropertyName);
Debug.Assert((string)reader.Value == "coordinates");
if (objectType == typeof(Coordinate))
{
return ReadJsonCoordinate(reader);
}
if (typeof(IEnumerable<Coordinate>).IsAssignableFrom(objectType))
{
return ReadJsonCoordinates(reader);
}
if (typeof(List<Coordinate[]>).IsAssignableFrom(objectType))
{
return ReadJsonCoordinatesEnumerable(reader);
}
if (typeof(List<List<Coordinate[]>>).IsAssignableFrom(objectType))
{
return ReadJsonCoordinatesEnumerable2(reader);
}
throw new Exception();
}
示例3: ReadJson
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <remarks>
/// this converter will never be needed to deserialize from json -
/// built-in converter is enough as Json.Net serializes any collection - including IEnumerable{T} to json arrays.
/// </remarks>
/// <param name="reader">The <see cref="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>executing this method will throw <see cref="NotSupportedException">NotSupportedException</see> since this converter should not be used for reading</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//this converter will never be needed to deserialize from json - built-in converter is enough as
//Json.Net serializes any collection - including IEnumerable<T> to json arrays.
throw new NotSupportedException(@"JsonLinqEnumerableConverter should not be used to deserialize collections from json -
if this exception gets thrown, it is probably a bug.");
}
示例4: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
return wktReader.Read((string)reader.Value);
}
示例5: ReadJson
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="T:Raven.Imports.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)
{
// Handle null values
if (reader.TokenType == JsonToken.Null) return null;
var arrayItemType = objectType.GetElementType();
var arrayRank = objectType.GetArrayRank();
// Retrieve all the values from the Json
var arrayValues = ReadRank(reader, serializer, arrayItemType);
// Determine the lengths of all ranks for the array
var rankLengthList = GetRankLengthList(arrayValues);
// If empty values were found, make sure the ranks match in size
for (var i = rankLengthList.Count; i < arrayRank; i++)
{
rankLengthList.Add(0);
}
var rankLengthArray = rankLengthList.ToArray();
// Create the array that will hold the values
var retVal = Array.CreateInstance(arrayItemType, rankLengthArray);
// Make the assignments
SetValues(retVal, rankLengthArray, new int[0], 0, arrayValues);
return retVal;
}
示例6: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken jo = JToken.Load(reader);
System.Net.IPAddress address = jo["Address"].ToObject<System.Net.IPAddress>(serializer);
int port = jo["Port"].Value<int>();
return new System.Net.IPEndPoint(address, port);
}
示例7: ReadJson
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="T:Raven.Imports.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 token = RavenJToken.ReadFrom(reader);
var val = token as RavenJValue;
if(val != null)
return val.Value;
var array = token as RavenJArray;
if (array != null)
{
var dynamicJsonObject = new DynamicJsonObject(new RavenJObject());
return new DynamicList(array.Select(dynamicJsonObject.TransformToValue).ToArray());
}
var typeName = token.Value<string>("$type");
if(typeName != null)
{
var type = Type.GetType(typeName, false);
if(type != null)
{
return serializer.Deserialize(new RavenJTokenReader(token), type);
}
}
return new DynamicJsonObject((RavenJObject)((RavenJObject)token).CloneToken());
}
示例8: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var collection = new NameValueCollection();
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject)
break;
var key = (string)reader.Value;
if (reader.Read() == false)
throw new InvalidOperationException("Expected PropertyName, got " + reader.TokenType);
if (reader.TokenType == JsonToken.StartArray)
{
var values = serializer.Deserialize<string[]>(reader);
foreach (var value in values)
{
collection.Add(key, value);
}
}
else
{
collection.Add(key, reader.Value.ToString());
}
}
return collection;
}
示例9: 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;
}
示例10: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var s = reader.Value as string;
if(s != null)
{
if (objectType == typeof(DateTime) || objectType == typeof(DateTime?))
{
DateTime time;
if (DateTime.TryParseExact(s, Default.DateTimeFormatsToRead, CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, out time))
{
if (time.Kind == DateTimeKind.Unspecified)
return DateTime.SpecifyKind(time, DateTimeKind.Local);
if (s.EndsWith("+00:00"))
return time.ToUniversalTime();
return time;
}
}
if(objectType == typeof(DateTimeOffset) || objectType == typeof(DateTimeOffset?))
{
DateTimeOffset time;
if (DateTimeOffset.TryParseExact(s, Default.DateTimeFormatsToRead, CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, out time))
return time;
}
}
return DeferReadToNextConverter(reader, objectType, serializer, existingValue);
}
示例11: Deserialize
protected object Deserialize(JsonReader reader, Type type)
{
using (RavenJTokenReader tokenReader = new RavenJTokenReader(RavenJToken.ReadFrom(reader)))
{
return serializer.Deserialize(tokenReader, type);
}
}
示例12: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var s = reader.Value as string;
if (s == null)
return null;
return Etag.Parse(s);
}
示例13: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
try
{
var jsonObject = JObject.Load(reader);
var className = jsonObject.Property("ClassName").Value.ToString();
var name = jsonObject.Property("Name").Value.ToString();
var assemblyName = jsonObject.Property("AssemblyName").Value.ToString();
var signature = jsonObject.Property("Signature").Value.ToString();
var start = signature.IndexOf('(') + 1;
var end = signature.IndexOf(')');
var parametersString = signature.Substring(start, end - start);
var parameterTypeStrings = parametersString.Split(',');
if (string.IsNullOrEmpty(className) ||
string.IsNullOrEmpty(name) ||
string.IsNullOrEmpty(assemblyName) ||
parameterTypeStrings == null)
throw new ArgumentException("Json object is not representing a MethodInfo");
var typeName = string.Format("{0}, {1}", className, assemblyName);
var type = Type.GetType(typeName);
var method = type.GetMethods().Where(m => m.Name.Equals(name) && DoesSignatureMatch(m, parameterTypeStrings)).SingleOrDefault();
return method;
}
catch {
return null;
}
}
示例14: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// NOTE: THIS DOESN'T SUPPORT READING OF DynamicJsonObject !!!
var o = RavenJToken.Load(reader);
return (o.Type == JTokenType.Null || o.Type == JTokenType.Undefined) ? null : o;
}
示例15: DeferReadToNextConverter
protected object DeferReadToNextConverter(JsonReader reader, Type objectType, JsonSerializer serializer, object existingValue)
{
var anotherConverter = serializer.Converters
.Skip(serializer.Converters.IndexOf(this) + 1)
.FirstOrDefault(x => x.CanConvert(objectType));
if (anotherConverter != null)
return anotherConverter.ReadJson(reader, objectType, existingValue, serializer);
return reader.Value;
}