本文整理汇总了C#中JsonReader类的典型用法代码示例。如果您正苦于以下问题:C# JsonReader类的具体用法?C# JsonReader怎么用?C# JsonReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonReader类属于命名空间,在下文中一共展示了JsonReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImportValue
protected override object ImportValue(ImportContext context, JsonReader reader)
{
if (reader == null)
throw new ArgumentNullException("reader");
bool value;
if (reader.TokenClass == JsonTokenClass.Number)
{
try
{
value = Convert.ToInt64(reader.Text, CultureInfo.InvariantCulture) != 0;
}
catch (FormatException e)
{
throw new JsonException(string.Format("The JSON Number {0} must be an integer to be convertible to System.Boolean.", reader.Text), e);
}
}
else if (reader.TokenClass == JsonTokenClass.Boolean)
{
value = reader.Text == JsonBoolean.TrueText;
}
else
{
throw new JsonException(string.Format("Found {0} where expecting a JSON Boolean.", reader.TokenClass));
}
return value ? BooleanObject.True : BooleanObject.False;
}
示例2: ReadJson
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing property value of the JSON that is being converted.</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;
}
else
{
if (reader.TokenType == JsonToken.String)
{
try
{
Version v = new Version((string)reader.Value);
return v;
}
catch (Exception ex)
{
throw JsonSerializationException.Create(reader, "Error parsing version string: {0}".FormatWith(CultureInfo.InvariantCulture, reader.Value), ex);
}
}
else
{
throw JsonSerializationException.Create(reader, "Unexpected token or value when parsing version. Token: {0}, Value: {1}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType, reader.Value));
}
}
}
示例3: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
NameContainer nameContainer = new NameContainer();
nameContainer.Value = (string)reader.Value;
return nameContainer;
}
示例4: ReadAndAssertProperty
private static void ReadAndAssertProperty(JsonReader reader, string propertyName)
{
ReadAndAssert(reader);
if (reader.TokenType != JsonToken.PropertyName || reader.Value.ToString() != propertyName)
throw new JsonSerializationException("Expected JSON property '{0}'.".FormatWith(CultureInfo.InvariantCulture, propertyName));
}
示例5: ReadJson
// Token: 0x06000685 RID: 1669
// RVA: 0x0003758C File Offset: 0x0003578C
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
if (!ReflectionUtils.IsNullable(objectType))
{
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Cannot convert null value to {0}.", CultureInfo.InvariantCulture, objectType));
}
return null;
}
else
{
if (reader.TokenType != JsonToken.StartConstructor || !string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal))
{
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Unexpected token or value when parsing date. Token: {0}, Value: {1}", CultureInfo.InvariantCulture, reader.TokenType, reader.Value));
}
reader.Read();
if (reader.TokenType != JsonToken.Integer)
{
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Unexpected token parsing date. Expected Integer, got {0}.", CultureInfo.InvariantCulture, reader.TokenType));
}
long javaScriptTicks = (long)reader.Value;
DateTime dateTime = DateTimeUtils.ConvertJavaScriptTicksToDateTime(javaScriptTicks);
reader.Read();
if (reader.TokenType != JsonToken.EndConstructor)
{
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Unexpected token parsing date. Expected EndConstructor, got {0}.", CultureInfo.InvariantCulture, reader.TokenType));
}
return dateTime;
}
}
示例6: ReadJson
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <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>The object value.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
DataTable dt = existingValue as DataTable;
if (dt == null)
{
// handle typed datasets
dt = (objectType == typeof(DataTable))
? new DataTable()
: (DataTable)Activator.CreateInstance(objectType);
}
if (reader.TokenType == JsonToken.PropertyName)
{
dt.TableName = (string)reader.Value;
CheckedRead(reader);
}
if (reader.TokenType != JsonToken.StartArray)
throw JsonSerializationException.Create(reader, "Unexpected JSON token when reading DataTable. Expected StartArray, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
CheckedRead(reader);
while (reader.TokenType != JsonToken.EndArray)
{
CreateRow(reader, dt);
CheckedRead(reader);
}
return dt;
}
示例7: ReadJson
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
User user = new User();
user.UserName = (string)reader.Value;
return user;
}
示例8: DeserializeNode
private void DeserializeNode(JsonReader reader, XmlDocument document, XmlNamespaceManager manager, XmlNode currentNode)
{
do
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
string propertyName = reader.Value.ToString();
reader.Read();
if (reader.TokenType == JsonToken.StartArray)
{
while (reader.Read() && reader.TokenType != JsonToken.EndArray)
{
DeserializeValue(reader, document, manager, propertyName, currentNode);
}
}
else
{
DeserializeValue(reader, document, manager, propertyName, currentNode);
}
break;
//case JsonToken.String:
// DeserializeValue(reader, document, manager, TextName, currentNode);
// break;
case JsonToken.EndObject:
case JsonToken.EndArray:
return;
default:
throw new JsonSerializationException("Unexpected JsonToken when deserializing node: " + reader.TokenType);
}
} while (reader.TokenType == JsonToken.PropertyName || reader.Read());
// don't read if current token is a property. token was already read when parsing element attributes
}
示例9: ReadJson
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <returns>The object value.</returns>
public override object ReadJson(JsonReader reader, Type objectType)
{
Type t = (ReflectionUtils.IsNullableType(objectType))
? Nullable.GetUnderlyingType(objectType)
: objectType;
if (reader.TokenType == JsonToken.Null)
{
if (!ReflectionUtils.IsNullable(objectType))
throw new Exception("Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
return null;
}
if (reader.TokenType != JsonToken.String)
throw new Exception("Unexpected token parsing binary. Expected String, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
string encodedData = reader.Value.ToString();
byte[] data = Convert.FromBase64String(encodedData);
if (t == typeof(byte[]))
return data;
#if !SILVERLIGHT && !PocketPC
if (typeof(Binary).IsAssignableFrom(t))
return new Binary(data);
#endif
#if !SILVERLIGHT
if (typeof(SqlBinary).IsAssignableFrom(t))
return new SqlBinary(data);
#endif
throw new Exception("Unexpected object type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, objectType));
}
示例10: ReadJson
// Token: 0x06000181 RID: 385
// RVA: 0x0002B488 File Offset: 0x00029688
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
Type type = ReflectionUtils.IsNullableType(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType;
if (reader.TokenType == JsonToken.Null)
{
if (!ReflectionUtils.IsNullable(objectType))
{
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Cannot convert null value to {0}.", CultureInfo.InvariantCulture, objectType));
}
return null;
}
else
{
byte[] value;
if (reader.TokenType == JsonToken.StartArray)
{
value = this.ReadByteArray(reader);
}
else
{
if (reader.TokenType != JsonToken.String)
{
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Unexpected token parsing binary. Expected String or StartArray, got {0}.", CultureInfo.InvariantCulture, reader.TokenType));
}
string s = reader.Value.ToString();
value = Convert.FromBase64String(s);
}
if (type == typeof(SqlBinary))
{
return new SqlBinary(value);
}
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Unexpected object type when writing binary: {0}", CultureInfo.InvariantCulture, objectType));
}
}
示例11: Read
public override object Read(JsonReader reader)
{
var p = (EnumerableProperty)base.Read(reader);
var length = int.Parse(reader.ReadProperty().Value.ToString());
p.Length = length;
return p;
}
示例12: ReadAndAssertProperty
private static void ReadAndAssertProperty(JsonReader reader, string propertyName)
{
ReadAndAssert(reader);
if (reader.TokenType != JsonToken.PropertyName || !string.Equals(reader.Value.ToString(), propertyName, StringComparison.OrdinalIgnoreCase))
throw new JsonSerializationException("Expected JSON property '{0}'.".FormatWith(CultureInfo.InvariantCulture, propertyName));
}
示例13: Populate
public void Populate(JsonReader reader, object target)
{
ValidationUtils.ArgumentNotNull(target, "target");
Type objectType = target.GetType();
JsonContract contract = _serializer.ContractResolver.ResolveContract(objectType);
if (reader.TokenType == JsonToken.None)
reader.Read();
if (reader.TokenType == JsonToken.StartArray)
{
PopulateList(CollectionUtils.CreateCollectionWrapper(target), reader, null, GetArrayContract(objectType));
}
else if (reader.TokenType == JsonToken.StartObject)
{
CheckedRead(reader);
string id = null;
if (reader.TokenType == JsonToken.PropertyName && string.Equals(reader.Value.ToString(), JsonTypeReflector.IdPropertyName, StringComparison.Ordinal))
{
CheckedRead(reader);
id = reader.Value.ToString();
CheckedRead(reader);
}
if (contract is JsonDictionaryContract)
PopulateDictionary(CollectionUtils.CreateDictionaryWrapper(target), reader, (JsonDictionaryContract) contract, id);
else if (contract is JsonObjectContract)
PopulateObject(target, reader, (JsonObjectContract) contract, id);
else
throw new Exception("dfsdfsdf");
}
}
示例14: ReadJson
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing property value of the JSON that is being converted.</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 t = (ReflectionUtils.IsNullableType(objectType)) ? Nullable.GetUnderlyingType(objectType) : objectType;
if (reader.TokenType == JsonToken.Null)
{
if (!ReflectionUtils.IsNullable(objectType))
throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType));
return null;
}
if (reader.TokenType != JsonToken.StartConstructor || !string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal))
throw JsonSerializationException.Create(reader, "Unexpected token or value when parsing date. Token: {0}, Value: {1}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType, reader.Value));
reader.Read();
if (reader.TokenType != JsonToken.Integer)
throw JsonSerializationException.Create(reader, "Unexpected token parsing date. Expected Integer, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
var ticks = (long)reader.Value;
var d = DateTimeUtils.ConvertJavaScriptTicksToDateTime(ticks);
reader.Read();
if (reader.TokenType != JsonToken.EndConstructor)
throw JsonSerializationException.Create(reader, "Unexpected token parsing date. Expected EndConstructor, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
if (t == typeof(DateTimeOffset))
return new DateTimeOffset(d);
return d;
}
示例15: ImportFromNumber
protected override object ImportFromNumber(ImportContext context, JsonReader reader)
{
Debug.Assert(context != null);
Debug.Assert(reader != null);
string text = reader.Text;
double time;
try
{
time = Convert.ToDouble(text, CultureInfo.InvariantCulture);
}
catch (FormatException e)
{
throw NumberError(e, text);
}
catch (OverflowException e)
{
throw NumberError(e, text);
}
try
{
return ReadReturning(reader, UnixTime.ToDateTime(time));
}
catch (ArgumentException e)
{
throw NumberError(e, text);
}
}