예제 #1
0
        public override IJobDetail Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            BsonReader bsonReader  = (BsonReader)context.Reader;
            Type       nominalType = args.NominalType;

            var bsonType = bsonReader.GetCurrentBsonType();

            if (bsonType == BsonType.Document)
            {
                bsonReader.ReadStartDocument();

                BsonSerializer.Deserialize(bsonReader, typeof(JobKey));
                bsonReader.ReadString(TYPE);

                Assembly assembly        = Assembly.Load(bsonReader.ReadString(ASSEMBLY));
                Type     jobType         = assembly.GetType(bsonReader.ReadString(CLASS));
                string   name            = bsonReader.ReadString(NAME);
                string   group           = bsonReader.ReadString(GROUP);
                bool     requestRecovery = bsonReader.ReadBoolean(REQUEST_RECOVERY);
                bool     durable         = bsonReader.ReadBoolean(DURABLE);

                IJobDetail jobDetail = new JobDetailImpl(name, group, jobType, durable, requestRecovery);

                bsonReader.ReadBsonType();
                JobDataMap map = (JobDataMap)BsonSerializer.Deserialize(bsonReader, typeof(JobDataMap));

                /*bsonReader.ReadBsonType();
                 * string description = (string)BsonSerializer.Deserialize(bsonReader, typeof(string));*/

                jobDetail = jobDetail.GetJobBuilder()
                            .UsingJobData(map)
                            /*.WithDescription(description)*/
                            .Build();

                bsonReader.ReadEndDocument();

                return(jobDetail);
            }
            else if (bsonType == BsonType.Null)
            {
                bsonReader.ReadNull();
                return(null);
            }
            else
            {
                var message = string.Format(DESERIALIZE_ERROR_MESSAGE, nominalType.FullName, bsonType);
                throw new BsonSerializationException(message);
            }
        }
예제 #2
0
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            IBsonSerializationOptions options
            )
        {
            var bsonType = bsonReader.CurrentBsonType;

            switch (bsonType)
            {
            case BsonType.Boolean:
                return(bsonReader.ReadBoolean());

            case BsonType.Double:
                return(bsonReader.ReadDouble() != 0.0);

            case BsonType.Int32:
                return(bsonReader.ReadInt32() != 0);

            case BsonType.Int64:
                return(bsonReader.ReadInt64() != 0);

            case BsonType.Null:
                bsonReader.ReadNull();
                return(false);

            case BsonType.String:
                return(XmlConvert.ToBoolean(bsonReader.ReadString().ToLower()));

            default:
                var message = string.Format("Cannot deserialize Boolean from BsonType: {0}", bsonType);
                throw new FileFormatException(message);
            }
        }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(CultureInfo));

            var bsonType = bsonReader.GetCurrentBsonType();

            switch (bsonType)
            {
            case BsonType.Null:
                bsonReader.ReadNull();
                return(null);

            case BsonType.Document:
                bsonReader.ReadStartDocument();
                var name            = bsonReader.ReadString("Name");
                var useUserOverride = bsonReader.ReadBoolean("UseUserOverride");
                bsonReader.ReadEndDocument();
                return(new CultureInfo(name, useUserOverride));

            case BsonType.String:
                return(new CultureInfo(bsonReader.ReadString()));

            default:
                var message = string.Format("Cannot deserialize CultureInfo from BsonType {0}.", bsonType);
                throw new FileFormatException(message);
            }
        }
예제 #4
0
        public override object Deserialize(BsonReader bsonReader, Type nominalType,
                                           Type actualType, IBsonSerializationOptions options)
        {
            var bsonType = bsonReader.CurrentBsonType;

            switch (bsonType)
            {
            case BsonType.Null:
                bsonReader.ReadNull();
                return(string.Empty);

            case BsonType.String:
                return(bsonReader.ReadString());

            case BsonType.Int32:
                return(bsonReader.ReadInt32().ToString(CultureInfo.InvariantCulture));

            case BsonType.Boolean:
                return(bsonReader.ReadBoolean().ToString(CultureInfo.InvariantCulture));

            case BsonType.Double:
                return(bsonReader.ReadDouble().ToString());

            default:
                var message = string.Format("Cannot deserialize BsonString,BsonBoolean or BsonInt32 from BsonType {0}.", bsonType);
                throw new BsonSerializationException(message);
            }
        }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(CultureInfo));

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.Null:
                    bsonReader.ReadNull();
                    return null;
                case BsonType.Document:
                    bsonReader.ReadStartDocument();
                    var name = bsonReader.ReadString("Name");
                    var useUserOverride = bsonReader.ReadBoolean("UseUserOverride");
                    bsonReader.ReadEndDocument();
                    return new CultureInfo(name, useUserOverride);
                case BsonType.String:
                    return new CultureInfo(bsonReader.ReadString());
                default:
                    var message = string.Format("Cannot deserialize CultureInfo from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
예제 #6
0
        static object ReadObject(BsonReader bsonReader)         //_120509_173140 keep consistent
        {
            switch (bsonReader.GetCurrentBsonType())
            {
            case BsonType.Array: return(ReadArray(bsonReader));                                                                                               // replacement

            case BsonType.Binary: var binary = BsonSerializer.Deserialize <BsonValue>(bsonReader); return(BsonTypeMapper.MapToDotNetValue(binary) ?? binary); // byte[] or Guid else self

            case BsonType.Boolean: return(bsonReader.ReadBoolean());

            case BsonType.DateTime: return(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()));

            case BsonType.Document: return(ReadCustomObject(bsonReader));                    // replacement

            case BsonType.Double: return(bsonReader.ReadDouble());

            case BsonType.Int32: return(bsonReader.ReadInt32());

            case BsonType.Int64: return(bsonReader.ReadInt64());

            case BsonType.Null: bsonReader.ReadNull(); return(null);

            case BsonType.ObjectId: return(bsonReader.ReadObjectId());

            case BsonType.String: return(bsonReader.ReadString());

            default: return(BsonSerializer.Deserialize <BsonValue>(bsonReader));
            }
        }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(bool));

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.Boolean:
                    return bsonReader.ReadBoolean();
                case BsonType.Double:
                    return bsonReader.ReadDouble() != 0.0;
                case BsonType.Int32:
                    return bsonReader.ReadInt32() != 0;
                case BsonType.Int64:
                    return bsonReader.ReadInt64() != 0;
                case BsonType.Null:
                    bsonReader.ReadNull();
                    return false;
                case BsonType.String:
                    return XmlConvert.ToBoolean(bsonReader.ReadString().ToLower());
                default:
                    var message = string.Format("Cannot deserialize Boolean from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
        // private methods
        private bool IsCSharpNullRepresentation(BsonReader bsonReader)
        {
            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            var bsonType = bsonReader.ReadBsonType();

            if (bsonType == BsonType.Boolean)
            {
                var name = bsonReader.ReadName();
                if (name == "_csharpnull" || name == "$csharpnull")
                {
                    var value = bsonReader.ReadBoolean();
                    if (value)
                    {
                        bsonType = bsonReader.ReadBsonType();
                        if (bsonType == BsonType.EndOfDocument)
                        {
                            bsonReader.ReadEndDocument();
                            return(true);
                        }
                    }
                }
            }

            bsonReader.ReturnToBookmark(bookmark);
            return(false);
        }
예제 #9
0
        public OXmlTextElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options)
        {
            OXmlTextElement element = new OXmlTextElement();

            while (true)
            {
                BsonType bsonType = bsonReader.ReadBsonType();
                if (bsonType == BsonType.EndOfDocument)
                {
                    break;
                }
                string name = bsonReader.ReadName();
                switch (name.ToLower())
                {
                case "type":
                    if (bsonType != BsonType.String)
                    {
                        throw new PBException($"wrong type value {bsonType}");
                    }
                    string type = bsonReader.ReadString();
                    if (type.ToLower() != "text")
                    {
                        throw new PBException($"invalid Type {type} when deserialize OXmlTextElement");
                    }
                    break;

                case "text":
                    if (bsonType == BsonType.Null)
                    {
                        break;
                    }
                    if (bsonType != BsonType.String)
                    {
                        throw new PBException($"wrong text value {bsonType}");
                    }
                    element.Text = bsonReader.ReadString();
                    break;

                case "preservespace":
                    if (bsonType == BsonType.Null)
                    {
                        break;
                    }
                    if (bsonType != BsonType.Boolean)
                    {
                        throw new PBException($"wrong PreserveSpace value {bsonType}");
                    }
                    element.PreserveSpace = bsonReader.ReadBoolean();
                    break;

                default:
                    throw new PBException($"unknow Text value \"{name}\"");
                }
            }
            return(element);
        }
예제 #10
0
        public void TestBooleanTrue()
        {
            var json = "true";

            using (bsonReader = BsonReader.Create(json)) {
                Assert.AreEqual(BsonType.Boolean, bsonReader.ReadBsonType());
                Assert.AreEqual(true, bsonReader.ReadBoolean());
                Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
            }
            Assert.AreEqual(json, BsonSerializer.Deserialize <bool>(new StringReader(json)).ToJson());
        }
예제 #11
0
        public void TestBooleanFalse()
        {
            var json = "false";

            using (_bsonReader = new JsonReader(json))
            {
                Assert.AreEqual(BsonType.Boolean, _bsonReader.ReadBsonType());
                Assert.AreEqual(false, _bsonReader.ReadBoolean());
                Assert.AreEqual(BsonReaderState.Done, _bsonReader.State);
            }
            Assert.AreEqual(json, BsonSerializer.Deserialize <bool>(json).ToJson());
        }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(BsonBoolean));

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.Boolean:
                    return (BsonBoolean)bsonReader.ReadBoolean();
                default:
                    var message = string.Format("Cannot deserialize BsonBoolean from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
예제 #13
0
        public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            object value     = null;
            var    valueType = actualType.GetConceptValueType();

            if (valueType == typeof(Guid))
            {
                var binaryData = bsonReader.ReadBinaryData();
                value = binaryData.ToGuid();
            }
            else if (valueType == typeof(double))
            {
                value = bsonReader.ReadDouble();
            }
            else if (valueType == typeof(float))
            {
                value = (float)bsonReader.ReadDouble();
            }
            else if (valueType == typeof(Int32))
            {
                value = bsonReader.ReadInt32();
            }
            else if (valueType == typeof(Int64))
            {
                value = bsonReader.ReadInt64();
            }
            else if (valueType == typeof(bool))
            {
                value = bsonReader.ReadBoolean();
            }
            else if (valueType == typeof(string))
            {
                value = bsonReader.ReadString();
            }
            else if (valueType == typeof(decimal))
            {
                value = decimal.Parse(bsonReader.ReadString());
            }

            var concept = ConceptFactory.CreateConceptInstance(actualType, value);

            return(concept);
        }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(BsonBoolean));

            var bsonType = bsonReader.GetCurrentBsonType();

            switch (bsonType)
            {
            case BsonType.Boolean:
                return((BsonBoolean)bsonReader.ReadBoolean());

            default:
                var message = string.Format("Cannot deserialize BsonBoolean from BsonType {0}.", bsonType);
                throw new FileFormatException(message);
            }
        }
예제 #15
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(BsonNull));

            var    bsonType = bsonReader.GetCurrentBsonType();
            string message;

            switch (bsonType)
            {
            case BsonType.Null:
                bsonReader.ReadNull();
                return(BsonNull.Value);

            case BsonType.Document:
                bsonReader.ReadStartDocument();
                var name = bsonReader.ReadName();
                if (name == "_csharpnull" || name == "$csharpnull")
                {
                    var csharpNull = bsonReader.ReadBoolean();
                    bsonReader.ReadEndDocument();
                    return(csharpNull ? null : BsonNull.Value);
                }
                else
                {
                    message = string.Format("Unexpected element name while deserializing a BsonNull: {0}.", name);
                    throw new FileFormatException(message);
                }

            default:
                message = string.Format("Cannot deserialize BsonNull from BsonType {0}.", bsonType);
                throw new FileFormatException(message);
            }
        }
예제 #16
0
        private void ReadValue()
        {
            //bool value = true;
            _type = PBBsonReaderType.Value;
            switch (_bsonType)
            {
            case BsonType.Document:
                WriteLine(2, "ReadStartDocument");
                WriteLine(1, "{");
                //_indent += 2;
                Indent(2);
                _reader.ReadStartDocument();
                _type = PBBsonReaderType.Document;
                //value = false;
                break;

            case BsonType.Array:
                WriteLine(2, "ReadStartArray");
                WriteLine(1, "[");
                //_indent += 2;
                Indent(2);
                _reader.ReadStartArray();
                _type = PBBsonReaderType.Array;
                //value = false;
                break;

            case BsonType.Binary:
                _value = BsonValue.Create(_reader.ReadBytes());
                break;

            case BsonType.Boolean:
                _value = BsonValue.Create(_reader.ReadBoolean());
                break;

            case BsonType.DateTime:
                _value = BsonValue.Create(_reader.ReadDateTime());
                break;

            case BsonType.Double:
                _value = BsonValue.Create(_reader.ReadDouble());
                break;

            case BsonType.Int32:
                _value = BsonValue.Create(_reader.ReadInt32());
                break;

            case BsonType.Int64:
                _value = BsonValue.Create(_reader.ReadInt64());
                break;

            case BsonType.JavaScript:
                _value = BsonValue.Create(_reader.ReadJavaScript());
                break;

            case BsonType.JavaScriptWithScope:
                _value = BsonValue.Create(_reader.ReadJavaScriptWithScope());
                break;

            case BsonType.MaxKey:
                _reader.ReadMaxKey();
                _value = BsonMaxKey.Value;
                break;

            case BsonType.MinKey:
                _reader.ReadMinKey();
                _value = BsonMinKey.Value;
                break;

            case BsonType.Null:
                _reader.ReadNull();
                _value = BsonNull.Value;
                break;

            case BsonType.ObjectId:
                _value = BsonValue.Create(_reader.ReadObjectId());
                break;

            case BsonType.RegularExpression:
                _value = BsonValue.Create(_reader.ReadRegularExpression());
                break;

            case BsonType.String:
                _value = BsonValue.Create(_reader.ReadString());
                break;

            case BsonType.Symbol:
                _value = BsonValue.Create(_reader.ReadSymbol());
                break;

            case BsonType.Timestamp:
                _value = BsonValue.Create(_reader.ReadTimestamp());
                break;

            case BsonType.Undefined:
                _reader.ReadUndefined();
                _value = BsonUndefined.Value;
                break;

            default:
                throw new PBException("error unable to read value type {0}", _bsonType);
            }
            //return value;
        }
        // private methods
        private bool IsCSharpNullRepresentation(BsonReader bsonReader)
        {
            var bookmark = bsonReader.GetBookmark();
            bsonReader.ReadStartDocument();
            var bsonType = bsonReader.ReadBsonType();
            if (bsonType == BsonType.Boolean)
            {
                var name = bsonReader.ReadName();
                if (name == "_csharpnull" || name == "$csharpnull")
                {
                    var value = bsonReader.ReadBoolean();
                    if (value)
                    {
                        bsonType = bsonReader.ReadBsonType();
                        if (bsonType == BsonType.EndOfDocument)
                        {
                            bsonReader.ReadEndDocument();
                            return true;
                        }
                    }
                }
            }

            bsonReader.ReturnToBookmark(bookmark);
            return false;
        }
예제 #18
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(BsonNull));

            var bsonType = bsonReader.GetCurrentBsonType();
            string message;
            switch (bsonType)
            {
                case BsonType.Null:
                    bsonReader.ReadNull();
                    return BsonNull.Value;
                case BsonType.Document:
                    bsonReader.ReadStartDocument();
                    var name = bsonReader.ReadName();
                    if (name == "_csharpnull" || name == "$csharpnull")
                    {
                        var csharpNull = bsonReader.ReadBoolean();
                        bsonReader.ReadEndDocument();
                        return csharpNull ? null : BsonNull.Value;
                    }
                    else
                    {
                        message = string.Format("Unexpected element name while deserializing a BsonNull: {0}.", name);
                        throw new FileFormatException(message);
                    }
                default:
                    message = string.Format("Cannot deserialize BsonNull from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }