     
     func singleValueContainer() -> SingleValueEncodingContainer {
         return BSONSingleValueContainer(wrapper: wrapper)
+    }
+}
+
+fileprivate struct BSONKeyedEncodingContainer<K: CodingKey>: KeyedEncodingContainerProtocol {
+    typealias Key = K
+    var codingPath: [CodingKey] = []
+    
+    let encoder: _BSONEncoder
+    
+    var wrapper: PrimitiveWrapper {
+        return encoder.wrapper
+    }
+    
+    init(encoder: _BSONEncoder) {
+        self.encoder = encoder
+    }
+    
+    mutating func encodeNil(forKey key: K) throws {
+        wrapper.doc[key.stringValue] = Null()
+    }
+    
+    mutating func encode(_ value: Bool, forKey key: K) throws {
+        wrapper.doc[key.stringValue] = value
+    }
+    
+    mutating func encode(_ value: Int, forKey key: K) throws {
+        wrapper.doc[key.stringValue] = value
+    }
+    
+    mutating func encode(_ value: Int8, forKey key: K) throws {
+        wrapper.append(value, for: key.stringValue)
+    }
+    
+    mutating func encode(_ value: Int16, forKey key: K) throws {
+        wrapper.append(value, for: key.stringValue)
+    }
+    
+    mutating func encode(_ value: Int32, forKey key: K) throws {
+        wrapper.doc[key.stringValue] = value
+    }
+    
+    mutating func encode(_ value: Int64, forKey key: K) throws {
+        wrapper.append(value, for: key.stringValue)
+    }
+    
+    mutating func encode(_ value: UInt, forKey key: K) throws {
+        wrapper.append(value, for: key.stringValue)
+    }
+    
+    mutating func encode(_ value: UInt8, forKey key: K) throws {
+        wrapper.append(value, for: key.stringValue)
+    }
+    
+    mutating func encode(_ value: UInt16, forKey key: K) throws {
+        wrapper.append(value, for: key.stringValue)
+    }
+    
+    mutating func encode(_ value: UInt32, forKey key: K) throws {
+        wrapper.append(value, for: key.stringValue)
+    }
+    
+    mutating func encode(_ value: UInt64, forKey key: K) throws {
+        wrapper.append(value, for: key.stringValue)
+    }
+    
+    mutating func encode(_ value: Float, forKey key: K) throws {
+        wrapper.doc[key.stringValue] = Double(value)
+    }
+    
+    mutating func encode(_ value: Double, forKey key: K) throws {
+        wrapper.doc[key.stringValue] = value
+    }
+    
+    mutating func encode(_ value: String, forKey key: K) throws {
+        wrapper.doc[key.stringValue] = value
+    }
+    
+    mutating func encode<T>(_ value: T, forKey key: K) throws where T : Encodable {
+        wrapper.doc[key.stringValue] = try BSONEncoder().encodePrimitive(value)
+    }
+    
+    mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: K) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
+        return KeyedEncodingContainer(BSONKeyedEncodingContainer<NestedKey>(encoder: self.wrapper.keyedSubEncoder(for: key.stringValue)))
+    }
+    
+    mutating func nestedUnkeyedContainer(forKey key: K) -> UnkeyedEncodingContainer {
+        return BSONUnkeyedEncodingContainer(encoder: self.wrapper.keyedSubEncoder(for: key.stringValue))
+    }
+    
+    mutating func superEncoder() -> Encoder {
+        return encoder
+    }
+    
+    mutating func superEncoder(forKey key: K) -> Encoder {
+        return encoder
     }
 }
 

    }
    
    mutating func encode(_ value: Int64) throws {
        try wrapper.encode(value)
    }
    
    mutating func encode(_ value: UInt) throws {
        try wrapper.encode(value)
    }
    
    mutating func encode(_ value: UInt8) throws {
        try wrapper.encode(value)
    }
    
    mutating func encode(_ value: UInt16) throws {
        try wrapper.encode(value)
    }
    
    mutating func encode(_ value: UInt32) throws {
        try wrapper.encode(value)
    }
    
    mutating func encode(_ value: UInt64) throws {
        try wrapper.encode(value)
    }
    
    mutating func encode(_ value: Float) throws {

    }
    
    mutating func encode(_ value: Int8) throws {
        wrapper.encode(value)
    }
    
    mutating func encode(_ value: Int16) throws {
        wrapper.encode(value)
    }
    
    mutating func encode(_ value: Int32) throws {
