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.Equal(@"Forest's Bakery And Cafe", jsonTextReader.Value); }
public void ReadBigInteger() { string json = @"{ ParentId: 1, ChildId: 333333333333333333333333333333333333333, }"; JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); Assert.True(jsonTextReader.Read()); Assert.Equal(JsonToken.StartObject, jsonTextReader.TokenType); Assert.True(jsonTextReader.Read()); Assert.Equal(JsonToken.PropertyName, jsonTextReader.TokenType); Assert.True(jsonTextReader.Read()); Assert.Equal(JsonToken.Integer, jsonTextReader.TokenType); Assert.True(jsonTextReader.Read()); Assert.Equal(JsonToken.PropertyName, jsonTextReader.TokenType); Assert.True(jsonTextReader.Read()); Assert.Equal(JsonToken.Integer, jsonTextReader.TokenType); Assert.Equal(typeof(BigInteger), jsonTextReader.ValueType); Assert.Equal(BigInteger.Parse("333333333333333333333333333333333333333"), jsonTextReader.Value); Assert.True(jsonTextReader.Read()); Assert.Equal(JsonToken.EndObject, jsonTextReader.TokenType); Assert.False(jsonTextReader.Read()); JObject o = JObject.Parse(json); var i = (BigInteger)((JValue)o["ChildId"]).Value; Assert.Equal(BigInteger.Parse("333333333333333333333333333333333333333"), i); }
public void ReadMultilineString() { string json = @"""first line second line third line"""; JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); Assert.True(jsonTextReader.Read()); Assert.Equal(JsonToken.String, jsonTextReader.TokenType); Assert.Equal(@"first line second line third line", jsonTextReader.Value); }
public void ParseAdditionalContent_WhitespaceThenText() { string json = @"'hi' a"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); AssertException.Throws<JsonReaderException>(() => { while (reader.Read()) { } }, "Additional text encountered after finished reading JSON content: a. Path '', line 1, position 5."); }
private string ReadString(string input) { MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(@"""" + input + @"""")); JsonTextReader reader = new JsonTextReader(new StreamReader(ms)); reader.Read(); string s = (string)reader.Value; return s; }
public void AppendCharsWhileReadingNewLine() { string json = @" { ""description"": ""A person"", ""type"": ""object"", ""properties"": { ""name"": {""type"":""string""}, ""hobbies"": { ""type"": ""array"", ""items"": {""type"":""string""} } } } "; JsonTextReader reader = new JsonTextReader(new StringReader(json)); #if DEBUG reader.SetCharBuffer(new char[129]); #endif for (int i = 0; i < 14; i++) { Assert.True(reader.Read()); } Assert.True(reader.Read()); Assert.Equal(JsonToken.PropertyName, reader.TokenType); Assert.Equal("type", reader.Value); }
public void FloatParseHandling_NaN() { string json = "[NaN]"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); reader.FloatParseHandling = Json.FloatParseHandling.Decimal; Assert.True(reader.Read()); Assert.Equal(JsonToken.StartArray, reader.TokenType); AssertException.Throws<JsonReaderException>(() => reader.Read(), "Cannot read NaN as a decimal."); }
public void NullCharReading() { string json = "\0{\0'\0h\0i\0'\0:\0[\01\0,\0'\0'\0\0,\0null\0]\0,\0do\0:true\0}\0\0/*\0sd\0f\0*/\0/*\0sd\0f\0*/ \0"; JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); Assert.True(reader.Read()); Assert.Equal(JsonToken.StartObject, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.PropertyName, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.StartArray, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.Integer, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.String, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.Null, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.EndArray, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.PropertyName, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.Boolean, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.EndObject, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.Comment, reader.TokenType); Assert.Equal("\0sd\0f\0", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.Comment, reader.TokenType); Assert.Equal("\0sd\0f\0", reader.Value); Assert.False(reader.Read()); }
public void ReadInt32Overflow_Negative() { long i = int.MinValue; JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture))); reader.Read(); Assert.Equal(typeof(long), reader.ValueType); Assert.Equal(i, reader.Value); for (int j = 1; j < 1000; j++) { long total = -j + i; AssertException.Throws<JsonReaderException>(() => { reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture))); reader.ReadAsInt32(); }, "JSON integer " + total + " is too large or small for an Int32. Path '', line 1, position 11."); } }
public void ReadingFromSlowStream() { string json = "[false, true, true, false, 'test!', 1.11, 0e-10, 0E-10, 0.25e-5, 0.3e10, 6.0221418e23, 'Purple\\r \\n monkey\\'s:\\tdishwasher']"; JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); Assert.True(reader.Read()); Assert.True(reader.Read()); Assert.Equal(false, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.Boolean, reader.TokenType); Assert.Equal(true, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.Boolean, reader.TokenType); Assert.Equal(true, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.Boolean, reader.TokenType); Assert.Equal(false, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.String, reader.TokenType); Assert.Equal("test!", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.Float, reader.TokenType); Assert.Equal(1.11d, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.Float, reader.TokenType); Assert.Equal(0d, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.Float, reader.TokenType); Assert.Equal(0d, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.Float, reader.TokenType); Assert.Equal(0.0000025d, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.Float, reader.TokenType); Assert.Equal(3000000000d, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.Float, reader.TokenType); Assert.Equal(602214180000000000000000d, reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.String, reader.TokenType); Assert.Equal(reader.Value, "Purple\r \n monkey's:\tdishwasher"); Assert.True(reader.Read()); }
public void ReadLongString() { string s = new string('a', 10000); JsonReader reader = new JsonTextReader(new StringReader("'" + s + "'")); reader.Read(); Assert.Equal(s, reader.Value); }
public void SurrogatePairValid() { string json = @"{ ""MATHEMATICAL ITALIC CAPITAL ALPHA"": ""\uD835\uDEE2"" }"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); Assert.True(reader.Read()); Assert.True(reader.Read()); Assert.True(reader.Read()); Assert.Equal(JsonToken.String, reader.TokenType); string s = reader.Value.ToString(); Assert.Equal(2, s.Length); StringInfo stringInfo = new StringInfo(s); Assert.Equal(1, stringInfo.LengthInTextElements); }
public void ReadingIndented() { string input = @"{ CPU: 'Intel', Drives: [ 'DVD read/writer', ""500 gigabyte hard drive"" ] }"; StringReader sr = new StringReader(input); using (JsonTextReader jsonReader = new JsonTextReader(sr)) { #if DEBUG jsonReader.SetCharBuffer(new char[5]); #endif Assert.Equal(jsonReader.TokenType, JsonToken.None); Assert.Equal(0, jsonReader.LineNumber); Assert.Equal(0, jsonReader.LinePosition); jsonReader.Read(); Assert.Equal(jsonReader.TokenType, JsonToken.StartObject); Assert.Equal(1, jsonReader.LineNumber); Assert.Equal(1, jsonReader.LinePosition); jsonReader.Read(); Assert.Equal(jsonReader.TokenType, JsonToken.PropertyName); Assert.Equal(jsonReader.Value, "CPU"); Assert.Equal(2, jsonReader.LineNumber); Assert.Equal(7, jsonReader.LinePosition); jsonReader.Read(); Assert.Equal(JsonToken.String, jsonReader.TokenType); Assert.Equal("Intel", jsonReader.Value); Assert.Equal(2, jsonReader.LineNumber); Assert.Equal(15, jsonReader.LinePosition); jsonReader.Read(); Assert.Equal(jsonReader.TokenType, JsonToken.PropertyName); Assert.Equal(jsonReader.Value, "Drives"); Assert.Equal(3, jsonReader.LineNumber); Assert.Equal(10, jsonReader.LinePosition); jsonReader.Read(); Assert.Equal(jsonReader.TokenType, JsonToken.StartArray); Assert.Equal(3, jsonReader.LineNumber); Assert.Equal(12, jsonReader.LinePosition); jsonReader.Read(); Assert.Equal(jsonReader.TokenType, JsonToken.String); Assert.Equal(jsonReader.Value, "DVD read/writer"); Assert.Equal(jsonReader.QuoteChar, '\''); Assert.Equal(4, jsonReader.LineNumber); Assert.Equal(22, jsonReader.LinePosition); jsonReader.Read(); Assert.Equal(jsonReader.TokenType, JsonToken.String); Assert.Equal(jsonReader.Value, "500 gigabyte hard drive"); Assert.Equal(jsonReader.QuoteChar, '"'); Assert.Equal(5, jsonReader.LineNumber); Assert.Equal(30, jsonReader.LinePosition); jsonReader.Read(); Assert.Equal(jsonReader.TokenType, JsonToken.EndArray); Assert.Equal(6, jsonReader.LineNumber); Assert.Equal(4, jsonReader.LinePosition); jsonReader.Read(); Assert.Equal(jsonReader.TokenType, JsonToken.EndObject); Assert.Equal(7, jsonReader.LineNumber); Assert.Equal(2, jsonReader.LinePosition); Assert.False(jsonReader.Read()); } }
public void EscapedPathInExceptionMessage() { string json = @"{ ""frameworks"": { ""aspnetcore50"": { ""dependencies"": { ""System.Xml.ReaderWriter"": { ""source"": !!! !!! } } } } }"; AssertException.Throws<JsonReaderException>( () => { JsonTextReader reader = new JsonTextReader(new StringReader(json)); while (reader.Read()) { } }, "Unexpected character encountered while parsing value: !. Path 'frameworks.aspnetcore50.dependencies.['System.Xml.ReaderWriter'].source', line 6, position 21."); }
public void WriteReadBoundaryDecimals() { StringWriter sw = new StringWriter(); JsonTextWriter writer = new JsonTextWriter(sw); writer.WriteStartArray(); writer.WriteValue(decimal.MaxValue); writer.WriteValue(decimal.MinValue); writer.WriteEndArray(); string json = sw.ToString(); StringReader sr = new StringReader(json); JsonTextReader reader = new JsonTextReader(sr); Assert.True(reader.Read()); decimal? max = reader.ReadAsDecimal(); Assert.Equal(decimal.MaxValue, max); decimal? min = reader.ReadAsDecimal(); Assert.Equal(decimal.MinValue, min); Assert.True(reader.Read()); }
public void ResetJsonTextReaderErrorCount() { ToggleReaderError toggleReaderError = new ToggleReaderError(new StringReader("{'first':1,'second':2,'third':3}")); JsonTextReader jsonTextReader = new JsonTextReader(toggleReaderError); Assert.True(jsonTextReader.Read()); toggleReaderError.Error = true; AssertException.Throws<Exception>(() => jsonTextReader.Read(), "Read error"); AssertException.Throws<Exception>(() => jsonTextReader.Read(), "Read error"); toggleReaderError.Error = false; Assert.True(jsonTextReader.Read()); Assert.Equal("first", jsonTextReader.Value); toggleReaderError.Error = true; AssertException.Throws<Exception>(() => jsonTextReader.Read(), "Read error"); toggleReaderError.Error = false; Assert.True(jsonTextReader.Read()); Assert.Equal(1L, jsonTextReader.Value); toggleReaderError.Error = true; AssertException.Throws<Exception>(() => jsonTextReader.Read(), "Read error"); AssertException.Throws<Exception>(() => jsonTextReader.Read(), "Read error"); AssertException.Throws<Exception>(() => jsonTextReader.Read(), "Read error"); toggleReaderError.Error = false; //a reader use to skip to the end after 3 errors in a row //Assert.False(jsonTextReader.Read()); }
public void UnescapeDoubleQuotes() { string json = @"{""recipe_id"":""12"",""recipe_name"":""Apocalypse Leather Armors"",""recipe_text"":""#C16------------------------------\r\n#C12Ingredients #C20\r\n#C16------------------------------\r\n\r\na piece of Leather Armor\r\n( ie #L \""Enhanced Leather Armor Boots\"" \""85644\"" )\r\n<img src=rdb:\/\/13264>\r\n\r\n#L \""Hacker Tool\"" \""87814\""\r\n<img src=rdb:\/\/99282>\r\n\r\n#L \""Clanalizer\"" \""208313\""\r\n<img src=rdb:\/\/156479>\r\n\r\n#C16------------------------------\r\n#C12Recipe #C16\r\n#C16------------------------------#C20\r\n\r\nHacker Tool\r\n#C15+#C20\r\na piece of Leather Armor\r\n#C15=#C20\r\n<img src=rdb:\/\/13264>\r\na piece of Hacked Leather Armor\r\n( ie : #L \""Hacked Leather Armor Boots\"" \""245979\"" )\r\n#C16Skills: | BE |#C20\r\n\r\n#C14------------------------------#C20\r\n\r\nClanalizer\r\n#C15+#C20\r\na piece of Hacked Leather Armor\r\n#C15=#C20\r\n<img src=rdb:\/\/13264>\r\na piece of Apocalypse Leather Armor\r\n( ie : #L \""Apocalypse Leather Armor Boots\"" \""245966\"" )\r\n#C16Skills: | ?? |#C20\r\n\r\n#C16------------------------------\r\n#C12Details#C16\r\n#C16------------------------------#C20\r\n\r\n#L \""Apocalypse Leather Armor Boots\"" \""245967\""\r\n#L \""Apocalypse Leather Armor Gloves\"" \""245969\""\r\n#L \""Apocalypse Leather Armor Helmet\"" \""245975\""\r\n#L \""Apocalypse Leather Armor Pants\"" \""245971\""\r\n#L \""Apocalypse Leather Armor Sleeves\"" \""245973\""\r\n#L \""Apocalypse Leather Body Armor\"" \""245965\""\r\n\r\n#C16------------------------------\r\n#C12Comments#C16\r\n#C16------------------------------#C20\r\n\r\nNice froob armor.. but ugleh!\r\n\r\n"",""recipe_author"":null}"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); Assert.True(reader.Read()); Assert.Equal(JsonToken.StartObject, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.PropertyName, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.String, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.PropertyName, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.String, reader.TokenType); Assert.True(reader.Read()); Assert.Equal(JsonToken.PropertyName, reader.TokenType); Assert.Equal("recipe_text", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.String, reader.TokenType); Assert.Equal("#C16------------------------------\r\n#C12Ingredients #C20\r\n#C16------------------------------\r\n\r\na piece of Leather Armor\r\n( ie #L \"Enhanced Leather Armor Boots\" \"85644\" )\r\n<img src=rdb://13264>\r\n\r\n#L \"Hacker Tool\" \"87814\"\r\n<img src=rdb://99282>\r\n\r\n#L \"Clanalizer\" \"208313\"\r\n<img src=rdb://156479>\r\n\r\n#C16------------------------------\r\n#C12Recipe #C16\r\n#C16------------------------------#C20\r\n\r\nHacker Tool\r\n#C15+#C20\r\na piece of Leather Armor\r\n#C15=#C20\r\n<img src=rdb://13264>\r\na piece of Hacked Leather Armor\r\n( ie : #L \"Hacked Leather Armor Boots\" \"245979\" )\r\n#C16Skills: | BE |#C20\r\n\r\n#C14------------------------------#C20\r\n\r\nClanalizer\r\n#C15+#C20\r\na piece of Hacked Leather Armor\r\n#C15=#C20\r\n<img src=rdb://13264>\r\na piece of Apocalypse Leather Armor\r\n( ie : #L \"Apocalypse Leather Armor Boots\" \"245966\" )\r\n#C16Skills: | ?? |#C20\r\n\r\n#C16------------------------------\r\n#C12Details#C16\r\n#C16------------------------------#C20\r\n\r\n#L \"Apocalypse Leather Armor Boots\" \"245967\"\r\n#L \"Apocalypse Leather Armor Gloves\" \"245969\"\r\n#L \"Apocalypse Leather Armor Helmet\" \"245975\"\r\n#L \"Apocalypse Leather Armor Pants\" \"245971\"\r\n#L \"Apocalypse Leather Armor Sleeves\" \"245973\"\r\n#L \"Apocalypse Leather Body Armor\" \"245965\"\r\n\r\n#C16------------------------------\r\n#C12Comments#C16\r\n#C16------------------------------#C20\r\n\r\nNice froob armor.. but ugleh!\r\n\r\n", reader.Value); }
public void DateParseHandling() { string json = @"[""1970-01-01T00:00:00Z"",""\/Date(0)\/""]"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); reader.DateParseHandling = Json.DateParseHandling.DateTime; Assert.True(reader.Read()); Assert.True(reader.Read()); Assert.Equal(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value); Assert.Equal(typeof(DateTime), reader.ValueType); Assert.True(reader.Read()); Assert.Equal(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value); Assert.Equal(typeof(DateTime), reader.ValueType); Assert.True(reader.Read()); #if !NET20 reader = new JsonTextReader(new StringReader(json)); reader.DateParseHandling = Json.DateParseHandling.DateTimeOffset; Assert.True(reader.Read()); Assert.True(reader.Read()); Assert.Equal(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value); Assert.Equal(typeof(DateTimeOffset), reader.ValueType); Assert.True(reader.Read()); Assert.Equal(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value); Assert.Equal(typeof(DateTimeOffset), reader.ValueType); Assert.True(reader.Read()); #endif reader = new JsonTextReader(new StringReader(json)); reader.DateParseHandling = Json.DateParseHandling.None; Assert.True(reader.Read()); Assert.True(reader.Read()); Assert.Equal(@"1970-01-01T00:00:00Z", reader.Value); Assert.Equal(typeof(string), reader.ValueType); Assert.True(reader.Read()); Assert.Equal(@"/Date(0)/", reader.Value); Assert.Equal(typeof(string), reader.ValueType); Assert.True(reader.Read()); #if !NET20 reader = new JsonTextReader(new StringReader(json)); reader.DateParseHandling = Json.DateParseHandling.DateTime; Assert.True(reader.Read()); reader.ReadAsDateTimeOffset(); Assert.Equal(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value); Assert.Equal(typeof(DateTimeOffset), reader.ValueType); reader.ReadAsDateTimeOffset(); Assert.Equal(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value); Assert.Equal(typeof(DateTimeOffset), reader.ValueType); Assert.True(reader.Read()); reader = new JsonTextReader(new StringReader(json)); reader.DateParseHandling = Json.DateParseHandling.DateTimeOffset; Assert.True(reader.Read()); reader.ReadAsDateTime(); Assert.Equal(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value); Assert.Equal(typeof(DateTime), reader.ValueType); reader.ReadAsDateTime(); Assert.Equal(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value); Assert.Equal(typeof(DateTime), reader.ValueType); Assert.True(reader.Read()); #endif }
public void Depth() { string input = @"{ value:'Purple', array:[1,2,new Date(1)], subobject:{prop:1,proparray:[1]} }"; StringReader sr = new StringReader(input); using (JsonReader reader = new JsonTextReader(sr)) { Assert.Equal(0, reader.Depth); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.StartObject); Assert.Equal(0, reader.Depth); Assert.Equal("", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.PropertyName); Assert.Equal(1, reader.Depth); Assert.Equal("value", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.String); Assert.Equal(reader.Value, @"Purple"); Assert.Equal(reader.QuoteChar, '\''); Assert.Equal(1, reader.Depth); Assert.Equal("value", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.PropertyName); Assert.Equal(1, reader.Depth); Assert.Equal("array", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.StartArray); Assert.Equal(1, reader.Depth); Assert.Equal("array", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.Integer); Assert.Equal(1L, reader.Value); Assert.Equal(2, reader.Depth); Assert.Equal("array[0]", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.Integer); Assert.Equal(2L, reader.Value); Assert.Equal(2, reader.Depth); Assert.Equal("array[1]", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.StartConstructor); Assert.Equal("Date", reader.Value); Assert.Equal(2, reader.Depth); Assert.Equal("array[2]", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.Integer); Assert.Equal(1L, reader.Value); Assert.Equal(3, reader.Depth); Assert.Equal("array[2][0]", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.EndConstructor); Assert.Equal(null, reader.Value); Assert.Equal(2, reader.Depth); Assert.Equal("array[2]", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.EndArray); Assert.Equal(1, reader.Depth); Assert.Equal("array", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.PropertyName); Assert.Equal(1, reader.Depth); Assert.Equal("subobject", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.StartObject); Assert.Equal(1, reader.Depth); Assert.Equal("subobject", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.PropertyName); Assert.Equal(2, reader.Depth); Assert.Equal("subobject.prop", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.Integer); Assert.Equal(2, reader.Depth); Assert.Equal("subobject.prop", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.PropertyName); Assert.Equal(2, reader.Depth); Assert.Equal("subobject.proparray", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.StartArray); Assert.Equal(2, reader.Depth); Assert.Equal("subobject.proparray", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.Integer); Assert.Equal(3, reader.Depth); Assert.Equal("subobject.proparray[0]", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.EndArray); Assert.Equal(2, reader.Depth); Assert.Equal("subobject.proparray", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.EndObject); Assert.Equal(1, reader.Depth); Assert.Equal("subobject", reader.Path); reader.Read(); Assert.Equal(reader.TokenType, JsonToken.EndObject); Assert.Equal(0, reader.Depth); Assert.Equal("", reader.Path); } }
public void YahooFinance() { string input = @"{ ""matches"" : [ {""t"":""C"", ""n"":""Citigroup Inc."", ""e"":""NYSE"", ""id"":""662713""} ,{""t"":""CHL"", ""n"":""China Mobile Ltd. (ADR)"", ""e"":""NYSE"", ""id"":""660998""} ,{""t"":""PTR"", ""n"":""PetroChina Company Limited (ADR)"", ""e"":""NYSE"", ""id"":""664536""} ,{""t"":""RIO"", ""n"":""Companhia Vale do Rio Doce (ADR)"", ""e"":""NYSE"", ""id"":""671472""} ,{""t"":""RIOPR"", ""n"":""Companhia Vale do Rio Doce (ADR)"", ""e"":""NYSE"", ""id"":""3512643""} ,{""t"":""CSCO"", ""n"":""Cisco Systems, Inc."", ""e"":""NASDAQ"", ""id"":""99624""} ,{""t"":""CVX"", ""n"":""Chevron Corporation"", ""e"":""NYSE"", ""id"":""667226""} ,{""t"":""TM"", ""n"":""Toyota Motor Corporation (ADR)"", ""e"":""NYSE"", ""id"":""655880""} ,{""t"":""JPM"", ""n"":""JPMorgan Chase \\x26 Co."", ""e"":""NYSE"", ""id"":""665639""} ,{""t"":""COP"", ""n"":""ConocoPhillips"", ""e"":""NYSE"", ""id"":""1691168""} ,{""t"":""LFC"", ""n"":""China Life Insurance Company Ltd. (ADR)"", ""e"":""NYSE"", ""id"":""688679""} ,{""t"":""NOK"", ""n"":""Nokia Corporation (ADR)"", ""e"":""NYSE"", ""id"":""657729""} ,{""t"":""KO"", ""n"":""The Coca-Cola Company"", ""e"":""NYSE"", ""id"":""6550""} ,{""t"":""VZ"", ""n"":""Verizon Communications Inc."", ""e"":""NYSE"", ""id"":""664887""} ,{""t"":""AMX"", ""n"":""America Movil S.A.B de C.V. (ADR)"", ""e"":""NYSE"", ""id"":""665834""}], ""all"" : false } "; using (JsonReader jsonReader = new JsonTextReader(new StringReader(input))) { while (jsonReader.Read()) { Console.WriteLine(jsonReader.Value); } } }
public void UnexpectedEndOfString() { JsonReader reader = new JsonTextReader(new StringReader("'hi")); AssertException.Throws<JsonReaderException>(() => { reader.Read(); }, "Unterminated string. Expected delimiter: '. Path '', line 1, position 3."); }
public void ReadConstructor() { string json = @"{""DefaultConverter"":new Date(0, ""hi""),""MemberConverter"":""1970-01-01T00:00:00Z""}"; JsonReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); Assert.True(reader.Read()); Assert.True(reader.Read()); Assert.True(reader.Read()); Assert.Equal(JsonToken.StartConstructor, reader.TokenType); Assert.Equal("Date", reader.Value); Assert.True(reader.Read()); Assert.Equal(0L, reader.Value); Assert.True(reader.Read()); Assert.Equal("hi", reader.Value); Assert.True(reader.Read()); Assert.Equal(JsonToken.EndConstructor, reader.TokenType); Assert.True(reader.Read()); Assert.Equal("MemberConverter", reader.Value); }
public void ReadLongJsonArray() { int valueCount = 10000; StringWriter sw = new StringWriter(); JsonTextWriter writer = new JsonTextWriter(sw); writer.WriteStartArray(); for (int i = 0; i < valueCount; i++) { writer.WriteValue(i); } writer.WriteEndArray(); string json = sw.ToString(); JsonTextReader reader = new JsonTextReader(new StringReader(json)); Assert.True(reader.Read()); for (int i = 0; i < valueCount; i++) { Assert.True(reader.Read()); Assert.Equal((long)i, reader.Value); } Assert.True(reader.Read()); Assert.False(reader.Read()); }
public void ParseAdditionalContent_Comma() { string json = @"[ ""Small"", ""Medium"", ""Large"" ],"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); AssertException.Throws<JsonReaderException>(() => { while (reader.Read()) { } }, "Additional text encountered after finished reading JSON content: ,. Path '', line 5, position 2."); }
public void AppendCharsWhileReadingNull() { string json = @"[ { ""$id"": ""1"", ""Name"": ""e1"", ""Manager"": null }, { ""$id"": ""2"", ""Name"": ""e2"", ""Manager"": null }, { ""$ref"": ""1"" }, { ""$ref"": ""2"" } ]"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); #if DEBUG reader.SetCharBuffer(new char[129]); #endif for (int i = 0; i < 15; i++) { reader.Read(); } reader.Read(); Assert.Equal(JsonToken.Null, reader.TokenType); }
public void ParseAdditionalContent_Text() { string json = @"[ ""Small"", ""Medium"", ""Large"" ]content"; JsonTextReader reader = new JsonTextReader(new StringReader(json)); #if DEBUG reader.SetCharBuffer(new char[2]); #endif reader.Read(); Assert.Equal(1, reader.LineNumber); reader.Read(); Assert.Equal(2, reader.LineNumber); reader.Read(); Assert.Equal(3, reader.LineNumber); reader.Read(); Assert.Equal(4, reader.LineNumber); reader.Read(); Assert.Equal(5, reader.LineNumber); AssertException.Throws<JsonReaderException>(() => { reader.Read(); }, "Additional text encountered after finished reading JSON content: c. Path '', line 5, position 2."); }
public void ReadInt64Overflow_Negative() { BigInteger i = new BigInteger(long.MinValue); JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture))); reader.Read(); Assert.Equal(typeof(long), reader.ValueType); for (int j = 1; j < 1000; j++) { BigInteger total = i + -j; reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture))); reader.Read(); Assert.Equal(typeof(BigInteger), reader.ValueType); } }
public void ParseAdditionalContent_Whitespace() { string json = @"[ ""Small"", ""Medium"", ""Large"" ] "; JsonTextReader reader = new JsonTextReader(new StringReader(json)); while (reader.Read()) { } }
public void WriteToken() { JsonTextReader reader = new JsonTextReader(new StringReader("[1,2,3,4,5]")); reader.Read(); reader.Read(); StringWriter sw = new StringWriter(); JsonTextWriter writer = new JsonTextWriter(sw); writer.WriteToken(reader); Assert.Equal("1", sw.ToString()); }
public void MaxDepthDoesNotRecursivelyError() { string json = "[[[[]]],[[]]]"; JsonTextReader reader = new JsonTextReader(new StringReader(json)) { MaxDepth = 1 }; Assert.True(reader.Read()); Assert.Equal(0, reader.Depth); AssertException.Throws<JsonReaderException>(() => { Assert.True(reader.Read()); }, "The reader's MaxDepth of 1 has been exceeded. Path '[0]', line 1, position 2."); Assert.Equal(1, reader.Depth); Assert.True(reader.Read()); Assert.Equal(2, reader.Depth); Assert.True(reader.Read()); Assert.Equal(3, reader.Depth); Assert.True(reader.Read()); Assert.Equal(3, reader.Depth); Assert.True(reader.Read()); Assert.Equal(2, reader.Depth); Assert.True(reader.Read()); Assert.Equal(1, reader.Depth); AssertException.Throws<JsonReaderException>(() => { Assert.True(reader.Read()); }, "The reader's MaxDepth of 1 has been exceeded. Path '[1]', line 1, position 9."); Assert.Equal(1, reader.Depth); Assert.True(reader.Read()); Assert.Equal(2, reader.Depth); Assert.True(reader.Read()); Assert.Equal(2, reader.Depth); Assert.True(reader.Read()); Assert.Equal(1, reader.Depth); Assert.True(reader.Read()); Assert.Equal(0, reader.Depth); Assert.False(reader.Read()); }