示例#1
0
        /// <summary>
        /// Initializes a new instance of the JsonWriter class.
        /// </summary>
        /// <param name="writer">A TextWriter.</param>
        /// <param name="settings">Optional JsonWriter settings.</param>
        public LocalDateTimeJsonWriter(TextWriter writer, JsonWriterSettingsExtended settings)
            : base(settings)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            _textWriter         = writer;
            _jsonWriterSettings = settings; // already frozen by base class
            _context            = new JsonWriterContext(null, ContextType.TopLevel, "");
            State = BsonWriterState.Initial;
        }
        /// <summary>
        /// Initializes a new instance of the JsonWriter class.
        /// </summary>
        /// <param name="writer">A TextWriter.</param>
        /// <param name="settings">Optional JsonWriter settings.</param>
        public LocalDateTimeJsonWriter(TextWriter writer, JsonWriterSettingsExtended settings)
            : base(settings)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            _textWriter = writer;
            _jsonWriterSettings = settings; // already frozen by base class
            _context = new JsonWriterContext(null, ContextType.TopLevel, "");
            State = BsonWriterState.Initial;
        }
示例#3
0
        public static string ToJson(this BsonValue value, JsonWriterSettingsExtended settings = null)
        {
            StringBuilder sb         = new StringBuilder();
            var           serializer = MongoDB.Bson.Serialization.BsonSerializer.LookupSerializer(typeof(BsonValue));

            MongoDB.Bson.Serialization.BsonSerializationArgs args = default(MongoDB.Bson.Serialization.BsonSerializationArgs);

            using (var stringWriter = new System.IO.StringWriter())
            {
                using (var bsonWriter = new LocalDateTimeJsonWriter(stringWriter, settings))
                {
                    var context = MongoDB.Bson.Serialization.BsonSerializationContext.CreateRoot(bsonWriter, null);
                    args.NominalType = typeof(BsonValue);
                    serializer.Serialize(context, args, value);
                }
                sb.Append(stringWriter.ToString());
            }

            return(sb.ToString());
        }