-Fixed bad BSON when writing long strings of complex UTF8 characters

-Fixed error not being raised for additional content in JSON string for JArray.Parse and JObject.Parse
This commit is contained in:
JamesNK
2011-11-19 03:16:04 +00:00
parent 5831b11de1
commit 9ef17d05a3
13 changed files with 100 additions and 19 deletions
@@ -1243,5 +1243,24 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(new byte[] { 72, 63, 62, 71, 92, 55 }, newObject.Data); Assert.AreEqual(new byte[] { 72, 63, 62, 71, 92, 55 }, newObject.Data);
} }
} }
#if !(WINDOWS_PHONE || SILVERLIGHT || NET20 || NET35)
public void Utf8Text()
{
string badText =System.IO.File.ReadAllText(@"PoisonText.txt");
var j = new JObject();
j["test"] = badText;
var memoryStream = new MemoryStream();
var bsonWriter = new BsonWriter(memoryStream);
j.WriteTo(bsonWriter);
bsonWriter.Flush();
memoryStream.Position = 0;
JObject o = JObject.Load(new BsonReader(memoryStream));
Assert.AreEqual(badText, (string)o["test"]);
}
#endif
} }
} }
@@ -448,5 +448,18 @@ Parameter name: index")]
Assert.AreEqual(123, (int)array[0]); Assert.AreEqual(123, (int)array[0]);
Assert.AreEqual(456, (int)array[1]); Assert.AreEqual(456, (int)array[1]);
} }
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected state: Finished. Line 5, position 3.")]
public void ParseAdditionalContent()
{
string json = @"[
""Small"",
""Medium"",
""Large""
], 987987";
JArray.Parse(json);
}
} }
} }
@@ -1624,5 +1624,23 @@ Parameter name: arrayIndex")]
Assert.AreEqual("9065acf3-c820-467d-be50-8d4664beaf35", v.ToString()); Assert.AreEqual("9065acf3-c820-467d-be50-8d4664beaf35", v.ToString());
} }
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected state: Finished. Line 10, position 3.")]
public void ParseAdditionalContent()
{
string json = @"{
""Name"": ""Apple"",
""Expiry"": new Date(1230422400000),
""Price"": 3.99,
""Sizes"": [
""Small"",
""Medium"",
""Large""
]
}, 987987";
JObject o = JObject.Parse(json);
}
} }
} }
@@ -739,5 +739,18 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.IsTrue(v1.DeepEquals(v2)); Assert.IsTrue(v1.DeepEquals(v2));
} }
[Test]
[ExpectedException(typeof(JsonReaderException), ExpectedMessage = "Unexpected state: Finished. Line 5, position 3.")]
public void ParseAdditionalContent()
{
string json = @"[
""Small"",
""Medium"",
""Large""
], 987987";
JToken.Parse(json);
}
} }
} }
@@ -306,6 +306,9 @@
<Content Include="bunny_pancake.jpg"> <Content Include="bunny_pancake.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
<Content Include="PoisonText.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5"> <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
@@ -66,5 +66,5 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.3.0")] [assembly: AssemblyVersion("4.0.3.0")]
#if !PocketPC #if !PocketPC
[assembly: AssemblyFileVersion("4.0.3.14302")] [assembly: AssemblyFileVersion("4.0.3.14319")]
#endif #endif
@@ -5046,6 +5046,7 @@ keyword such as type of business.""
} }
} }
#if !(SILVERLIGHT || WINDOWS_PHONE || NET20)
[Test] [Test]
public void DeserializeDecimalsWithCulture() public void DeserializeDecimalsWithCulture()
{ {
@@ -5071,6 +5072,7 @@ keyword such as type of business.""
Thread.CurrentThread.CurrentUICulture = initialCulture; Thread.CurrentThread.CurrentUICulture = initialCulture;
} }
} }
#endif
} }
public class DecimalTestClass public class DecimalTestClass
+11 -13
View File
@@ -10,7 +10,7 @@ namespace Newtonsoft.Json.Bson
{ {
internal class BsonBinaryWriter internal class BsonBinaryWriter
{ {
private static readonly Encoding Encoding = Encoding.UTF8; private static readonly Encoding Encoding = new UTF8Encoding(false);
private readonly BinaryWriter _writer; private readonly BinaryWriter _writer;
@@ -169,12 +169,19 @@ namespace Newtonsoft.Json.Bson
if (calculatedlengthPrefix != null) if (calculatedlengthPrefix != null)
_writer.Write(calculatedlengthPrefix.Value); _writer.Write(calculatedlengthPrefix.Value);
WriteUtf8Bytes(s, byteCount);
_writer.Write((byte)0);
}
public void WriteUtf8Bytes(string s, int byteCount)
{
if (s != null) if (s != null)
{ {
if (_largeByteBuffer == null) if (_largeByteBuffer == null)
{ {
_largeByteBuffer = new byte[256]; _largeByteBuffer = new byte[256];
_maxChars = 256/Encoding.GetMaxByteCount(1); _maxChars = 256 / Encoding.GetMaxByteCount(1);
} }
if (byteCount <= 256) if (byteCount <= 256)
{ {
@@ -183,19 +190,10 @@ namespace Newtonsoft.Json.Bson
} }
else else
{ {
int charCount; byte[] bytes = Encoding.GetBytes(s);
int totalCharsWritten = 0; _writer.Write(bytes);
for (int i = s.Length; i > 0; i -= charCount)
{
charCount = (i > _maxChars) ? _maxChars : i;
int count = Encoding.GetBytes(s, totalCharsWritten, charCount, _largeByteBuffer, 0);
_writer.Write(_largeByteBuffer, 0, count);
totalCharsWritten += charCount;
}
} }
} }
_writer.Write((byte)0);
} }
private int CalculateSize(int stringByteCount) private int CalculateSize(int stringByteCount)
@@ -81,7 +81,7 @@ namespace Newtonsoft.Json.Converters
if (CamelCaseText) if (CamelCaseText)
{ {
var names = resolvedEnumName.Split(',').Select(item => StringUtils.ToCamelCase(item.Trim())); string[] names = resolvedEnumName.Split(',').Select(item => StringUtils.ToCamelCase(item.Trim())).ToArray();
resolvedEnumName = string.Join(", ", names); resolvedEnumName = string.Join(", ", names);
} }
+6 -1
View File
@@ -136,7 +136,12 @@ namespace Newtonsoft.Json.Linq
{ {
JsonReader jsonReader = new JsonTextReader(new StringReader(json)); JsonReader jsonReader = new JsonTextReader(new StringReader(json));
return Load(jsonReader); JArray a = Load(jsonReader);
if (jsonReader.Read() && jsonReader.TokenType != JsonToken.Comment)
throw new Exception("Additional text found in JSON string after parsing content.");
return a;
} }
/// <summary> /// <summary>
+6 -1
View File
@@ -342,7 +342,12 @@ namespace Newtonsoft.Json.Linq
{ {
JsonReader jsonReader = new JsonTextReader(new StringReader(json)); JsonReader jsonReader = new JsonTextReader(new StringReader(json));
return Load(jsonReader); JObject o = Load(jsonReader);
if (jsonReader.Read() && jsonReader.TokenType != JsonToken.Comment)
throw new Exception("Additional text found in JSON string after parsing content.");
return o;
} }
/// <summary> /// <summary>
+6 -1
View File
@@ -1229,7 +1229,12 @@ namespace Newtonsoft.Json.Linq
{ {
JsonReader jsonReader = new JsonTextReader(new StringReader(json)); JsonReader jsonReader = new JsonTextReader(new StringReader(json));
return Load(jsonReader); JToken t = Load(jsonReader);
if (jsonReader.Read() && jsonReader.TokenType != JsonToken.Comment)
throw new Exception("Additional text found in JSON string after parsing content.");
return t;
} }
/// <summary> /// <summary>
@@ -85,7 +85,7 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.3.0")] [assembly: AssemblyVersion("4.0.3.0")]
#if !PocketPC #if !PocketPC
[assembly: AssemblyFileVersion("4.0.3.14302")] [assembly: AssemblyFileVersion("4.0.3.14319")]
#endif #endif
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]