2012-03-20 13 views
8

मैं मोंगोडीबी के लिए नया हूं, और सी # चालक को एफ # कक्षाओं को क्रमबद्ध करने के लिए काम करने की कोशिश कर रहा हूं। मैं इसे म्यूटेबल एफ # फ़ील्ड्स & पैरामीटर रहित कन्स्ट्रक्टर का उपयोग कर क्लास ऑटोमैपर के साथ काम कर रहा हूं, लेकिन वास्तव में मुझे अपरिवर्तनीयता बरकरार रखने की आवश्यकता है, इसलिए मैंने कस्टम सीरियलाइजेशन करने के लिए आईबीसनसेरियलाइज़र को कार्यान्वित करना शुरू कर दिया। मुझे इनमें से किसी एक को लिखने के लिए कोई दस्तावेज नहीं मिला है इसलिए मैंने ड्राइवर स्रोत कोड से अनुमान लगाने की कोशिश की है।मोंगोडीबी कस्टम सीरियलाइज़र कार्यान्वयन

मैंने एक समस्या में भाग लिया है, जब सेरियलाइज़र पर Deserialize विधि को बुलाया जाता है, तो CurrentBsonType शुरुआत के बजाय EndOfDocument पर सेट है जैसा कि मैं उम्मीद कर रहा हूं। मैंने यह सुनिश्चित करने के लिए सी # में बराबर लिखा था कि यह कुछ एफ # अजीबता नहीं थी, लेकिन समस्या बनी हुई है। क्रमबद्धता भाग ठीक काम करता प्रतीत होता है और खोल से पूछताछ योग्य है।

class Calendar { 
    public string Id { get; private set; } 
    public DateTime[] Holidays { get; private set; } 

    public Calendar(string id, DateTime[] holidays) { 
     Id = id; 
     Holidays = holidays; 
    } 
} 

class CalendarSerializer : BsonBaseSerializer { 
    public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { 
     var calendar = (Calendar) value; 
     bsonWriter.WriteStartDocument(); 
     bsonWriter.WriteString("_id", calendar.Id); 
     bsonWriter.WriteName("holidays"); 
     var ser = new ArraySerializer<DateTime>(); 
     ser.Serialize(bsonWriter, typeof(DateTime[]), calendar.Holidays, null); 
     bsonWriter.WriteEndDocument(); 
    } 

    public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { 
     if (nominalType != typeof(Calendar) || actualType != typeof(Calendar)) 
      throw new BsonSerializationException(); 

     if (bsonReader.CurrentBsonType != BsonType.Document) 
      throw new FileFormatException(); 

     bsonReader.ReadStartDocument(); 
     var id = bsonReader.ReadString("_id"); 
     var ser = new ArraySerializer<DateTime>(); 
     var holidays = (DateTime[])ser.Deserialize(bsonReader, typeof(DateTime[]), null); 
     bsonReader.ReadEndDocument(); 
     return new Calendar(id, holidays); 
    } 

    public override bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator) { 
     var calendar = (Calendar) document; 
     id = calendar.Id; 
     idNominalType = typeof (string); 
     idGenerator = new StringObjectIdGenerator(); 
     return true; 
    } 

    public override void SetDocumentId(object document, object id) { 
     throw new NotImplementedException("SetDocumentId is not implemented"); 
    } 
} 

यह deserialize में FileFormatException के साथ चल रही है जब CurrentBsonType दस्तावेज़ नहीं है: यहाँ नमूना कोड है। मैं ड्राइवर स्रोत के नवीनतम संस्करण 1.4 का उपयोग कर रहा हूं।

उत्तर

7

मैंने इसे अंत में समझ लिया। मुझे bsonReader.GetCurrentBsonType() का उपयोग करना चाहिए bsonReader.CurrentBsonType के बजाय। यह बस आखिरी चीज़ को देखने के बजाय बफर से BsonType को पढ़ता है। मैंने बाद में बग derserializing भी तय किया। अद्यतन विधि इस तरह दिखती है:

public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { 
    if (nominalType != typeof(Calendar) || actualType != typeof(Calendar)) 
     throw new BsonSerializationException(); 

    if (bsonReader.GetCurrentBsonType() != BsonType.Document) 
     throw new FileFormatException(); 

    bsonReader.ReadStartDocument(); 
    var id = bsonReader.ReadString("_id"); 
    bsonReader.ReadName(); 
    var ser = new ArraySerializer<DateTime>(); 
    var holidays = (DateTime[])ser.Deserialize(bsonReader, typeof(DateTime[]), null); 
    bsonReader.ReadEndDocument(); 
    return new Calendar(id, holidays); 
} 
संबंधित मुद्दे