Skip to content

Commit

Permalink
-Fix ReadAsBytes for empty strings
Browse files Browse the repository at this point in the history
-Fix (hopefully) signing merged assemblies
  • Loading branch information
JamesNK committed Apr 20, 2010
1 parent 2f11d3f commit 7085a49
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ function MergeAssembly($dllPrimaryAssembly, $signKey, [string[]]$mergedAssemlies
$temporaryDir = $primary.DirectoryName + "\" + [Guid]::NewGuid().ToString()
New-Item $temporaryDir -ItemType Directory

$ilMergeKeyFile = switch($signAssemblies) { $true { "/keyfile:$signKeyPath" } default { "" } }

try
{
exec { .\Tools\ILMerge\ilmerge.exe "/internalize" "/closed" "/log:$workingDir\$mergedAssemblyName.MergeLog.txt" $ilMergeKeyFile "/out:$temporaryDir\$mergedAssemblyName" $dllPrimaryAssembly $mergeAssemblyPaths } "Error executing ILMerge"
Expand Down
21 changes: 19 additions & 2 deletions Src/Newtonsoft.Json.Tests/Linq/JTokenReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ public void ReadBytesFromString()

TextReader textReader = new StringReader(json);
JsonReader jsonReader = new JsonTextReader(textReader);
//var result = (HasBytes)JsonSerializer.Create(null)
// .Deserialize(jsonReader, typeof(HasBytes));

var jToken = JToken.ReadFrom(jsonReader);

Expand All @@ -273,5 +271,24 @@ public void ReadBytesFromString()

Assert.AreEqual(new byte[] { 1, 2, 3, 4 }, result2.Bytes);
}

[Test]
public void ReadBytesFromEmptyString()
{
var bytes = new HasBytes { Bytes = new byte[0] };
var json = JsonConvert.SerializeObject(bytes);

TextReader textReader = new StringReader(json);
JsonReader jsonReader = new JsonTextReader(textReader);

var jToken = JToken.ReadFrom(jsonReader);

jsonReader = new JTokenReader(jToken);

var result2 = (HasBytes)JsonSerializer.Create(null)
.Deserialize(jsonReader, typeof(HasBytes));

Assert.AreEqual(new byte[0], result2.Bytes);
}
}
}
27 changes: 27 additions & 0 deletions Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3436,5 +3436,32 @@ public void SerializeHashSet()
Assert.IsTrue(d.Contains("III"));
}
#endif

private class MyClass
{
public byte[] Prop1 { get; set; }

public MyClass()
{
Prop1 = new byte[0];
}
}

[Test]
public void DeserializeByteArray()
{
JsonSerializer serializer1 = new JsonSerializer();
serializer1.Converters.Add(new IsoDateTimeConverter());
serializer1.NullValueHandling = NullValueHandling.Ignore;

string json = @"[{""Prop1"":""""},{""Prop1"":""""}]";

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

MyClass[] z = (MyClass[])serializer1.Deserialize(reader, typeof(MyClass[]));
Assert.AreEqual(2, z.Length);
Assert.AreEqual(0, z[0].Prop1.Length);
Assert.AreEqual(0, z[1].Prop1.Length);
}
}
}
12 changes: 10 additions & 2 deletions Src/Newtonsoft.Json/JsonTextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,16 @@ public override byte[] ReadAsBytes()
case '\'':
ReadStringIntoBuffer(currentChar);

byte[] data = Convert.FromBase64CharArray(_buffer.GetInternalBuffer(), 0, _buffer.Position);
_buffer.Position = 0;
byte[] data;
if (_buffer.Position == 0)
{
data = new byte[0];
}
else
{
data = Convert.FromBase64CharArray(_buffer.GetInternalBuffer(), 0, _buffer.Position);
_buffer.Position = 0;
}

SetToken(JsonToken.Bytes, data);

Expand Down
3 changes: 2 additions & 1 deletion Src/Newtonsoft.Json/Linq/JTokenReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public override byte[] ReadAsBytes()
// attempt to convert possible base 64 string to bytes
if (TokenType == JsonToken.String)
{
byte[] data = Convert.FromBase64String((string)Value);
string s = (string) Value;
byte[] data = (s.Length == 0) ? new byte[0] : Convert.FromBase64String(s);
SetToken(JsonToken.Bytes, data);
}

Expand Down

0 comments on commit 7085a49

Please sign in to comment.