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


C# JsonTextReader.Read方法代码示例

本文整理汇总了C#中Newtonsoft.Json.JsonTextReader.Read方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader.Read方法的具体用法?C# JsonTextReader.Read怎么用?C# JsonTextReader.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Newtonsoft.Json.JsonTextReader的用法示例。


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

示例1: ProcessMessage

 public void ProcessMessage(string message)
 {
     Console.WriteLine("incoming: " + message);
     JsonReader jsonReader = new JsonTextReader(new StringReader(message));
     string messageHeader = "";
     while (jsonReader.Read())
     {
         if ((jsonReader.TokenType == JsonToken.PropertyName) && (jsonReader.Value.ToString() == "Request"))
         {
             jsonReader.Read();
             messageHeader = jsonReader.Value.ToString();
             break;
         }
     }
     if (messageHeader.Length > 0)
     {
         BaseProcessor processor = GetProcessor(messageHeader);
         if (processor != null)
         {
             Type messageType = GetMessageType(messageHeader);
             if (messageType != null)
             {
                 processor.Process((BaseMessage) JsonConvert.DeserializeObject(message, messageType));
             }
         }
     }
 }
开发者ID:gobixm,项目名称:learn,代码行数:27,代码来源:QueueDirector.cs

示例2: ReadBigInteger

        public void ReadBigInteger()
        {
            string json = @"{
    ParentId: 1,
    ChildId: 333333333333333333333333333333333333333,
}";

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

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType);

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType);

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.Integer, jsonTextReader.TokenType);

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType);

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.Integer, jsonTextReader.TokenType);
            Assert.AreEqual(typeof(BigInteger), jsonTextReader.ValueType);
            Assert.AreEqual(BigInteger.Parse("333333333333333333333333333333333333333"), jsonTextReader.Value);

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType);

            Assert.IsFalse(jsonTextReader.Read());

            JObject o = JObject.Parse(json);
            var i = (BigInteger)((JValue)o["ChildId"]).Value;
            Assert.AreEqual(BigInteger.Parse("333333333333333333333333333333333333333"), i);
        }
开发者ID:NitroXenon,项目名称:LoLSpectX,代码行数:35,代码来源:JsonTextReaderTest.cs

示例3: FloatParseHandling

        public void FloatParseHandling()
        {
            string json = "[1.0,1,9.9,1E-06]";

            JsonTextReader reader = new JsonTextReader(new StringReader(json));
            reader.FloatParseHandling = Json.FloatParseHandling.Decimal;

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.StartArray, reader.TokenType);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(1.0m, reader.Value);
            Assert.AreEqual(typeof(decimal), reader.ValueType);
            Assert.AreEqual(JsonToken.Float, reader.TokenType);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(1L, reader.Value);
            Assert.AreEqual(typeof(long), reader.ValueType);
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(9.9m, reader.Value);
            Assert.AreEqual(typeof(decimal), reader.ValueType);
            Assert.AreEqual(JsonToken.Float, reader.TokenType);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(Convert.ToDecimal(1E-06), reader.Value);
            Assert.AreEqual(typeof(decimal), reader.ValueType);
            Assert.AreEqual(JsonToken.Float, reader.TokenType);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
        }
开发者ID:visi,项目名称:Newtonsoft.Json,代码行数:33,代码来源:JsonTextReaderTest.cs

示例4: ParseDateTimeOffset

        public static DateTimeOffset? ParseDateTimeOffset(JToken value)
        {
            var jValue = value as JValue;
            if (jValue != null)
            {
                if (jValue.Value == null)
                    return null;

                if (jValue.Value is DateTimeOffset)
                    return jValue.Value<DateTimeOffset>();
            }

            var rawValue = value.AsString();

            if (string.IsNullOrWhiteSpace(rawValue))
                return null;

            rawValue = rawValue.Replace("NeoDate", "Date");

            if (!DateRegex.IsMatch(rawValue))
            {
                DateTimeOffset parsed;
                if (!DateTimeOffset.TryParse(rawValue, out parsed))
                    return null;
            }

            var text = string.Format("{{\"a\":\"{0}\"}}", rawValue);
            var reader = new JsonTextReader(new StringReader(text)) {DateParseHandling = DateParseHandling.DateTimeOffset};
            reader.Read(); // JsonToken.StartObject
            reader.Read(); // JsonToken.PropertyName
            return reader.ReadAsDateTimeOffset();
        }
开发者ID:albumprinter,项目名称:Neo4jClient,代码行数:32,代码来源:CommonDeserializerMethods.cs

示例5: ParseCheckpointTagCorrelationId

 public static string ParseCheckpointTagCorrelationId(this string source)
 {
     try
     {
         if (string.IsNullOrEmpty(source))
             return null;
         var reader = new JsonTextReader(new StringReader(source));
         if (!reader.Read()) return null;
         if (reader.TokenType != JsonToken.StartObject) return null;
         while (true)
         {
             CheckpointTag.Check(reader.Read(), reader);
             if (reader.TokenType == JsonToken.EndObject)
                 break;
             if (reader.TokenType != JsonToken.PropertyName) return null;
             var name = (string) reader.Value;
             switch (name)
             {
                 default:
                     if (!reader.Read()) return null;
                     var jToken = JToken.ReadFrom(reader);
                     if (name == "$correlationId")
                         return jToken.ToString();
                     break;
             }
         }
         return null;
     }
     catch (JsonReaderException)
     {
         return null;
     }
 }
开发者ID:jjoergensen,项目名称:EventStore,代码行数:33,代码来源:CheckpointTagExtensions.cs

示例6: SelectOperationInternal

        private string SelectOperationInternal(Message message)
        {
            byte[] rawBody;
            using (XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents()) {
                bodyReader.ReadStartElement("Binary");
                rawBody = bodyReader.ReadContentAsBase64();
            }

            string operation = null;
            using (var bodyReader = new JsonTextReader(new StreamReader(new MemoryStream(rawBody)))) {
                while (bodyReader.Read()) {
                    if (bodyReader.TokenType == JsonToken.PropertyName) {
                        string propertyName = (string)bodyReader.Value;
                        if (propertyName.Equals("method", StringComparison.Ordinal)) {
                            if (!bodyReader.Read() || bodyReader.TokenType != JsonToken.String)
                                throw new InvalidOperationException("Invalid message format.");

                            operation = (string)bodyReader.Value;
                            break;
                        }
                    }
                }
            }

            return operation;
        }
开发者ID:yonglehou,项目名称:JsonRpc.ServiceModel,代码行数:26,代码来源:JsonRpcOperationSelector.cs

示例7: ParseStepsAsync

        private async Task ParseStepsAsync(
            Stream stream,
            RecipeDescriptor descriptor,
            Func<RecipeDescriptor, RecipeStepDescriptor, Task> stepActionAsync)
        {
            var serializer = new JsonSerializer();

            StreamReader streamReader = new StreamReader(stream);
            JsonTextReader reader = new JsonTextReader(streamReader);

            // Go to Steps, then iterate.
            while (reader.Read())
            {
                if (reader.Path == "steps" && reader.TokenType == JsonToken.StartArray)
                {
                    int stepId = 0;
                    while (reader.Read() && reader.Depth > 1)
                    {
                        if (reader.Depth == 2)
                        {
                            var child = JToken.Load(reader);
                            await stepActionAsync(descriptor, new RecipeStepDescriptor
                            {
                                Id = (stepId++).ToString(CultureInfo.InvariantCulture),
                                RecipeName = descriptor.Name,
                                Name = child.Value<string>("name"),
                                Step = child
                            });
                        }
                    }
                }
            }
        }
开发者ID:jchenga,项目名称:Orchard2,代码行数:33,代码来源:JsonRecipeParser.cs

示例8: Deserialize

        public override void Deserialize(Node node, string data)
        {
            TextReader tr = new StringReader(data);
            Dictionary<string, object> properties = new Dictionary<string, object>();

            using(JsonTextReader jsonReader = new JsonTextReader(tr))
            {
                while (jsonReader.Read()){
                    if (jsonReader.TokenType == JsonToken.PropertyName) {
                        string name = jsonReader.Value.ToString();
                        if (jsonReader.Read()) {
                            switch (jsonReader.TokenType) {
                            case JsonToken.String:
                            case JsonToken.Boolean:
                            case JsonToken.Date:
                            case JsonToken.Integer:
                                properties.Add(name, jsonReader.Value);
                                break;
                            case JsonToken.StartArray:
                                List<object> objs = new List<object>();
                                while(jsonReader.Read() && jsonReader.TokenType != JsonToken.EndArray) {
                                    objs.Add(jsonReader.Value);
                                }
                                properties.Add(name, objs.ToArray());
                                break;
                            }

                        }

                    }
                }
            }

            UpdateNode (node, properties);
        }
开发者ID:flipback,项目名称:Galilei,代码行数:35,代码来源:JsonSerializer.cs

示例9: ReadInitialHit

        private void ReadInitialHit(JsonTextReader jsonReader)
        {
            // advance to StartObject
            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.StartObject) ;

            List<object> initialValues = new List<object>();

            // read initial hit until EndObject
            inSource = false;
            while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndObject)
            {
                if (jsonReader.TokenType == JsonToken.PropertyName)
                {
                    if (!inSource && string.CompareOrdinal((string)jsonReader.Value, "_source") == 0)
                    {
                        jsonReader.Read();
                        jsonReader.Read();
                        inSource = true;
                    }

                    _fieldNames.Add((string)jsonReader.Value);

                    jsonReader.Read(); // read value
                    _fieldTypes.Add(jsonReader.ValueType);
                    initialValues.Add(jsonReader.Value);
                }
            }

            currentHit = initialValues.ToArray();
        }
开发者ID:krisdaniels,项目名称:ElasticConnection,代码行数:30,代码来源:HitsParser.cs

示例10: FromJsonBytes

        public static SystemSettings FromJsonBytes(byte[] json)
        {
            using (var reader = new JsonTextReader(new StreamReader(new MemoryStream(json))))
            {
                Check(reader.Read(), reader);
                Check(JsonToken.StartObject, reader);

                StreamAcl userStreamAcl = null;
                StreamAcl systemStreamAcl = null;

                while (true)
                {
                    Check(reader.Read(), reader);
                    if (reader.TokenType == JsonToken.EndObject)
                        break;
                    Check(JsonToken.PropertyName, reader);
                    var name = (string)reader.Value;
                    switch (name)
                    {
                        case SystemMetadata.UserStreamAcl: userStreamAcl = StreamMetadata.ReadAcl(reader); break;
                        case SystemMetadata.SystemStreamAcl: systemStreamAcl = StreamMetadata.ReadAcl(reader); break;
                        default:
                        {
                            Check(reader.Read(), reader);
                            // skip
                            JToken.ReadFrom(reader);
                            break;
                        }
                    }
                }
                return new SystemSettings(userStreamAcl, systemStreamAcl);
            }
        }
开发者ID:thinkbeforecoding,项目名称:EventStore,代码行数:33,代码来源:SystemSettings.cs

示例11: ReadJsonWithInnerObjectTest

        public void ReadJsonWithInnerObjectTest()
        {
            const string json = "{\"properties\":{\"test1\":\"value1\",\"test2\": { \"innertest1\":\"innervalue1\" }}}";
            AttributesTableConverter target = new AttributesTableConverter();
            using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
            {
                JsonSerializer serializer = new JsonSerializer();

                // read start object token and prepare the next token
                reader.Read();
                reader.Read();
                AttributesTable result =
                    (AttributesTable)
                    target.ReadJson(reader, typeof(AttributesTable), new AttributesTable(), serializer);
                Assert.IsFalse(reader.Read()); // read the end of object and ensure there are no more tokens available
                Assert.IsNotNull(result);
                Assert.AreEqual(2, result.Count);
                Assert.AreEqual("value1", result["test1"]);
                Assert.IsNotNull(result["test2"]);
                Assert.IsInstanceOf<IAttributesTable>(result["test2"]);
                IAttributesTable inner = (IAttributesTable)result["test2"];
                Assert.AreEqual(1, inner.Count);
                Assert.AreEqual("innervalue1", inner["innertest1"]);
            }
        }
开发者ID:ste10k41,项目名称:nettopologysuite,代码行数:25,代码来源:AttributesTableConverterTest.cs

示例12: DeserializeRequest

        public void DeserializeRequest(Message message, object[] parameters)
        {
            object bodyFormatProperty;
            if (!message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out bodyFormatProperty) ||
                (bodyFormatProperty as WebBodyFormatMessageProperty).Format != WebContentFormat.Raw)
            {
                throw new InvalidOperationException("Incoming messages must have a body format of Raw. Is a ContentTypeMapper set on the WebHttpBinding?");
            }

            XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
            bodyReader.ReadStartElement("Binary");
            byte[] rawBody = bodyReader.ReadContentAsBase64();
            MemoryStream ms = new MemoryStream(rawBody);

            StreamReader sr = new StreamReader(ms);
            JsonSerializer serializer = new JsonSerializer
            {
                PreserveReferencesHandling = PreserveReferencesHandling.Objects
            };

            if (parameters.Length == 1)
            {
                // single parameter, assuming bare
                parameters[0] = serializer.Deserialize(sr, this.operation.Messages[0].Body.Parts[0].Type);
            }
            else
            {
                // multiple parameter, needs to be wrapped
                JsonReader reader = new JsonTextReader(sr);
                reader.Read();
                if (reader.TokenType != JsonToken.StartObject)
                {
                    throw new InvalidOperationException("Input needs to be wrapped in an object");
                }

                reader.Read();
                while (reader.TokenType == JsonToken.PropertyName)
                {
                    string parameterName = reader.Value as string;
                    reader.Read();
                    if (this.parameterNames.ContainsKey(parameterName))
                    {
                        int parameterIndex = this.parameterNames[parameterName];
                        parameters[parameterIndex] = serializer.Deserialize(reader, this.operation.Messages[0].Body.Parts[parameterIndex].Type);
                    }
                    else
                    {
                        reader.Skip();
                    }

                    reader.Read();
                }

                reader.Close();
            }

            sr.Close();
            ms.Close();
        }
开发者ID:thomasgalliker,项目名称:EmployeeManagement,代码行数:59,代码来源:NewtonsoftJsonDispatchFormatter.cs

示例13: Main

        static void Main(string[] args)
        {
            String ini;
            if (args.Length < 1) {
                Console.WriteLine("Please enter the name of the ini file");
                ini = Console.ReadLine();
            }
            else
                ini = args[0];
            Console.WriteLine(ini);
            var twit = new Twitter(ini);
            //twit.testStream();
            var testCount = 100;
            twit.populateData(testCount);

            while (twit.Data.Count() < testCount - 1) {
                Console.WriteLine(twit.Data.Count());
                System.Threading.Thread.Sleep(5000);
            }
            twit.writeDateToFile(@"C:\Users\Kevin\Documents\TestJson\data.txt");
            //twit.testStream("Disney");

            Tweets data = new Tweets();
            JsonSerializer serializer = new JsonSerializer();
            serializer.NullValueHandling = NullValueHandling.Ignore;
            using (StreamReader sr = new StreamReader(@"C:\Users\Kevin\Documents\TestJson\data.txt"))
            using (JsonTextReader jr = new JsonTextReader(sr)) {
                if(!jr.Read() || jr.TokenType != JsonToken.StartArray) {
                    throw new Exception("Expected start of array");
                }
                while (jr.Read()) {
                    if (jr.TokenType == JsonToken.EndArray) break;
                    var item = serializer.Deserialize<TweetData>(jr);
                    data.add(item);
                }
            }
            Console.WriteLine("done");
            foreach (var tweet in data) {
                Console.WriteLine(tweet.text);
            }

            //twit.getData();
            //while (twit.Data.Length <= 1) ;
            //Console.WriteLine(twit.Data+"\n");

            //Twitter.testStream(ini);
            var news = new News();
            news.getGoogleNews("Disney");
            foreach (var article in news.Data) {
                Console.WriteLine(article.Title+" "+article.publishData.ToString() );
            }
            Console.WriteLine("Done with everything");
            Console.ReadLine();
        }
开发者ID:karashak1,项目名称:ThesisProject,代码行数:54,代码来源:Main.cs

示例14: ReadSingleQuoteInsideDoubleQuoteString

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

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

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

示例15: ParsingQuotedPropertyWithControlCharacters

 public void ParsingQuotedPropertyWithControlCharacters()
 {
     JsonReader reader = new JsonTextReader(new StringReader(@"{'hi\r\nbye':1}"));
     Assert.IsTrue(reader.Read());
     Assert.IsTrue(reader.Read());
     Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
     Assert.AreEqual("hi\r\nbye", reader.Value);
     Assert.IsTrue(reader.Read());
     Assert.AreEqual(JsonToken.Integer, reader.TokenType);
     Assert.AreEqual(1L, reader.Value);
     Assert.IsTrue(reader.Read());
     Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
     Assert.IsFalse(reader.Read());
 }
开发者ID:JamesNK,项目名称:Newtonsoft.Json,代码行数:14,代码来源:ParseTests.cs


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