Guest User

Untitled

a guest
Jul 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. public class Client
  2. {
  3. public string Name{ get; set; }
  4. public string Address{ get; set; }
  5. public List<ExtraField> ExtraFields{ get; set; } //these fields are extra ones
  6. }
  7.  
  8. public class ExtraField {
  9. public string Key{ get; set; }
  10. public string Type { get; set; }
  11. public string Value { get; set; } }
  12.  
  13. public static BsonValue ToBson(Client client)
  14. {
  15. if (client == null)
  16. return null;
  17.  
  18. var doc = new BsonDocument();
  19. doc["Name"] = client.Name;
  20. doc["Address"] = client.Address;
  21. foreach (var f in client.ExtraFields)
  22. {
  23. var fieldValue = new BsonDocument();
  24. fieldValue["Type"] = f.Type;
  25. fieldValue["Value"] = f.Value;
  26. doc[f.Key] = fieldValue;
  27. }
  28.  
  29. return doc;
  30. }
  31.  
  32. public static Client FromBson(BsonValue bson)
  33. {
  34. if (bson == null || !bson.IsBsonDocument)
  35. return null;
  36.  
  37. var doc = bson.AsBsonDocument;
  38.  
  39. var client = new Client
  40. {
  41. ExtraFields = new List<ExtraField>(),
  42. Address = doc["Address"].AsString,
  43. Name = doc["Name"].AsString
  44. };
  45. foreach (var name in doc.Names)
  46. {
  47. var val = doc[name];
  48. if (val is BsonDocument)
  49. {
  50. var fieldDoc = val as BsonDocument;
  51. var field = new ExtraField
  52. {
  53. Key = name,
  54. Value = fieldDoc["Value"].AsString,
  55. Type = fieldDoc["Type"].AsString
  56. };
  57. client.ExtraFields.Add(field);
  58. }
  59. }
  60.  
  61. return client;
  62. }
  63.  
  64. var server = MongoServer.Create("mongodb://localhost:27020");
  65. var database = server.GetDatabase("SO");
  66.  
  67. var clients = database.GetCollection<Type>("clients");
  68.  
  69.  
  70. var client = new Client() {Id = ObjectId.GenerateNewId().ToString()};
  71. client.Name = "Andrew";
  72. client.Address = "Address";
  73. client.ExtraFields = new List<ExtraField>();
  74. client.ExtraFields.Add(new ExtraField()
  75. {
  76. Key = "key1",
  77. Type = "type1",
  78. Value = "value1"
  79. });
  80. client.ExtraFields.Add(new ExtraField()
  81. {
  82. Key = "key2",
  83. Type = "type2",
  84. Value = "value2"
  85. });
  86.  
  87. //When inseting/saving use ToBson to serialize client
  88. clients.Insert(Client.ToBson(client));
  89.  
  90. //When reading back from the database use FromBson method:
  91. var fromDb = Client.FromBson(clients.FindOneAs<BsonDocument>());
  92.  
  93. {
  94. "_id" : ObjectId("4e3a66679c66673e9c1da660"),
  95. "Name" : "Andrew",
  96. "Address" : "Address",
  97. "key1" : {
  98. "Type" : "type1",
  99. "Value" : "value1"
  100. },
  101. "key2" : {
  102. "Type" : "type2",
  103. "Value" : "value2"
  104. }
  105. }
  106.  
  107. using System;
  108. using System.Collections.Generic;
  109. using System.Linq; using System.Text;
  110. using MongoDB.Bson;
  111. using MongoDB.Bson.Serialization;
  112. using MongoDB.Bson.Serialization.Serializers;
  113.  
  114. namespace TestDataGeneration {
  115. public class FieldsWrapper : IBsonSerializable
  116. {
  117. public List<DataFieldValue> DataFieldValues { get; set; }
  118.  
  119.  
  120. public object Deserialize(MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
  121. {
  122. if (nominalType != typeof(FieldsWrapper)) throw new ArgumentException("Cannot deserialize anything but self");
  123. var doc = BsonDocument.ReadFrom(bsonReader);
  124. var list = new List<DataFieldValue>();
  125. foreach (var name in doc.Names)
  126. {
  127. var val = doc[name];
  128. if (val.IsString)
  129. list.Add(new DataFieldValue {LocalIdentifier = name, Values = new List<string> {val.AsString}});
  130. else if (val.IsBsonArray)
  131. {
  132. DataFieldValue df = new DataFieldValue {LocalIdentifier = name};
  133. foreach (var elem in val.AsBsonArray)
  134. {
  135. df.Values.Add(elem.AsString);
  136. }
  137. list.Add(df);
  138. }
  139. }
  140. return new FieldsWrapper {DataFieldValues = list};
  141. }
  142.  
  143.  
  144. public void Serialize(MongoDB.Bson.IO.BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
  145. {
  146. if (nominalType != typeof (FieldsWrapper))
  147. throw new ArgumentException("Cannot serialize anything but self");
  148. bsonWriter.WriteStartDocument();
  149. foreach (var dataFieldValue in DataFieldValues)
  150. {
  151.  
  152. bsonWriter.WriteName(dataFieldValue.LocalIdentifier);
  153. if (dataFieldValue.Values.Count != 1)
  154. {
  155. var list = new string[dataFieldValue.Values.Count];
  156. for (int i = 0; i < dataFieldValue.Values.Count; i++)
  157. list[i] = dataFieldValue.Values[i];
  158. BsonSerializer.Serialize(bsonWriter, list);
  159. }
  160. else
  161. {
  162. BsonSerializer.Serialize(bsonWriter, dataFieldValue.Values[0]);
  163. }
  164. }
  165. bsonWriter.WriteEndDocument();
  166. }
  167.  
  168. } }
Add Comment
Please, Sign In to add comment