Browse Source

Fixed CSHARP-100. Also improved implementation of ArraySerializer.

pull/27/head
rstam 15 years ago
parent
commit
d2b97b5889
  1. 2
      Bson/Bson.csproj
  2. 5
      Bson/DefaultSerializer/BsonDefaultSerializer.cs
  3. 93
      Bson/DefaultSerializer/Serializers/ArraySerializer.cs
  4. 118
      Bson/DefaultSerializer/Serializers/GenericArraySerializer.cs
  5. 6
      DriverOnlineTests/Jira/CSharp100Tests.cs

2
Bson/Bson.csproj

@ -156,7 +156,7 @@
<Compile Include="DefaultSerializer\BsonDefaultSerializer.cs" />
<Compile Include="DefaultSerializer\Serializers\BsonBaseSerializer.cs" />
<Compile Include="DefaultSerializer\Serializers\GeneralEnumSerializer.cs" />
<Compile Include="DefaultSerializer\Serializers\GenericArraySerializer.cs" />
<Compile Include="DefaultSerializer\Serializers\ArraySerializer.cs" />
<Compile Include="DefaultSerializer\Serializers\BsonIBsonSerializableSerializer.cs" />
<Compile Include="Serialization\IBsonIdGenerator.cs" />
<Compile Include="Serialization\IBsonSerializable.cs" />

5
Bson/DefaultSerializer/BsonDefaultSerializer.cs

@ -219,7 +219,10 @@ namespace MongoDB.Bson.DefaultSerializer {
object serializationOptions
) {
if (type.IsArray) {
return GenericArraySerializer.Singleton;
var elementType = type.GetElementType();
var arraySerializerDefinition = typeof(ArraySerializer<>);
var arraySerializerType = arraySerializerDefinition.MakeGenericType(elementType);
return (IBsonSerializer) Activator.CreateInstance(arraySerializerType, serializationOptions);
}
if (type.IsEnum) {

93
Bson/DefaultSerializer/Serializers/ArraySerializer.cs

@ -0,0 +1,93 @@
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
namespace MongoDB.Bson.DefaultSerializer {
public class ArraySerializer<T> : BsonBaseSerializer {
#region constructors
public ArraySerializer() {
}
public ArraySerializer(
object serializationOptions
) {
}
#endregion
#region public methods
public override object Deserialize(
BsonReader bsonReader,
Type nominalType
) {
VerifyType(nominalType);
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null) {
bsonReader.ReadNull();
return null;
} else {
bsonReader.ReadStartArray();
List<T> list = new List<T>();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) {
bsonReader.SkipName();
var element = BsonSerializer.Deserialize<T>(bsonReader);
list.Add(element);
}
bsonReader.ReadEndArray();
return list.ToArray();
}
}
public override void Serialize(
BsonWriter bsonWriter,
Type nominalType,
object value,
bool serializeIdFirst
) {
if (value == null) {
bsonWriter.WriteNull();
} else {
VerifyType(value.GetType());
var array = (T[]) value;
bsonWriter.WriteStartArray();
for (int index = 0; index < array.Length; index++) {
bsonWriter.WriteName(index.ToString());
BsonSerializer.Serialize(bsonWriter, typeof(T), array[index]);
}
bsonWriter.WriteEndArray();
}
}
#endregion
#region private methods
private void VerifyType(
Type type
) {
if (type != typeof(T[])) {
var message = string.Format("ArraySerializer<{0}> cannot be used with type: {1}", typeof(T).FullName, type.FullName);
throw new BsonSerializationException(message);
}
}
#endregion
}
}

118
Bson/DefaultSerializer/Serializers/GenericArraySerializer.cs

@ -1,118 +0,0 @@
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
namespace MongoDB.Bson.DefaultSerializer {
public class GenericArraySerializer : BsonBaseSerializer {
#region private static fields
private static GenericArraySerializer singleton = new GenericArraySerializer();
#endregion
#region constructors
private GenericArraySerializer() {
}
#endregion
#region public static properties
public static GenericArraySerializer Singleton {
get { return singleton; }
}
#endregion
#region public methods
public override object Deserialize(
BsonReader bsonReader,
Type nominalType
) {
VerifyNominalType(nominalType);
var bsonType = bsonReader.CurrentBsonType;
if (bsonType == BsonType.Null) {
bsonReader.ReadNull();
return null;
} else {
var elementType = nominalType.GetElementType();
var deserializeHelperDefinition = this.GetType().GetMethod("DeserializeHelper");
var deserializeHelperInfo = deserializeHelperDefinition.MakeGenericMethod(elementType);
var parameters = new object[] { bsonReader };
var result = deserializeHelperInfo.Invoke(this, parameters);
return result;
}
}
public object DeserializeHelper<TElement>(
BsonReader bsonReader
) {
bsonReader.ReadStartArray();
List<TElement> value = new List<TElement>();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) {
bsonReader.SkipName();
TElement element = BsonSerializer.Deserialize<TElement>(bsonReader);
value.Add(element);
}
bsonReader.ReadEndArray();
return value.ToArray();
}
public override void Serialize(
BsonWriter bsonWriter,
Type nominalType,
object value,
bool serializeIdFirst
) {
VerifyNominalType(nominalType);
if (value == null) {
bsonWriter.WriteNull();
} else {
var elementType = nominalType.GetElementType();
var serializeHelperDefinition = this.GetType().GetMethod("SerializeHelper");
var serializeHelperInfo = serializeHelperDefinition.MakeGenericMethod(elementType);
serializeHelperInfo.Invoke(this, new object[] { bsonWriter, value });
}
}
public void SerializeHelper<TElement>(
BsonWriter bsonWriter,
TElement[] value
) {
bsonWriter.WriteStartArray();
for (int index = 0; index < value.Length; index++) {
bsonWriter.WriteName(index.ToString());
BsonSerializer.Serialize(bsonWriter, typeof(TElement), value[index]);
}
bsonWriter.WriteEndArray();
}
#endregion
#region private methods
private void VerifyNominalType(
Type nominalType
) {
if (!nominalType.IsArray) {
var message = string.Format("GenericArraySerializer cannot be used with type: {0}", nominalType.FullName);
throw new BsonSerializationException(message);
}
}
#endregion
}
}

6
DriverOnlineTests/Jira/CSharp100Tests.cs

@ -26,7 +26,7 @@ using MongoDB.Bson.DefaultSerializer;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
namespace MongoDB.DriverOnlineTests.Jira.CSharpxx {
namespace MongoDB.DriverOnlineTests.Jira.CSharp100 {
[TestFixture]
public class CSharp100Tests {
[DataContract]
@ -50,7 +50,7 @@ namespace MongoDB.DriverOnlineTests.Jira.CSharpxx {
public void TestDeserializationOfTwoBs() {
var server = MongoServer.Create("mongodb://localhost/?safe=true");
var database = server["onlinetests"];
var collection = database["csharpxx"];
var collection = database["csharp100"];
collection.RemoveAll();
var obj = new ChildClass { SomeProperty = null };
@ -61,6 +61,8 @@ namespace MongoDB.DriverOnlineTests.Jira.CSharpxx {
collection.Save(obj, SafeMode.True);
obj = new ChildClass { SomeProperty = new List<SomeClass> { new SomeClass(), new SomeClass() } };
collection.Save(obj, SafeMode.True);
obj = new ChildClass { SomeProperty = new [] { new SomeClass(), new SomeClass() } };
collection.Save(obj, SafeMode.True);
}
}
}
Loading…
Cancel
Save