public OXmlSimpleFieldElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlSimpleFieldElement element = new OXmlSimpleFieldElement(); 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() != "simplefield") throw new PBException($"invalid Type {type} when deserialize OXmlSimpleFieldElement"); break; case "instruction": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.String) throw new PBException($"wrong Instruction value {bsonType}"); element.Instruction = bsonReader.ReadString(); break; default: throw new PBException($"unknow SimpleField value \"{name}\""); } } return element; }
public OXmlOpenHeaderElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlOpenHeaderElement element = new OXmlOpenHeaderElement(); 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(); //"openfooter" if (type.ToLower() != "openheader") throw new PBException($"invalid Type {type} when deserialize OXmlOpenHeader"); break; case "headertype": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.String) throw new PBException($"wrong HeaderType value {bsonType}"); element.HeaderType = bsonReader.ReadString().zParseEnum<HeaderFooterValues>(); break; default: //OpenHeaderFooter throw new PBException($"unknow OpenHeader value \"{name}\""); } } return element; }
// 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); } }
// 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) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { bsonReader.ReadStartDocument(); DeserializeType(bsonReader, "link"); bsonReader.ReadName("properties"); bsonReader.ReadStartDocument(); var href = bsonReader.ReadString("href"); string hrefType = null; if (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { hrefType = bsonReader.ReadString("type"); } bsonReader.ReadEndDocument(); bsonReader.ReadEndDocument(); return(new GeoJsonLinkedCoordinateReferenceSystem(href, hrefType)); } }
// 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) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return null; } else { bsonReader.ReadStartDocument(); DeserializeType(bsonReader, "link"); bsonReader.ReadName("properties"); bsonReader.ReadStartDocument(); var href = bsonReader.ReadString("href"); string hrefType = null; if (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { hrefType = bsonReader.ReadString("type"); } bsonReader.ReadEndDocument(); bsonReader.ReadEndDocument(); return new GeoJsonLinkedCoordinateReferenceSystem(href, hrefType); } }
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { object value = null; var valueType = actualType.GetConceptValueType(); if (valueType == typeof(Guid)) { var guidBytes = new byte[16]; BsonBinarySubType subType; bsonReader.ReadBinaryData (out guidBytes, out subType); value = new Guid (guidBytes); } 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 OXmlParagraphElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlParagraphElement paragraph = new OXmlParagraphElement(); while (true) { BsonType bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) break; //if (bsonType != BsonType.String) // throw new PBException("error ZStringArray cannot contain value of type {0}", bsonType); //var value = bsonReader.ReadString(); 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() != "paragraph") throw new PBException($"invalid Type {type} when deserialize OXmlParagraphElement"); break; case "style": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.String) throw new PBException($"wrong style value {bsonType}"); paragraph.Style = bsonReader.ReadString(); break; default: throw new PBException($"unknow Paragraph value \"{name}\""); } } return paragraph; }
private ZValue ReadZValueFromBsonDocument(BsonReader bsonReader) { // { "_t" : "ZString", "value" : "" } bsonReader.ReadStartDocument(); BsonType type = bsonReader.ReadBsonType(); if (type != BsonType.String) { throw new PBException("error reading ZValue can't find ZValue type \"_t\""); } string name = bsonReader.ReadName(); if (name != "_t") { throw new PBException("error reading ZValue can't find ZValue type \"_t\""); } string typeName = bsonReader.ReadString(); type = bsonReader.ReadBsonType(); name = bsonReader.ReadName(); if (name != "value") { throw new PBException("error reading ZValue can't find ZValue value \"value\""); } ZValue value = null; switch (typeName) { case "ZString": if (type != BsonType.String) { throw new PBException("error reading ZString value is'nt a string ({0})", type); } value = new ZString(bsonReader.ReadString()); break; //case "ZStringArray": // if (type != BsonType.Array) // throw new PBException("error reading ZStringArray value is'nt an array ({0})", type); // value = new ZString(bsonReader.ReadString()); // break; case "ZInt": if (type != BsonType.Int32) { throw new PBException("error reading ZInt value is'nt an int32 ({0})", type); } value = new ZInt(bsonReader.ReadInt32()); break; default: throw new PBException("error reading ZValue type \"{0}\" is'nt a ZValue type", typeName); } type = bsonReader.ReadBsonType(); if (type != BsonType.EndOfDocument) { throw new PBException("error reading ZValue cant find end of document ({0})", type); } bsonReader.ReadEndDocument(); return(value); }
// 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); } }
public OXmlDocDefaultsRunPropertiesElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlDocDefaultsRunPropertiesElement element = new OXmlDocDefaultsRunPropertiesElement(); 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() != "docdefaultsrunproperties") { throw new PBException($"invalid Type {type} when deserialize OXmlDocDefaultsRunPropertiesElement"); } break; case "runfonts": if (bsonType == BsonType.Null) { break; } if (bsonType != BsonType.Document) { throw new PBException($"wrong RunFonts value {bsonType}"); } element.RunFonts = OXmlCommonSerializer.ReadRunFonts(bsonReader); break; case "fontsize": if (bsonType == BsonType.Null) { break; } if (bsonType != BsonType.String) { throw new PBException($"wrong FontSize value {bsonType}"); } element.FontSize = bsonReader.ReadString(); break; default: throw new PBException($"unknow DocDefaultsRunProperties value \"{name}\""); } } return(element); }
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); }
// 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(DateTimeOffset)); var dateTimeSerializationOptions = EnsureSerializationOptions <DateTimeSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); DateTimeOffset value; switch (bsonType) { case BsonType.DateTime: // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly value = BsonDateTime.Create(bsonReader.ReadDateTime()).Value; break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTimeUTC"); // ignore value (use Ticks instead) value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc); bsonReader.ReadEndDocument(); break; case BsonType.Int64: value = new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono if (dateTimeSerializationOptions.DateOnly) { value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" }; value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } break; default: var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType); throw new FormatException(message); } return(value); }
public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { EvaluationResult er = new EvaluationResult(); bsonReader.ReadStartDocument(); er.Id = bsonReader.ReadObjectId(); er.DurationInSeconds = bsonReader.ReadDouble(); er.startTime = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()); er.endTime = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()); er.Result = EvaluationStatus.Parse(bsonReader.ReadString()); er.JSON = bsonReader.ReadString(); bsonReader.ReadEndDocument(); return(er); }
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); } }
// 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(MongoDBRef)); if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null) { bsonReader.ReadNull(); return(null); } else { string databaseName = null; string collectionName = null; BsonValue id = null; bsonReader.ReadStartDocument(); BsonType bsonType; while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "$ref": collectionName = bsonReader.ReadString(); break; case "$id": id = BsonValue.ReadFrom(bsonReader);; break; case "$db": databaseName = bsonReader.ReadString(); break; default: var message = string.Format("Element '{0}' is not valid for MongoDBRef.", name); throw new FileFormatException(message); } } bsonReader.ReadEndDocument(); return(new MongoDBRef(databaseName, collectionName, id)); } }
// 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); } }
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var typeName = bsonReader.ReadString(); var type = Type.GetType(typeName); return(type); }
public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { return XmlConvert.ToDateTime(bsonReader.ReadString(), XmlDateTimeSerializationMode.RoundtripKind); }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var val = bsonReader.ReadString(); // TODO: we should create ID via private/protected constructor and set ID value via IIdentity interface return(Activator.CreateInstance(nominalType, val)); }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (_trace) { pb.Trace.WriteLine("ZStringArraySerializer.Deserialize()"); } VerifyTypes(nominalType, actualType, typeof(ZStringArray)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: bsonReader.ReadStartArray(); //return new ZString(bsonReader.ReadString()); var array = new List <string>(); bsonType = bsonReader.ReadBsonType(); while (bsonType != BsonType.EndOfDocument) { if (bsonType != BsonType.String) { throw new PBException("error ZStringArray cannot contain value of type {0}", bsonType); } var value = bsonReader.ReadString(); array.Add(value); bsonType = bsonReader.ReadBsonType(); } bsonReader.ReadEndArray(); return(new ZStringArray(array.ToArray())); default: throw new PBException("error cannot deserialize ZStringArray from BsonType {0}.", bsonType); } }
// 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(BsonSymbol)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.String: return(BsonSymbol.Create(bsonReader.ReadString())); case BsonType.Symbol: return(BsonSymbol.Create(bsonReader.ReadSymbol())); default: var message = string.Format("Cannot deserialize BsonSymbol from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public Type GetActualType(BsonReader bsonReader, Type nominalType) { //Edit: added additional check for list if (nominalType != typeof(IRestriction) && nominalType != typeof(List <IRestriction>)) { throw new Exception("Cannot use IRestrictionDiscriminatorConvention for type " + nominalType); } var ret = nominalType; var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); if (bsonReader.FindElement(ElementName)) { var value = bsonReader.ReadString(); ret = Type.GetType(value); if (ret == null) { throw new Exception("Could not find type from " + value); } //Edit: doing the checking a little different if (!typeof(IRestriction).IsAssignableFrom(ret) && !ret.IsSubclassOf(typeof(IRestriction))) { throw new Exception("type is not an IRestriction"); } } bsonReader.ReturnToBookmark(bookmark); return(ret); }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (_trace) pb.Trace.WriteLine("ZStringArraySerializer.Deserialize()"); VerifyTypes(nominalType, actualType, typeof(ZStringArray)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: bsonReader.ReadStartArray(); //return new ZString(bsonReader.ReadString()); var array = new List<string>(); bsonType = bsonReader.ReadBsonType(); while (bsonType != BsonType.EndOfDocument) { if (bsonType != BsonType.String) throw new PBException("error ZStringArray cannot contain value of type {0}", bsonType); var value = bsonReader.ReadString(); array.Add(value); bsonType = bsonReader.ReadBsonType(); } bsonReader.ReadEndArray(); return new ZStringArray(array.ToArray()); default: throw new PBException("error cannot deserialize ZStringArray from BsonType {0}.", bsonType); } }
/// <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); } }
/// <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 ) { BsonType bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.ObjectId: int timestamp; int machine; short pid; int increment; bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment); return(new ObjectId(timestamp, machine, pid, increment)); case BsonType.String: return(ObjectId.Parse(bsonReader.ReadString())); default: var message = string.Format("Cannot deserialize ObjectId from BsonType: {0}", bsonType); throw new FileFormatException(message); } }
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); } }
/// <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 representationOptions = (RepresentationSerializationOptions)options ?? defaultRepresentationOptions; var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Double: return(representationOptions.ToInt64(bsonReader.ReadDouble())); case BsonType.Int32: return(representationOptions.ToInt64(bsonReader.ReadInt32())); case BsonType.Int64: return(bsonReader.ReadInt64()); case BsonType.String: return(XmlConvert.ToInt64(bsonReader.ReadString())); default: var message = string.Format("Cannot deserialize Int64 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(string)); var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { var representation = (options == null) ? BsonType.String : ((RepresentationSerializationOptions)options).Representation; switch (representation) { case BsonType.ObjectId: int timestamp, machine, increment; short pid; bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment); var objectId = new ObjectId(timestamp, machine, pid, increment); return(objectId.ToString()); case BsonType.String: return(bsonReader.ReadString()); case BsonType.Symbol: return(bsonReader.ReadSymbol()); default: var message = string.Format("Cannot deserialize string 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(ushort)); var representationSerializationOptions = EnsureSerializationOptions <RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Double: return(representationSerializationOptions.ToUInt16(bsonReader.ReadDouble())); case BsonType.Int32: return(representationSerializationOptions.ToUInt16(bsonReader.ReadInt32())); case BsonType.Int64: return(representationSerializationOptions.ToUInt16(bsonReader.ReadInt64())); case BsonType.String: return(XmlConvert.ToUInt16(bsonReader.ReadString())); default: var message = string.Format("Cannot deserialize uInt16 from BsonType {0}.", bsonType); throw new Exception(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(IPAddress)); BsonType bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.String: var stringValue = bsonReader.ReadString(); IPAddress address; if (IPAddress.TryParse(stringValue, out address)) { return(address); } message = string.Format("Invalid IPAddress value '{0}'.", stringValue); throw new FileFormatException(message); default: message = string.Format("Cannot deserialize IPAddress from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
/// <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, // ignored IBsonSerializationOptions options ) { VerifyDeserializeType(nominalType); var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Int32: return(Enum.ToObject(nominalType, bsonReader.ReadInt32())); case BsonType.Int64: return(Enum.ToObject(nominalType, bsonReader.ReadInt64())); case BsonType.Double: return(Enum.ToObject(nominalType, (long)bsonReader.ReadDouble())); case BsonType.String: return(Enum.Parse(nominalType, bsonReader.ReadString())); default: var message = string.Format("Cannot deserialize {0} from BsonType {1}.", nominalType.FullName, bsonType); throw new FileFormatException(message); } }
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(double)); var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Double: return bsonReader.ReadDouble(); case BsonType.Int32: return representationSerializationOptions.ToDouble(bsonReader.ReadInt32()); case BsonType.Int64: return representationSerializationOptions.ToDouble(bsonReader.ReadInt64()); case BsonType.String: return XmlConvert.ToDouble(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize Double from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
/// <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.Binary: byte[] bytes; BsonBinarySubType subType; bsonReader.ReadBinaryData(out bytes, out subType); if (bytes.Length != 16) { throw new FileFormatException("BinaryData length is not 16"); } if (subType != BsonBinarySubType.Uuid) { throw new FileFormatException("BinaryData sub type is not Uuid"); } return(new Guid(bytes)); case BsonType.String: return(new Guid(bsonReader.ReadString())); default: var message = string.Format("Cannot deserialize Guid from BsonType: {0}", bsonType); throw new FileFormatException(message); } }
// private methods private Type GetActualType(BsonReader bsonReader) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); if (bsonReader.FindElement("type")) { var type = bsonReader.ReadString(); bsonReader.ReturnToBookmark(bookmark); switch (type) { case "link": return(typeof(GeoJsonLinkedCoordinateReferenceSystem)); case "name": return(typeof(GeoJsonNamedCoordinateReferenceSystem)); default: var message = string.Format("The type field of the GeoJsonCoordinateReferenceSystem is not valid: '{0}'.", type); throw new FormatException(message); } } else { throw new FormatException("GeoJsonCoordinateReferenceSystem object is missing the type field."); } }
public OXmlParagraphElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlParagraphElement paragraph = new OXmlParagraphElement(); while (true) { BsonType bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) { break; } //if (bsonType != BsonType.String) // throw new PBException("error ZStringArray cannot contain value of type {0}", bsonType); //var value = bsonReader.ReadString(); 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() != "paragraph") { throw new PBException($"invalid Type {type} when deserialize OXmlParagraphElement"); } break; case "style": if (bsonType == BsonType.Null) { break; } if (bsonType != BsonType.String) { throw new PBException($"wrong style value {bsonType}"); } paragraph.Style = bsonReader.ReadString(); break; default: throw new PBException($"unknow Paragraph value \"{name}\""); } } return(paragraph); }
public OXmlOpenHeaderElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlOpenHeaderElement element = new OXmlOpenHeaderElement(); 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(); //"openfooter" if (type.ToLower() != "openheader") { throw new PBException($"invalid Type {type} when deserialize OXmlOpenHeader"); } break; case "headertype": if (bsonType == BsonType.Null) { break; } if (bsonType != BsonType.String) { throw new PBException($"wrong HeaderType value {bsonType}"); } element.HeaderType = bsonReader.ReadString().zParseEnum <HeaderFooterValues>(); break; default: //OpenHeaderFooter throw new PBException($"unknow OpenHeader value \"{name}\""); } } return(element); }
public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { return(XmlConvert.ToDateTime(bsonReader.ReadString(), XmlDateTimeSerializationMode.RoundtripKind)); }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(DateTime)); var stringDateTime = bsonReader.ReadString(); var value = DateTime.Parse(stringDateTime); return(value); }
// 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) { var arraySerializationOptions = EnsureSerializationOptions <ArraySerializationOptions>(options); var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions; var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Array: var instance = CreateInstance(actualType); var itemDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); Type lastItemType = null; IBsonSerializer lastItemSerializer = null; bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var itemType = itemDiscriminatorConvention.GetActualType(bsonReader, typeof(object)); IBsonSerializer itemSerializer; if (itemType == lastItemType) { itemSerializer = lastItemSerializer; } else { itemSerializer = BsonSerializer.LookupSerializer(itemType); lastItemType = itemType; lastItemSerializer = itemSerializer; } var item = itemSerializer.Deserialize(bsonReader, typeof(object), itemType, itemSerializationOptions); AddItem(instance, item); } bsonReader.ReadEndArray(); return(FinalizeResult(instance, actualType)); case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); // skip over discriminator bsonReader.ReadName("_v"); var value = Deserialize(bsonReader, actualType, actualType, options); bsonReader.ReadEndDocument(); return(value); default: var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, 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(DateTimeOffset)); var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); DateTimeOffset value; switch (bsonType) { case BsonType.DateTime: // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly value = (new BsonDateTime(bsonReader.ReadDateTime())).ToUniversalTime(); break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTimeUTC"); // ignore value (use Ticks instead) value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc); bsonReader.ReadEndDocument(); break; case BsonType.Int64: value = new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono if (dateTimeSerializationOptions.DateOnly) { value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" }; value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } break; default: var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType); throw new FormatException(message); } return value; }
// protected methods protected void DeserializeType(BsonReader bsonReader, string expectedType) { var type = bsonReader.ReadString("type"); if (type != expectedType) { var message = string.Format("Expected type to be '{0}'.", expectedType); throw new FormatException(message); } }
public OXmlDocDefaultsRunPropertiesElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlDocDefaultsRunPropertiesElement element = new OXmlDocDefaultsRunPropertiesElement(); 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() != "docdefaultsrunproperties") throw new PBException($"invalid Type {type} when deserialize OXmlDocDefaultsRunPropertiesElement"); break; case "runfonts": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Document) throw new PBException($"wrong RunFonts value {bsonType}"); element.RunFonts = OXmlCommonSerializer.ReadRunFonts(bsonReader); break; case "fontsize": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.String) throw new PBException($"wrong FontSize value {bsonType}"); element.FontSize = bsonReader.ReadString(); break; default: throw new PBException($"unknow DocDefaultsRunProperties value \"{name}\""); } } return element; }
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; }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(WebImage)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.String: return new WebImage(bsonReader.ReadString()); case BsonType.Null: bsonReader.ReadNull(); return new WebImage(null); default: throw new PBException("error cannot deserialize UrlImage from BsonType {0}.", bsonType); } }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return Guid.Empty; case BsonType.String: var readString = bsonReader.ReadString(); return Guid.Parse(readString); default: var message = string.Format("Cannot deserialize BsonString from BsonType {0}.", bsonType); throw new BsonSerializationException(message); } }
public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { var nvc = new NameValueCollection(); bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName().Replace("__period__", "."); var value = bsonReader.ReadString(); nvc.Add(name, value); } bsonReader.ReadEndDocument(); return nvc; }
public OXmlDocSectionElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlDocSectionElement element = new OXmlDocSectionElement(); 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() != "docsection") throw new PBException($"invalid Type {type} when deserialize OXmlDocSectionElement"); break; case "pagesize": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Document) throw new PBException($"wrong PageSize value {bsonType}"); element.PageSize = ReadPageSize(bsonReader); break; case "pagemargin": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Document) throw new PBException($"wrong PageMargin value {bsonType}"); element.PageMargin = ReadPageMargin(bsonReader); break; case "pagenumberstart": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Int32) throw new PBException($"wrong PageNumberStart value {bsonType}"); element.PageNumberStart = bsonReader.ReadInt32(); break; default: throw new PBException($"unknow DocSection value \"{name}\""); } } return element; }
// 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) { if (_trace) pb.Trace.WriteLine("ZStringSerializer.Deserialize()"); VerifyTypes(nominalType, actualType, typeof(ZString)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.String: return new ZString(bsonReader.ReadString()); default: //var message = string.Format("Cannot deserialize BsonString from BsonType {0}.", bsonType); //throw new FileFormatException(message); throw new PBException("error cannot deserialize ZString from BsonType {0}.", bsonType); } }
// 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(BsonString)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.String: return new BsonString(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize BsonString from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public override object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { switch (bsonReader.CurrentBsonType) { case BsonType.ObjectId: int timestamp; int machine; short pid; int increment; bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment); var id = new ObjectId(timestamp, machine, pid, increment); return new Identity(id.ToByteArray()); case BsonType.String: return new Identity(bsonReader.ReadString()); default: throw new FormatException(string.Format("Cannot deserialize Identity from BsonType: {0}", bsonReader.CurrentBsonType)); } }
// public methods public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return null; } else { bsonReader.ReadStartDocument(); DeserializeType(bsonReader, "name"); bsonReader.ReadName("properties"); bsonReader.ReadStartDocument(); var name = bsonReader.ReadString("name"); bsonReader.ReadEndDocument(); bsonReader.ReadEndDocument(); return new GeoJsonNamedCoordinateReferenceSystem(name); } }
// public methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <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(byte[])); BsonType bsonType = bsonReader.GetCurrentBsonType(); byte[] bytes; string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: bytes = bsonReader.ReadBytes(); return bytes; case BsonType.String: var s = bsonReader.ReadString(); if ((s.Length % 2) != 0) { s = "0" + s; // prepend a zero to make length even } bytes = new byte[s.Length / 2]; for (int i = 0; i < s.Length; i += 2) { var hex = s.Substring(i, 2); var b = byte.Parse(hex, NumberStyles.HexNumber); bytes[i / 2] = b; } return bytes; default: message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// private methods private Type GetActualType(BsonReader bsonReader) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); if (bsonReader.FindElement("type")) { var type = bsonReader.ReadString(); bsonReader.ReturnToBookmark(bookmark); switch (type) { case "link": return typeof(GeoJsonLinkedCoordinateReferenceSystem); case "name": return typeof(GeoJsonNamedCoordinateReferenceSystem); default: var message = string.Format("The type field of the GeoJsonCoordinateReferenceSystem is not valid: '{0}'.", type); throw new FormatException(message); } } else { throw new FormatException("GeoJsonCoordinateReferenceSystem object is missing the type field."); } }
// 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(IPEndPoint)); BsonType bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.String: var stringValue = bsonReader.ReadString(); var match = Regex.Match(stringValue, @"^(?<address>(.+|\[.*\]))\:(?<port>\d+)$"); if (match.Success) { IPAddress address; if (IPAddress.TryParse(match.Groups["address"].Value, out address)) { int port; if (int.TryParse(match.Groups["port"].Value, out port)) { return new IPEndPoint(address, port); } } } message = string.Format("Invalid IPEndPoint value '{0}'.", stringValue); throw new FileFormatException(message); default: message = string.Format("Cannot deserialize IPEndPoint from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public override object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { dynamic item = new ebayItemDataForSeller(); IDictionary<String, Object> dict = new Dictionary<String, Object>(); bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var elementName = bsonReader.ReadName(); var pi = nominalType.GetProperty(elementName); if (pi != null) { dynamic data = null; if (bsonReader.CurrentBsonType.Equals(BsonType.Null)) { bsonReader.ReadNull(); } else { data = BsonDefaultSerializer.Instance.GetSerializer(pi.PropertyType).Deserialize(bsonReader, pi.PropertyType, options); this.GetType().GetProperty(pi.Name).SetValue(item, data, null); } } else { if (bsonReader.CurrentBsonType.Equals(BsonType.Null)) { bsonReader.SkipValue(); } else dict[elementName] = bsonReader.ReadString(); } } bsonReader.ReadEndDocument(); item.SetDynamics(dict); return this; }
/// <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.Null: bsonReader.ReadNull(); return null; case BsonType.String: return BsonSymbol.Create(bsonReader.ReadString()); case BsonType.Symbol: return BsonSymbol.Create(bsonReader.ReadSymbol()); default: var message = string.Format("Cannot deserialize BsonSymbol from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// explicit interface implementation object IBsonSerializable.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { if (bsonReader.CurrentBsonType == Bson.BsonType.Null) { bsonReader.ReadNull(); return null; } else { bsonReader.ReadStartDocument(); BsonType bsonType; while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "abbreviated": _abbreviated = bsonReader.ReadString(); break; case "client": _client = bsonReader.ReadString(); break; case "command": _command = BsonDocument.ReadFrom(bsonReader); break; case "cursorid": _cursorId = BsonValue.ReadFrom(bsonReader).ToInt64(); break; case "err": _error = bsonReader.ReadString(); break; case "exception": _exception = bsonReader.ReadString(); break; case "exceptionCode": _exceptionCode = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "exhaust": _exhaust = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "fastmod": _fastMod = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "fastmodinsert": _fastModInsert = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "idhack": _idHack = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "info": _info = bsonReader.ReadString(); break; case "keyUpdates": _keyUpdates = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "millis": _duration = TimeSpan.FromMilliseconds(BsonValue.ReadFrom(bsonReader).ToDouble()); break; case "moved": _moved = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "nreturned": _numberReturned = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ns": _namespace = bsonReader.ReadString(); break; case "nscanned": _numberScanned = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ntoreturn": _numberToReturn = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ntoskip": _numberToSkip = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "op": _op = bsonReader.ReadString(); break; case "query": _query = BsonDocument.ReadFrom(bsonReader); break; case "responseLength": _responseLength = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "scanAndOrder": _scanAndOrder = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "ts": _timestamp = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()); break; case "updateobj": _updateObject = BsonDocument.ReadFrom(bsonReader); break; case "upsert": _upsert = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "user": _user = bsonReader.ReadString(); break; default: break; // ignore unknown elements } } bsonReader.ReadEndDocument(); return this; } }
public void TestStringEmpty() { var json = "\"\""; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.String, bsonReader.ReadBsonType()); Assert.AreEqual("", bsonReader.ReadString()); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize<string>(new StringReader(json)).ToJson()); }
/// <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; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else { var dateTimeOptions = (options == null) ? DateTimeSerializationOptions.Defaults : (DateTimeSerializationOptions) options; long? millisecondsSinceEpoch = null; long? ticks = null; switch (bsonType) { case BsonType.DateTime: millisecondsSinceEpoch = bsonReader.ReadDateTime(); break; case BsonType.Document: bsonReader.ReadStartDocument(); millisecondsSinceEpoch = bsonReader.ReadDateTime("DateTime"); bsonReader.ReadName("Ticks"); var ticksValue = BsonValue.ReadFrom(bsonReader); if (!ticksValue.IsBsonUndefined) { ticks = ticksValue.ToInt64(); } bsonReader.ReadEndDocument(); break; case BsonType.Int64: ticks = bsonReader.ReadInt64(); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono DateTime dateTime; if (dateTimeOptions.DateOnly) { dateTime = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", }; dateTime = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } ticks = dateTime.Ticks; break; default: var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType); throw new FileFormatException(message); } BsonDateTime bsonDateTime; if (ticks.HasValue) { bsonDateTime = BsonDateTime.Create(new DateTime(ticks.Value, DateTimeKind.Utc)); } else { bsonDateTime = BsonDateTime.Create(millisecondsSinceEpoch.Value); } if (dateTimeOptions.DateOnly) { var dateTime = bsonDateTime.Value; if (dateTime.TimeOfDay != TimeSpan.Zero) { throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero."); } bsonDateTime = BsonDateTime.Create(DateTime.SpecifyKind(dateTime, dateTimeOptions.Kind)); // not ToLocalTime or ToUniversalTime! } else { if (bsonDateTime.IsValidDateTime) { var dateTime = bsonDateTime.Value; switch (dateTimeOptions.Kind) { case DateTimeKind.Local: case DateTimeKind.Unspecified: dateTime = BsonUtils.ToLocalTime(dateTime, dateTimeOptions.Kind); break; case DateTimeKind.Utc: dateTime = BsonUtils.ToUniversalTime(dateTime); break; } bsonDateTime = BsonDateTime.Create(dateTime); } else { if (dateTimeOptions.Kind != DateTimeKind.Utc) { throw new FileFormatException("BsonDateTime is outside the range of .NET DateTime."); } } } return bsonDateTime; } }