|
| 1 | +/* Copyright 2010-2013 10gen Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using MongoDB.Bson; |
| 18 | +using MongoDB.Bson.IO; |
| 19 | +using MongoDB.Bson.Serialization; |
| 20 | +using MongoDB.Bson.Serialization.Attributes; |
| 21 | +using MongoDB.Bson.Serialization.Serializers; |
| 22 | +using System.Collections.Generic; |
| 23 | + |
| 24 | +namespace MongoDB.Driver |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// Represents a serializer for a DistinctCommandResult with values of type TValue. |
| 28 | + /// </summary> |
| 29 | + /// <typeparam name="TValue">The type of the value.</typeparam> |
| 30 | + public class DistinctCommandResultSerializer<TValue> : BsonBaseSerializer |
| 31 | + { |
| 32 | + /// <summary> |
| 33 | + /// Deserializes an object from a BsonReader. |
| 34 | + /// </summary> |
| 35 | + /// <param name="bsonReader">The BsonReader.</param> |
| 36 | + /// <param name="nominalType">The nominal type of the object.</param> |
| 37 | + /// <param name="actualType">The actual type of the object.</param> |
| 38 | + /// <param name="options">The serialization options.</param> |
| 39 | + /// <returns> |
| 40 | + /// An object. |
| 41 | + /// </returns> |
| 42 | + public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) |
| 43 | + { |
| 44 | + var response = new BsonDocument(); |
| 45 | + IEnumerable<TValue> values = null; |
| 46 | + |
| 47 | + bsonReader.ReadStartDocument(); |
| 48 | + while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) |
| 49 | + { |
| 50 | + var name = bsonReader.ReadName(); |
| 51 | + if (name == "values") |
| 52 | + { |
| 53 | + var enumerableSerializer = new EnumerableSerializer<TValue>(); |
| 54 | + values = (IEnumerable<TValue>)enumerableSerializer.Deserialize(bsonReader, typeof(List<TValue>), null); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); |
| 59 | + response.Add(name, value); |
| 60 | + } |
| 61 | + } |
| 62 | + bsonReader.ReadEndDocument(); |
| 63 | + |
| 64 | + return new DistinctCommandResult<TValue>(response, values); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments