| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910 |
- /* Copyright 2010-2014 MongoDB 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.IO;
- using System.Linq;
- using MongoDB.Bson.Serialization;
- using MongoDB.Bson.Serialization.Serializers;
- namespace MongoDB.Bson.IO
- {
- /// <summary>
- /// Represents a BSON writer for some external format (see subclasses).
- /// </summary>
- public abstract class BsonWriter : IDisposable
- {
- // private fields
- private bool _disposed = false;
- private BsonWriterSettings _settings;
- private BsonWriterState _state;
- private string _name;
- private bool _checkElementNames;
- private bool _checkUpdateDocument;
- private int _serializationDepth;
- // constructors
- /// <summary>
- /// Initializes a new instance of the BsonWriter class.
- /// </summary>
- /// <param name="settings">The writer settings.</param>
- protected BsonWriter(BsonWriterSettings settings)
- {
- if (settings == null)
- {
- throw new ArgumentNullException("settings");
- }
- _settings = settings.FrozenCopy();
- _state = BsonWriterState.Initial;
- }
- // 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>
- public int SerializationDepth
- {
- get { return _serializationDepth; }
- }
- /// <summary>
- /// Gets the settings of the writer.
- /// </summary>
- public BsonWriterSettings Settings
- {
- get { return _settings; }
- }
- /// <summary>
- /// Gets the current state of the writer.
- /// </summary>
- public BsonWriterState State
- {
- get { return _state; }
- protected set { _state = value; }
- }
- // protected properties
- /// <summary>
- /// Gets whether the BsonWriter has been disposed.
- /// </summary>
- public bool Disposed
- {
- get { return _disposed; }
- }
- // protected properties
- /// <summary>
- /// Gets the name of the element being written.
- /// </summary>
- protected string Name
- {
- get { return _name; }
- }
- // 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.
- /// </summary>
- public abstract void Close();
- /// <summary>
- /// Disposes of any resources used by the writer.
- /// </summary>
- public void Dispose()
- {
- if (!_disposed)
- {
- Dispose(true);
- _disposed = true;
- }
- }
- /// <summary>
- /// Flushes any pending data to the output destination.
- /// </summary>
- 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.
- /// </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)
- {
- var guidRepresentation = (subType == BsonBinarySubType.UuidStandard) ? GuidRepresentation.Standard : GuidRepresentation.Unspecified;
- WriteBinaryData(bytes, subType, guidRepresentation);
- }
- /// <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.
- /// </summary>
- /// <param name="name">The name of the element.</param>
- /// <param name="binaryData">The binary data.</param>
- public void WriteBinaryData(string name, BsonBinaryData binaryData)
- {
- WriteName(name);
- WriteBinaryData(binaryData);
- }
- /// <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);
- }
- /// <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>
- /// <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);
- }
- /// <summary>
- /// Writes a BSON Boolean to the writer.
- /// </summary>
- /// <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);
- }
- /// <summary>
- /// Writes a BSON Double to the writer.
- /// </summary>
- /// <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>
- public virtual void WriteEndArray()
- {
- _serializationDepth--;
- }
- /// <summary>
- /// Writes the end of a BSON document to the writer.
- /// </summary>
- public virtual void WriteEndDocument()
- {
- _serializationDepth--;
- }
- /// <summary>
- /// Writes a BSON Int32 to the writer.
- /// </summary>
- /// <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>
- /// <param name="name">The name of the element.</param>
- public virtual void WriteName(string name)
- {
- if (name == null)
- {
- throw new ArgumentNullException("name");
- }
- if (name.IndexOf('\0') != -1)
- {
- throw new ArgumentException("Element names cannot contain nulls.", "name");
- }
- if (_disposed) { throw new ObjectDisposedException(this.GetType().Name); }
- if (_state != BsonWriterState.Name)
- {
- ThrowInvalidState("WriteName", BsonWriterState.Name);
- }
- CheckElementName(name);
- _name = name;
- _state = BsonWriterState.Value;
- }
- /// <summary>
- /// Writes a BSON null to the writer.
- /// </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
- using (var bsonBuffer = new BsonBuffer())
- {
- 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))
- {
- bsonReader.ReadStartDocument();
- bsonReader.ReadName("x");
- array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
- bsonReader.ReadEndDocument();
- }
- 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))
- {
- var document = BsonSerializer.Deserialize<BsonDocument>(bsonReader);
- BsonDocumentSerializer.Instance.Serialize(this, typeof(BsonDocument), document, null);
- }
- }
- /// <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);
- }
- /// <summary>
- /// Writes a BSON regular expression to the writer.
- /// </summary>
- /// <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>
- public virtual void WriteStartArray()
- {
- _serializationDepth++;
- if (_serializationDepth > _settings.MaxSerializationDepth)
- {
- throw new BsonSerializationException("Maximum serialization depth exceeded (does the object being serialized have a circular reference?).");
- }
- }
- /// <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>
- public virtual void WriteStartDocument()
- {
- _serializationDepth++;
- if (_serializationDepth > _settings.MaxSerializationDepth)
- {
- 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();
- }
- /// <summary>
- /// Writes a BSON String to the writer.
- /// </summary>
- /// <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>
- /// <param name="disposing">True if called from Dispose.</param>
- protected virtual void Dispose(bool disposing)
- {
- }
- /// <summary>
- /// Throws an InvalidOperationException when the method called is not valid for the current ContextType.
- /// </summary>
- /// <param name="methodName">The name of the method.</param>
- /// <param name="actualContextType">The actual ContextType.</param>
- /// <param name="validContextTypes">The valid ContextTypes.</param>
- protected void ThrowInvalidContextType(
- string methodName,
- ContextType actualContextType,
- params ContextType[] validContextTypes)
- {
- var validContextTypesString = string.Join(" or ", validContextTypes.Select(c => c.ToString()).ToArray());
- var message = string.Format(
- "{0} can only be called when ContextType is {1}, not when ContextType is {2}.",
- methodName, validContextTypesString, actualContextType);
- throw new InvalidOperationException(message);
- }
- /// <summary>
- /// Throws an InvalidOperationException when the method called is not valid for the current state.
- /// </summary>
- /// <param name="methodName">The name of the method.</param>
- /// <param name="validStates">The valid states.</param>
- protected void ThrowInvalidState(string methodName, params BsonWriterState[] validStates)
- {
- string message;
- if (_state == BsonWriterState.Initial || _state == BsonWriterState.ScopeDocument || _state == BsonWriterState.Done)
- {
- if (!methodName.StartsWith("End", StringComparison.Ordinal) && methodName != "WriteName")
- {
- var typeName = methodName.Substring(5);
- if (typeName.StartsWith("Start", StringComparison.Ordinal))
- {
- typeName = typeName.Substring(5);
- }
- var article = "A";
- if (new char[] { 'A', 'E', 'I', 'O', 'U' }.Contains(typeName[0]))
- {
- article = "An";
- }
- message = string.Format(
- "{0} {1} value cannot be written to the root level of a BSON document.",
- article, typeName);
- throw new InvalidOperationException(message);
- }
- }
- var validStatesString = string.Join(" or ", validStates.Select(s => s.ToString()).ToArray());
- message = string.Format(
- "{0} can only be called when State is {1}, not when State is {2}",
- methodName, validStatesString, _state);
- throw new InvalidOperationException(message);
- }
- }
- }
|