|
|
@@ -1,4 +1,4 @@
|
|
|
-/* Copyright 2010-2014 MongoDB Inc.
|
|
|
+/* Copyright 2010-2016 MongoDB Inc.
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
@@ -14,7 +14,7 @@
|
|
|
*/
|
|
|
|
|
|
using System;
|
|
|
-using System.IO;
|
|
|
+using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using MongoDB.Bson.Serialization;
|
|
|
using MongoDB.Bson.Serialization.Serializers;
|
|
|
@@ -24,15 +24,16 @@ namespace MongoDB.Bson.IO
|
|
|
/// <summary>
|
|
|
/// Represents a BSON writer for some external format (see subclasses).
|
|
|
/// </summary>
|
|
|
- public abstract class BsonWriter : IDisposable
|
|
|
+ public abstract class BsonWriter : IBsonWriter
|
|
|
{
|
|
|
// private fields
|
|
|
+ private Func<IElementNameValidator> _childElementNameValidatorFactory = () => NoOpElementNameValidator.Instance;
|
|
|
private bool _disposed = false;
|
|
|
+ private IElementNameValidator _elementNameValidator = NoOpElementNameValidator.Instance;
|
|
|
+ private Stack<IElementNameValidator> _elementNameValidatorStack = new Stack<IElementNameValidator>();
|
|
|
private BsonWriterSettings _settings;
|
|
|
private BsonWriterState _state;
|
|
|
private string _name;
|
|
|
- private bool _checkElementNames;
|
|
|
- private bool _checkUpdateDocument;
|
|
|
private int _serializationDepth;
|
|
|
|
|
|
// constructors
|
|
|
@@ -52,24 +53,6 @@ namespace MongoDB.Bson.IO
|
|
|
}
|
|
|
|
|
|
// public properties
|
|
|
- /// <summary>
|
|
|
- /// Gets or sets whether to check element names (no periods or leading $).
|
|
|
- /// </summary>
|
|
|
- public bool CheckElementNames
|
|
|
- {
|
|
|
- get { return _checkElementNames; }
|
|
|
- set { _checkElementNames = value; }
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Gets or sets whether to check an update document (turns CheckElementNames on if first element name does *not* start with $).
|
|
|
- /// </summary>
|
|
|
- public bool CheckUpdateDocument
|
|
|
- {
|
|
|
- get { return _checkUpdateDocument; }
|
|
|
- set { _checkUpdateDocument = value; }
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Gets the current serialization depth.
|
|
|
/// </summary>
|
|
|
@@ -114,100 +97,6 @@ namespace MongoDB.Bson.IO
|
|
|
}
|
|
|
|
|
|
// public static methods
|
|
|
- /// <summary>
|
|
|
- /// Creates a BsonWriter to a BsonBuffer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="settings">Optional BsonBinaryWriterSettings.</param>
|
|
|
- /// <returns>A BsonWriter.</returns>
|
|
|
- public static BsonWriter Create(BsonBinaryWriterSettings settings)
|
|
|
- {
|
|
|
- return new BsonBinaryWriter(null, null, settings);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Creates a BsonWriter to a BsonBuffer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="buffer">A BsonBuffer.</param>
|
|
|
- /// <returns>A BsonWriter.</returns>
|
|
|
- public static BsonWriter Create(BsonBuffer buffer)
|
|
|
- {
|
|
|
- return new BsonBinaryWriter(null, buffer, BsonBinaryWriterSettings.Defaults);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Creates a BsonWriter to a BsonBuffer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="buffer">A BsonBuffer.</param>
|
|
|
- /// <param name="settings">Optional BsonBinaryWriterSettings.</param>
|
|
|
- /// <returns>A BsonWriter.</returns>
|
|
|
- public static BsonWriter Create(BsonBuffer buffer, BsonBinaryWriterSettings settings)
|
|
|
- {
|
|
|
- return new BsonBinaryWriter(null, buffer, settings);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Creates a BsonWriter to a BsonDocument.
|
|
|
- /// </summary>
|
|
|
- /// <param name="document">A BsonDocument.</param>
|
|
|
- /// <returns>A BsonWriter.</returns>
|
|
|
- public static BsonWriter Create(BsonDocument document)
|
|
|
- {
|
|
|
- return Create(document, BsonDocumentWriterSettings.Defaults);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Creates a BsonWriter to a BsonDocument.
|
|
|
- /// </summary>
|
|
|
- /// <param name="document">A BsonDocument.</param>
|
|
|
- /// <param name="settings">The settings.</param>
|
|
|
- /// <returns>A BsonWriter.</returns>
|
|
|
- public static BsonWriter Create(BsonDocument document, BsonDocumentWriterSettings settings)
|
|
|
- {
|
|
|
- return new BsonDocumentWriter(document, settings);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Creates a BsonWriter to a BSON Stream.
|
|
|
- /// </summary>
|
|
|
- /// <param name="stream">A Stream.</param>
|
|
|
- /// <returns>A BsonWriter.</returns>
|
|
|
- public static BsonWriter Create(Stream stream)
|
|
|
- {
|
|
|
- return Create(stream, BsonBinaryWriterSettings.Defaults);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Creates a BsonWriter to a BSON Stream.
|
|
|
- /// </summary>
|
|
|
- /// <param name="stream">A Stream.</param>
|
|
|
- /// <param name="settings">Optional BsonBinaryWriterSettings.</param>
|
|
|
- /// <returns>A BsonWriter.</returns>
|
|
|
- public static BsonWriter Create(Stream stream, BsonBinaryWriterSettings settings)
|
|
|
- {
|
|
|
- return new BsonBinaryWriter(stream, null, settings);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Creates a BsonWriter to a JSON TextWriter.
|
|
|
- /// </summary>
|
|
|
- /// <param name="writer">A TextWriter.</param>
|
|
|
- /// <returns>A BsonWriter.</returns>
|
|
|
- public static BsonWriter Create(TextWriter writer)
|
|
|
- {
|
|
|
- return new JsonWriter(writer, JsonWriterSettings.Defaults);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Creates a BsonWriter to a JSON TextWriter.
|
|
|
- /// </summary>
|
|
|
- /// <param name="writer">A TextWriter.</param>
|
|
|
- /// <param name="settings">Optional JsonWriterSettings.</param>
|
|
|
- /// <returns>A BsonWriter.</returns>
|
|
|
- public static BsonWriter Create(TextWriter writer, JsonWriterSettings settings)
|
|
|
- {
|
|
|
- return new JsonWriter(writer, settings);
|
|
|
- }
|
|
|
-
|
|
|
// public methods
|
|
|
/// <summary>
|
|
|
/// Closes the writer.
|
|
|
@@ -232,80 +121,36 @@ namespace MongoDB.Bson.IO
|
|
|
public abstract void Flush();
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Writes BSON binary data to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="binaryData">The binary data.</param>
|
|
|
- public abstract void WriteBinaryData(BsonBinaryData binaryData);
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON binary data element to the writer.
|
|
|
+ /// Pops the element name validator.
|
|
|
/// </summary>
|
|
|
- /// <param name="bytes">The binary data.</param>
|
|
|
- /// <param name="subType">The binary data subtype.</param>
|
|
|
- [Obsolete("Use WriteBinaryData(BsonBinaryData binaryData) instead.")]
|
|
|
- public void WriteBinaryData(byte[] bytes, BsonBinarySubType subType)
|
|
|
+ /// <returns>The popped element validator.</returns>
|
|
|
+ public void PopElementNameValidator()
|
|
|
{
|
|
|
- var guidRepresentation = (subType == BsonBinarySubType.UuidStandard) ? GuidRepresentation.Standard : GuidRepresentation.Unspecified;
|
|
|
- WriteBinaryData(bytes, subType, guidRepresentation);
|
|
|
+ _elementNameValidator = _elementNameValidatorStack.Pop();
|
|
|
+ _childElementNameValidatorFactory = () => _elementNameValidator;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Writes BSON binary data to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="bytes">The binary data.</param>
|
|
|
- /// <param name="subType">The binary data subtype.</param>
|
|
|
- /// <param name="guidRepresentation">The respresentation for Guids.</param>
|
|
|
- [Obsolete("Use WriteBinaryData(BsonBinaryData binaryData) instead.")]
|
|
|
- public void WriteBinaryData(
|
|
|
- byte[] bytes,
|
|
|
- BsonBinarySubType subType,
|
|
|
- GuidRepresentation guidRepresentation)
|
|
|
- {
|
|
|
- var binaryData = new BsonBinaryData(bytes, subType, guidRepresentation);
|
|
|
- WriteBinaryData(binaryData);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON binary data element to the writer.
|
|
|
+ /// Pushes the element name validator.
|
|
|
/// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="binaryData">The binary data.</param>
|
|
|
- public void WriteBinaryData(string name, BsonBinaryData binaryData)
|
|
|
+ /// <param name="validator">The validator.</param>
|
|
|
+ public void PushElementNameValidator(IElementNameValidator validator)
|
|
|
{
|
|
|
- WriteName(name);
|
|
|
- WriteBinaryData(binaryData);
|
|
|
- }
|
|
|
+ if (validator == null)
|
|
|
+ {
|
|
|
+ throw new ArgumentNullException("validator");
|
|
|
+ }
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON binary data element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="bytes">The binary data.</param>
|
|
|
- /// <param name="subType">The binary data subtype.</param>
|
|
|
- [Obsolete("Use WriteBinaryData(string name, BsonBinaryData binaryData) instead.")]
|
|
|
- public void WriteBinaryData(string name, byte[] bytes, BsonBinarySubType subType)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteBinaryData(bytes, subType);
|
|
|
+ _elementNameValidatorStack.Push(_elementNameValidator);
|
|
|
+ _elementNameValidator = validator;
|
|
|
+ _childElementNameValidatorFactory = () => _elementNameValidator;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// Writes a BSON binary data element to the writer.
|
|
|
+ /// Writes BSON binary data to the writer.
|
|
|
/// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="bytes">The binary data.</param>
|
|
|
- /// <param name="subType">The binary data subtype.</param>
|
|
|
- /// <param name="guidRepresentation">The representation for Guids.</param>
|
|
|
- [Obsolete("Use WriteBinaryData(string name, BsonBinaryData binaryData) instead.")]
|
|
|
- public void WriteBinaryData(
|
|
|
- string name,
|
|
|
- byte[] bytes,
|
|
|
- BsonBinarySubType subType,
|
|
|
- GuidRepresentation guidRepresentation)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteBinaryData(bytes, subType, guidRepresentation);
|
|
|
- }
|
|
|
+ /// <param name="binaryData">The binary data.</param>
|
|
|
+ public abstract void WriteBinaryData(BsonBinaryData binaryData);
|
|
|
|
|
|
/// <summary>
|
|
|
/// Writes a BSON Boolean to the writer.
|
|
|
@@ -313,50 +158,20 @@ namespace MongoDB.Bson.IO
|
|
|
/// <param name="value">The Boolean value.</param>
|
|
|
public abstract void WriteBoolean(bool value);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON Boolean element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="value">The Boolean value.</param>
|
|
|
- public void WriteBoolean(string name, bool value)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteBoolean(value);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes BSON binary data to the writer.
|
|
|
/// </summary>
|
|
|
/// <param name="bytes">The bytes.</param>
|
|
|
public abstract void WriteBytes(byte[] bytes);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON binary data element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="bytes">The bytes.</param>
|
|
|
- public void WriteBytes(string name, byte[] bytes)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteBytes(bytes);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a BSON DateTime to the writer.
|
|
|
/// </summary>
|
|
|
/// <param name="value">The number of milliseconds since the Unix epoch.</param>
|
|
|
public abstract void WriteDateTime(long value);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON DateTime element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="value">The number of milliseconds since the Unix epoch.</param>
|
|
|
- public void WriteDateTime(string name, long value)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteDateTime(value);
|
|
|
- }
|
|
|
+ /// <inheritdoc />
|
|
|
+ public abstract void WriteDecimal128(Decimal128 value);
|
|
|
|
|
|
/// <summary>
|
|
|
/// Writes a BSON Double to the writer.
|
|
|
@@ -364,17 +179,6 @@ namespace MongoDB.Bson.IO
|
|
|
/// <param name="value">The Double value.</param>
|
|
|
public abstract void WriteDouble(double value);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON Double element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="value">The Double value.</param>
|
|
|
- public void WriteDouble(string name, double value)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteDouble(value);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes the end of a BSON array to the writer.
|
|
|
/// </summary>
|
|
|
@@ -389,6 +193,8 @@ namespace MongoDB.Bson.IO
|
|
|
public virtual void WriteEndDocument()
|
|
|
{
|
|
|
_serializationDepth--;
|
|
|
+
|
|
|
+ PopElementNameValidator();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -397,98 +203,34 @@ namespace MongoDB.Bson.IO
|
|
|
/// <param name="value">The Int32 value.</param>
|
|
|
public abstract void WriteInt32(int value);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON Int32 element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="value">The Int32 value.</param>
|
|
|
- public void WriteInt32(string name, int value)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteInt32(value);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a BSON Int64 to the writer.
|
|
|
/// </summary>
|
|
|
/// <param name="value">The Int64 value.</param>
|
|
|
public abstract void WriteInt64(long value);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON Int64 element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="value">The Int64 value.</param>
|
|
|
- public void WriteInt64(string name, long value)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteInt64(value);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a BSON JavaScript to the writer.
|
|
|
/// </summary>
|
|
|
/// <param name="code">The JavaScript code.</param>
|
|
|
public abstract void WriteJavaScript(string code);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON JavaScript element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="code">The JavaScript code.</param>
|
|
|
- public void WriteJavaScript(string name, string code)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteJavaScript(code);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a BSON JavaScript to the writer (call WriteStartDocument to start writing the scope).
|
|
|
/// </summary>
|
|
|
/// <param name="code">The JavaScript code.</param>
|
|
|
public abstract void WriteJavaScriptWithScope(string code);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON JavaScript element to the writer (call WriteStartDocument to start writing the scope).
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="code">The JavaScript code.</param>
|
|
|
- public void WriteJavaScriptWithScope(string name, string code)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteJavaScriptWithScope(code);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a BSON MaxKey to the writer.
|
|
|
/// </summary>
|
|
|
public abstract void WriteMaxKey();
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON MaxKey element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- public void WriteMaxKey(string name)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteMaxKey();
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a BSON MinKey to the writer.
|
|
|
/// </summary>
|
|
|
public abstract void WriteMinKey();
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON MinKey element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- public void WriteMinKey(string name)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteMinKey();
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes the name of an element to the writer.
|
|
|
/// </summary>
|
|
|
@@ -501,14 +243,20 @@ namespace MongoDB.Bson.IO
|
|
|
}
|
|
|
if (name.IndexOf('\0') != -1)
|
|
|
{
|
|
|
- throw new ArgumentException("Element names cannot contain nulls.", "name");
|
|
|
+ throw new BsonSerializationException("Element names cannot contain nulls.");
|
|
|
}
|
|
|
if (_disposed) { throw new ObjectDisposedException(this.GetType().Name); }
|
|
|
if (_state != BsonWriterState.Name)
|
|
|
{
|
|
|
ThrowInvalidState("WriteName", BsonWriterState.Name);
|
|
|
}
|
|
|
- CheckElementName(name);
|
|
|
+
|
|
|
+ if (!_elementNameValidator.IsValidElementName(name))
|
|
|
+ {
|
|
|
+ var message = string.Format("Element name '{0}' is not valid'.", name);
|
|
|
+ throw new BsonSerializationException(message);
|
|
|
+ }
|
|
|
+ _childElementNameValidatorFactory = () => _elementNameValidator.GetValidatorForChildContent(name);
|
|
|
|
|
|
_name = name;
|
|
|
_state = BsonWriterState.Value;
|
|
|
@@ -519,131 +267,69 @@ namespace MongoDB.Bson.IO
|
|
|
/// </summary>
|
|
|
public abstract void WriteNull();
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON null element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- public void WriteNull(string name)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteNull();
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a BSON ObjectId to the writer.
|
|
|
/// </summary>
|
|
|
/// <param name="objectId">The ObjectId.</param>
|
|
|
public abstract void WriteObjectId(ObjectId objectId);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON ObjectId to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="timestamp">The timestamp.</param>
|
|
|
- /// <param name="machine">The machine hash.</param>
|
|
|
- /// <param name="pid">The PID.</param>
|
|
|
- /// <param name="increment">The increment.</param>
|
|
|
- [Obsolete("Use WriteObjectId(ObjectId objectId) instead.")]
|
|
|
- public void WriteObjectId(int timestamp, int machine, short pid, int increment)
|
|
|
- {
|
|
|
- var objectId = new ObjectId(timestamp, machine, pid, increment);
|
|
|
- WriteObjectId(objectId);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON ObjectId element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="objectId">The ObjectId.</param>
|
|
|
- public void WriteObjectId(string name, ObjectId objectId)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteObjectId(objectId);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON ObjectId element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="timestamp">The timestamp.</param>
|
|
|
- /// <param name="machine">The machine hash.</param>
|
|
|
- /// <param name="pid">The PID.</param>
|
|
|
- /// <param name="increment">The increment.</param>
|
|
|
- [Obsolete("Use WriteObjectId(string name, ObjectId objectId) instead.")]
|
|
|
- public void WriteObjectId(string name, int timestamp, int machine, short pid, int increment)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteObjectId(timestamp, machine, pid, increment);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a raw BSON array.
|
|
|
/// </summary>
|
|
|
/// <param name="slice">The byte buffer containing the raw BSON array.</param>
|
|
|
public virtual void WriteRawBsonArray(IByteBuffer slice)
|
|
|
{
|
|
|
- // overridden in BsonBinaryWriter
|
|
|
+ // overridden in BsonBinaryWriter to write the raw bytes to the stream
|
|
|
+ // for all other streams, deserialize the raw bytes and serialize the resulting array instead
|
|
|
|
|
|
- using (var bsonBuffer = new BsonBuffer())
|
|
|
+ using (var chunkSource = new InputBufferChunkSource(BsonChunkPool.Default))
|
|
|
+ using (var buffer = new MultiChunkBuffer(chunkSource))
|
|
|
+ using (var stream = new ByteBufferStream(buffer))
|
|
|
{
|
|
|
- BsonArray array;
|
|
|
-
|
|
|
// wrap the array in a fake document so we can deserialize it
|
|
|
- var arrayLength = slice.Length;
|
|
|
- var documentLength = arrayLength + 8;
|
|
|
- bsonBuffer.WriteInt32(documentLength);
|
|
|
- bsonBuffer.WriteByte((byte)BsonType.Array);
|
|
|
- bsonBuffer.WriteByte((byte)'x');
|
|
|
- bsonBuffer.WriteByte((byte)0);
|
|
|
- bsonBuffer.ByteBuffer.WriteBytes(slice);
|
|
|
- bsonBuffer.WriteByte((byte)0);
|
|
|
-
|
|
|
- bsonBuffer.Position = 0;
|
|
|
- using (var bsonReader = new BsonBinaryReader(bsonBuffer, true, BsonBinaryReaderSettings.Defaults))
|
|
|
+ var documentLength = slice.Length + 8;
|
|
|
+ buffer.EnsureCapacity(documentLength);
|
|
|
+ stream.WriteInt32(documentLength);
|
|
|
+ stream.WriteBsonType(BsonType.Array);
|
|
|
+ stream.WriteByte((byte)'x');
|
|
|
+ stream.WriteByte(0);
|
|
|
+ stream.WriteSlice(slice);
|
|
|
+ stream.WriteByte(0);
|
|
|
+ buffer.MakeReadOnly();
|
|
|
+
|
|
|
+ stream.Position = 0;
|
|
|
+ using (var reader = new BsonBinaryReader(stream, BsonBinaryReaderSettings.Defaults))
|
|
|
{
|
|
|
- bsonReader.ReadStartDocument();
|
|
|
- bsonReader.ReadName("x");
|
|
|
- array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
|
|
|
- bsonReader.ReadEndDocument();
|
|
|
+ var deserializationContext = BsonDeserializationContext.CreateRoot(reader);
|
|
|
+ reader.ReadStartDocument();
|
|
|
+ reader.ReadName("x");
|
|
|
+ var array = BsonArraySerializer.Instance.Deserialize(deserializationContext);
|
|
|
+ reader.ReadEndDocument();
|
|
|
+
|
|
|
+ var serializationContext = BsonSerializationContext.CreateRoot(this);
|
|
|
+ BsonArraySerializer.Instance.Serialize(serializationContext, array);
|
|
|
}
|
|
|
-
|
|
|
- BsonArraySerializer.Instance.Serialize(this, typeof(BsonArray), array, null);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a raw BSON array.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name.</param>
|
|
|
- /// <param name="slice">The byte buffer containing the raw BSON array.</param>
|
|
|
- public void WriteRawBsonArray(string name, IByteBuffer slice)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteRawBsonArray(slice);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a raw BSON document.
|
|
|
/// </summary>
|
|
|
/// <param name="slice">The byte buffer containing the raw BSON document.</param>
|
|
|
public virtual void WriteRawBsonDocument(IByteBuffer slice)
|
|
|
{
|
|
|
- // overridden in BsonBinaryWriter
|
|
|
- using (var bsonReader = new BsonBinaryReader(new BsonBuffer(slice, false), true, BsonBinaryReaderSettings.Defaults))
|
|
|
+ // overridden in BsonBinaryWriter to write the raw bytes to the stream
|
|
|
+ // for all other streams, deserialize the raw bytes and serialize the resulting document instead
|
|
|
+
|
|
|
+ using (var stream = new ByteBufferStream(slice, ownsBuffer: false))
|
|
|
+ using (var bsonReader = new BsonBinaryReader(stream, BsonBinaryReaderSettings.Defaults))
|
|
|
{
|
|
|
- var document = BsonSerializer.Deserialize<BsonDocument>(bsonReader);
|
|
|
- BsonDocumentSerializer.Instance.Serialize(this, typeof(BsonDocument), document, null);
|
|
|
- }
|
|
|
- }
|
|
|
+ var deserializationContext = BsonDeserializationContext.CreateRoot(bsonReader);
|
|
|
+ var document = BsonDocumentSerializer.Instance.Deserialize(deserializationContext);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a raw BSON document.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name.</param>
|
|
|
- /// <param name="slice">The byte buffer containing the raw BSON document.</param>
|
|
|
- public void WriteRawBsonDocument(string name, IByteBuffer slice)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteRawBsonDocument(slice);
|
|
|
+ var serializationContext = BsonSerializationContext.CreateRoot(this);
|
|
|
+ BsonDocumentSerializer.Instance.Serialize(serializationContext, document);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -652,42 +338,6 @@ namespace MongoDB.Bson.IO
|
|
|
/// <param name="regex">A BsonRegularExpression.</param>
|
|
|
public abstract void WriteRegularExpression(BsonRegularExpression regex);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON regular expression to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="pattern">A regular expression pattern.</param>
|
|
|
- /// <param name="options">A regular expression options.</param>
|
|
|
- [Obsolete("Use WriteRegularExpression(BsonRegularExpression regex) instead.")]
|
|
|
- public void WriteRegularExpression(string pattern, string options)
|
|
|
- {
|
|
|
- var regex = new BsonRegularExpression(pattern, options);
|
|
|
- WriteRegularExpression(regex);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON regular expression element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="regex">A BsonRegularExpression.</param>
|
|
|
- public void WriteRegularExpression(string name, BsonRegularExpression regex)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteRegularExpression(regex);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON regular expression element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="pattern">A regular expression pattern.</param>
|
|
|
- /// <param name="options">A regular expression options.</param>
|
|
|
- [Obsolete("Use WriteRegularExpression(string name, BsonRegularExpression regex) instead.")]
|
|
|
- public void WriteRegularExpression(string name, string pattern, string options)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteRegularExpression(pattern, options);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes the start of a BSON array to the writer.
|
|
|
/// </summary>
|
|
|
@@ -700,16 +350,6 @@ namespace MongoDB.Bson.IO
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes the start of a BSON array element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- public void WriteStartArray(string name)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteStartArray();
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes the start of a BSON document to the writer.
|
|
|
/// </summary>
|
|
|
@@ -720,16 +360,8 @@ namespace MongoDB.Bson.IO
|
|
|
{
|
|
|
throw new BsonSerializationException("Maximum serialization depth exceeded (does the object being serialized have a circular reference?).");
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes the start of a BSON document element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- public void WriteStartDocument(string name)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteStartDocument();
|
|
|
+ PushElementNameValidator(_childElementNameValidatorFactory());
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -738,113 +370,24 @@ namespace MongoDB.Bson.IO
|
|
|
/// <param name="value">The String value.</param>
|
|
|
public abstract void WriteString(string value);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON String element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="value">The String value.</param>
|
|
|
- public void WriteString(string name, string value)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteString(value);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a BSON Symbol to the writer.
|
|
|
/// </summary>
|
|
|
/// <param name="value">The symbol.</param>
|
|
|
public abstract void WriteSymbol(string value);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON Symbol element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="value">The symbol.</param>
|
|
|
- public void WriteSymbol(string name, string value)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteSymbol(value);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a BSON timestamp to the writer.
|
|
|
/// </summary>
|
|
|
/// <param name="value">The combined timestamp/increment value.</param>
|
|
|
public abstract void WriteTimestamp(long value);
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON timestamp element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- /// <param name="value">The combined timestamp/increment value.</param>
|
|
|
- public void WriteTimestamp(string name, long value)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteTimestamp(value);
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Writes a BSON undefined to the writer.
|
|
|
/// </summary>
|
|
|
public abstract void WriteUndefined();
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// Writes a BSON undefined element to the writer.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The name of the element.</param>
|
|
|
- public void WriteUndefined(string name)
|
|
|
- {
|
|
|
- WriteName(name);
|
|
|
- WriteUndefined();
|
|
|
- }
|
|
|
-
|
|
|
// protected methods
|
|
|
- /// <summary>
|
|
|
- /// Checks that the element name is valid.
|
|
|
- /// </summary>
|
|
|
- /// <param name="name">The element name to be checked.</param>
|
|
|
- protected void CheckElementName(string name)
|
|
|
- {
|
|
|
- if (_checkUpdateDocument)
|
|
|
- {
|
|
|
- _checkElementNames = name == "" || name[0] != '$';
|
|
|
- _checkUpdateDocument = false;
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (_checkElementNames)
|
|
|
- {
|
|
|
- if (name == "")
|
|
|
- {
|
|
|
- var message = "Element name '' is not valid because it is an empty string.";
|
|
|
- throw new BsonSerializationException(message);
|
|
|
- }
|
|
|
-
|
|
|
- if (name[0] == '$')
|
|
|
- {
|
|
|
- // a few element names starting with $ have to be allowed for historical reasons
|
|
|
- switch (name)
|
|
|
- {
|
|
|
- case "$code":
|
|
|
- case "$db":
|
|
|
- case "$id":
|
|
|
- case "$ref":
|
|
|
- case "$scope":
|
|
|
- break;
|
|
|
- default:
|
|
|
- var message = string.Format("Element name '{0}' is not valid because it starts with a '$'.", name);
|
|
|
- throw new BsonSerializationException(message);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (name.IndexOf('.') != -1)
|
|
|
- {
|
|
|
- var message = string.Format("Element name '{0}' is not valid because it contains a '.'.", name);
|
|
|
- throw new BsonSerializationException(message);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Disposes of any resources used by the writer.
|
|
|
/// </summary>
|