当前位置: 首页>>代码示例>>C#>>正文


C# JsonReader类代码示例

本文整理汇总了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;
 }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:29,代码来源:BooleanImporter.cs

示例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));
         }
     }
 }
开发者ID:extesla,项目名称:OpenGamingLibrary,代码行数:34,代码来源:VersionConverter.cs

示例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;
        }
开发者ID:JamesNK,项目名称:Newtonsoft.Json,代码行数:7,代码来源:NameContainerConverter.cs

示例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));
    }
开发者ID:Gert-Cominotto,项目名称:MvvmCross,代码行数:7,代码来源:EntityKeyMemberConverter.cs

示例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;
     }
 }
开发者ID:newchild,项目名称:Project-DayZero,代码行数:33,代码来源:JavaScriptDateTimeConverter.cs

示例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;
        }
开发者ID:jrwren,项目名称:yahoofinance,代码行数:41,代码来源:DataTableConverter.cs

示例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;
            }
开发者ID:GorelH,项目名称:ravendb,代码行数:7,代码来源:JsonConverterAttributeClass.cs

示例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
        }
开发者ID:chrisfay,项目名称:EnvatoTracker,代码行数:34,代码来源:XmlNodeConverter.cs

示例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));
        }
开发者ID:jabbo,项目名称:Jabbo,代码行数:40,代码来源:BinaryConverter.cs

示例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));
     }
 }
开发者ID:newchild,项目名称:Project-DayZero,代码行数:36,代码来源:BinaryConverter.cs

示例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;
 }
开发者ID:sethjuarez,项目名称:numl,代码行数:7,代码来源:EnumerablePropertySerializer.cs

示例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));
        }
开发者ID:sat1582,项目名称:CODEFramework,代码行数:7,代码来源:EntityKeyMemberConverter.cs

示例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");
              }
        }
开发者ID:BGCX262,项目名称:zulu-omoto-pos-client-svn-to-git,代码行数:35,代码来源:JsonSerializerReader.cs

示例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;
        }
开发者ID:sat1582,项目名称:CODEFramework,代码行数:42,代码来源:JavaScriptDateTimeConverter.cs

示例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);
            }
        }
开发者ID:atifaziz,项目名称:Jayrock,代码行数:31,代码来源:DateTimeImporter.cs


注:本文中的JsonReader类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。