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


C# JsonTextReader类代码示例

本文整理汇总了C#中JsonTextReader的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader类的具体用法?C# JsonTextReader怎么用?C# JsonTextReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


JsonTextReader类属于命名空间,在下文中一共展示了JsonTextReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Example

        public void Example()
        {
            #region Usage
            // person.json, has a relative external schema reference 'address.json'
            // --------
            // {
            //   'type': 'object',
            //   'properties': {
            //     'name': {'type':'string'},
            //     'addresses': {
            //       'type': 'array',
            //       'items': {'$ref': 'address.json'}
            //     }
            //   }
            // }
            // --------

            using (StreamReader file = File.OpenText(@"c:\person.json"))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                JSchemaUrlResolver resolver = new JSchemaUrlResolver();

                JSchema schema = JSchema.Load(reader, new JSchemaReaderSettings
                {
                    Resolver = resolver,
                    // where the schema is being loaded from
                    // referenced 'address.json' schema will be loaded from disk at 'c:\address.json'
                    BaseUri = new Uri(@"c:\person.json")
                });

                // validate JSON
            }
            #endregion
        }
开发者ID:Pondidum,项目名称:Newtonsoft.Json.Schema,代码行数:34,代码来源:JSchemaUrlResolverRelative.cs

示例2: ReadFrom

    public void ReadFrom()
    {
      JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(new StringReader("{'pie':true}")));
      Assert.AreEqual(true, (bool)o["pie"]);

      JArray a = (JArray)JToken.ReadFrom(new JsonTextReader(new StringReader("[1,2,3]")));
      Assert.AreEqual(1, (int)a[0]);
      Assert.AreEqual(2, (int)a[1]);
      Assert.AreEqual(3, (int)a[2]);

      JsonReader reader = new JsonTextReader(new StringReader("{'pie':true}"));
      reader.Read();
      reader.Read();

      JProperty p = (JProperty)JToken.ReadFrom(reader);
      Assert.AreEqual("pie", p.Name);
      Assert.AreEqual(true, (bool)p.Value);

      JConstructor c = (JConstructor)JToken.ReadFrom(new JsonTextReader(new StringReader("new Date(1)")));
      Assert.AreEqual("Date", c.Name);
      Assert.IsTrue(JToken.DeepEquals(new JValue(1), c.Values().ElementAt(0)));

      JValue v;

      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"""stringvalue""")));
      Assert.AreEqual("stringvalue", (string)v);

      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1")));
      Assert.AreEqual(1, (int)v);

      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1.1")));
      Assert.AreEqual(1.1, (double)v);
    }
开发者ID:adrianhewitt,项目名称:to-Screenshot-Generator,代码行数:33,代码来源:JTokenTests.cs

示例3: Example

        public void Example()
        {
            #region Usage
            string json = @"{ 'name': 'Admin' }{ 'name': 'Publisher' }";

            IList<Role> roles = new List<Role>();

            JsonTextReader reader = new JsonTextReader(new StringReader(json));
            reader.SupportMultipleContent = true;

            while (true)
            {
                if (!reader.Read())
                    break;

                JsonSerializer serializer = new JsonSerializer();
                Role role = serializer.Deserialize<Role>(reader);

                roles.Add(role);
            }

            foreach (Role role in roles)
            {
                Console.WriteLine(role.Name);
            }

            // Admin
            // Publisher
            #endregion

            Assert.AreEqual(2, roles.Count);
            Assert.AreEqual("Admin", roles[0].Name);
            Assert.AreEqual("Publisher", roles[1].Name);
        }
开发者ID:b-bot-110,项目名称:Newtonsoft.Json,代码行数:34,代码来源:ReadMultipleContentWithJsonReader.cs

示例4: ReadLargeJson

        public static void ReadLargeJson()
        {
            using (var jsonFile = System.IO.File.OpenText("large.json"))
            using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Deserialize<IList<PerformanceTests.RootObject>>(jsonTextReader);
            }

            Console.WriteLine("Press any key to start deserialization");
            Console.ReadKey();
            Console.WriteLine("Deserializing...");

            Stopwatch sw = new Stopwatch();
            sw.Start();

            for (int i = 0; i < 5; i++)
            {
                using (var jsonFile = System.IO.File.OpenText("large.json"))
                using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Deserialize<IList<PerformanceTests.RootObject>>(jsonTextReader);
                }
            }

            sw.Stop();

            Console.WriteLine("Finished. Total seconds: " + sw.Elapsed.TotalSeconds);
        }
开发者ID:NTUST-PTL,项目名称:PTL-Project,代码行数:30,代码来源:Program.cs

示例5: AssertImport

 private static void AssertImport(object expected, string input)
 {
     JsonTextReader reader = new JsonTextReader(new StringReader(input));
     Type expectedType = expected.GetType();
     object o = JsonImporterStock.Get(expectedType).Import(reader);
     Assert.IsInstanceOfType(expectedType, o);
     Assert.AreEqual(expected, o);
 }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:8,代码来源:TestNumberImporter.cs

示例6: CheckInnerReader

    public void CheckInnerReader()
    {
      string json = "{'name':'James','hobbies':['pie','cake']}";
      JsonReader reader = new JsonTextReader(new StringReader(json));

      JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
      Assert.AreEqual(reader, validatingReader.Reader);
    }
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:8,代码来源:JsonValidatingReaderTests.cs

示例7: AssertImport

 private static void AssertImport(object expected, string input)
 {
     JsonTextReader reader = new JsonTextReader(new StringReader(input));
     Type expectedType = expected.GetType();
     ImportContext context = new ImportContext();
     object o = context.Import(expectedType, reader);
     Assert.IsInstanceOfType(expectedType, o);
     Assert.AreEqual(expected, o);
 }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:9,代码来源:TestNumberImporter.cs

示例8: Load

        public void Load()
        {
            JsonReader reader = new JsonTextReader(new StringReader("new Date(123)"));
            reader.Read();

            JConstructor constructor = JConstructor.Load(reader);
            Assert.AreEqual("Date", constructor.Name);
            Assert.IsTrue(JToken.DeepEquals(new JValue(123), constructor.Values().ElementAt(0)));
        }
开发者ID:JamesNK,项目名称:Newtonsoft.Json,代码行数:9,代码来源:JConstructorTests.cs

示例9: ReaderValidation

        private void ReaderValidation()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader(Json));
            JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
            validatingReader.Schema = Schema;

            while (validatingReader.Read())
            {
            }
        }
开发者ID:Pondidum,项目名称:Newtonsoft.Json.Schema,代码行数:10,代码来源:PerformanceTests.cs

示例10: AssertImport

 private static void AssertImport(object expected, string input)
 {
     JsonTextReader reader = new JsonTextReader(new StringReader(input));
     GuidImporter importer = new GuidImporter();
     object o = importer.Import(new ImportContext(), reader);
     Assert.IsTrue(reader.EOF, "Reader must be at EOF.");
     if (expected != null)
         Assert.IsInstanceOfType(typeof(Guid), o);
     Assert.AreEqual(expected, o);
 }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:10,代码来源:TestGuidImporter.cs

示例11: Example

    public void Example()
    {
      JObject o1 = JObject.Parse(File.ReadAllText(@"c:\videogames.json"));

      // read JSON directly from a file
      using (StreamReader file = File.OpenText(@"c:\videogames.json"))
      using (JsonTextReader reader = new JsonTextReader(file))
      {
        JObject o2 = (JObject) JToken.ReadFrom(reader);
      }
    }
开发者ID:che3tah7,项目名称:Newtonsoft.Json,代码行数:11,代码来源:ReadJson.cs

示例12: CanReadComma

        public void CanReadComma()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader(","));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(JsonToken.Comma, token);
        }
开发者ID:cosullivan,项目名称:JsonLite,代码行数:11,代码来源:JsonTextReaderFacts.cs

示例13: CanReadDecimal

        public void CanReadDecimal()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader("123.456"));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(new JsonToken(JsonTokenKind.Fractional, "123.456"), token);
        }
开发者ID:cosullivan,项目名称:JsonLite,代码行数:11,代码来源:JsonTextReaderFacts.cs

示例14: CanReadBooleanTrue

        public void CanReadBooleanTrue()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader("true"));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(JsonToken.True, token);
        }
开发者ID:cosullivan,项目名称:JsonLite,代码行数:11,代码来源:JsonTextReaderFacts.cs

示例15: ReadDollarQuoteStringWithTag

        public void ReadDollarQuoteStringWithTag()
        {
            string json = @"{""NameOfStore"":$tag$Forest's Bakery And Cafe$tag$}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }
开发者ID:alanthinker,项目名称:Newtonsoft.Json,代码行数:11,代码来源:DollarQuoteStringTest.cs


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