public override Vehicle Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { var serializer = BsonSerializer.LookupSerializer(typeof(BsonDocument)); var document = serializer.Deserialize(context, args); var bsonDocument = document.ToBsonDocument(); var result = BsonExtensionMethods.ToJson(bsonDocument); var jObject = JObject.Parse(result); jObject.Remove("_id"); var documentWithNoId = jObject.ToString(); //switch (mvnt.GetType().Name) //{ // case "Tiger": // //your serialization here // break; // case "Zebra": // //your serialization here // break; // default: // break; //} return(JsonConvert.DeserializeObject <Vehicle>(documentWithNoId)); }
public JArray Get() { /* JObject x =new JObject(); * x["operationName"] = "Liran - Server"; * x["date"] = "23/2/2015"; * * JObject y = new JObject(); * y["operationName"] = "Ziv - Server"; * y["date"] = "23/2/2015"; * * JArray result = new JArray(); * * result.Add(x); * result.Add(y); * * return result; * //return new string[] { "value1", "value2" }; */ IMongoCollection <BsonDocument> collection = Dal.getInstance().GetCollection("Persons"); var resultBson = collection.Find(new BsonDocument()).ToList(); string stringJson = BsonExtensionMethods.ToJson(resultBson); JArray json = JsonConvert.DeserializeObject <JArray>(stringJson); return(json); }
public Consult GetConsult(string id) { var consult = _consults.Find <Consult>(cons => cons.Id == id).FirstOrDefault(); var pet = this.GetPet(consult.PetID); var vet = this.GetVet(consult.VetID); if (pet != null) { consult.PetName = pet.Name; } else { consult.PetName = ""; } if (vet != null) { consult.VetName = vet.Name; } else { consult.VetName = ""; } var bsonDoc = BsonExtensionMethods.ToJson(consult.MedsBson); consult.Meds = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(bsonDoc); bsonDoc = BsonExtensionMethods.ToJson(consult.VaccsBson); consult.Vaccs = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(bsonDoc); consult.OwnerName = pet.Owner_Name; consult.Genus = pet.Genus; return(consult); }
public List <Consult> GetAllConsultsFor(string petID) { var consults = _consults.Find(consult => consult.PetID == petID).ToList(); foreach (Consult consult in consults) { var pet = this.GetPet(consult.PetID); var vet = this.GetVet(consult.VetID); if (pet != null) { consult.PetName = pet.Name; } else { consult.PetName = ""; } if (vet != null) { consult.VetName = vet.Name; } else { consult.VetName = ""; } var bsonDoc = BsonExtensionMethods.ToJson(consult.MedsBson); consult.Meds = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(bsonDoc); bsonDoc = BsonExtensionMethods.ToJson(consult.VaccsBson); consult.Vaccs = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(bsonDoc); consult.Genus = pet.Genus; consult.OwnerName = pet.Owner_Name; } return(consults); }
public override string Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { var bsonReader = context.Reader; var serializer = BsonSerializer.LookupSerializer(typeof(TObject)); var obj = (TObject)serializer.Deserialize(context); return(obj == null ? null : BsonExtensionMethods.ToJson(obj)); }
public override Dictionary <string, object> Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { var serializer = BsonSerializer.LookupSerializer(typeof(BsonDocument)); var document = serializer.Deserialize(context, args); var bsonDocument = document.ToBsonDocument(); var result = BsonExtensionMethods.ToJson(bsonDocument); return(JsonConvert.DeserializeObject <Dictionary <string, object> >(result)); }
public static string ToClientJson(this IEnumerable <BsonDocument> document) { var sb = new StringBuilder(); sb.Append("["); var all = document.Select(x => RemoveObjectId.Replace(BsonExtensionMethods.ToJson <BsonDocument>(x), "$2")); sb.Append(string.Join(",", all)); sb.Append("]"); return(sb.ToString()); }
public static string ToJson(this object obj) { if (obj is string) { return("{ \"_t\": \"System.String\", \"_v\": " + BsonExtensionMethods.ToJson <string>(obj as string) + "}"); } var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; return(Convert.ToBson(obj).ToJson <BsonDocument>(jsonWriterSettings)); }
private JArray GetData(string collection, string bsonQuery, FindOptions <BsonDocument> findOptions) { IMongoCollection <BsonDocument> query = _mongoDataBase.GetCollection <BsonDocument>(collection); JArray resultData = new JArray(); using (var cursor = query.FindAsync <BsonDocument>(GetFilter(bsonQuery), findOptions).Result) { while (cursor.MoveNext()) { var batch = cursor.Current; foreach (BsonDocument document in batch) { var documentJson = BsonExtensionMethods.ToJson(document, new MongoDB.Bson.IO.JsonWriterSettings { OutputMode = MongoDB.Bson.IO.JsonOutputMode.Strict }); var jobjData = JObject.Parse(documentJson); jobjData.Remove(CommonConst.CommonField.ID); resultData.Add(jobjData); } } } return(resultData); }
public static string ToJson(this object obj) { if (obj == null) { return(""); } if (obj is string) { return("{ \"_t\": \"System.String\", \"_v\": " + BsonExtensionMethods.ToJson <string>(obj as string) + "}"); } if (obj is IEnumerable && !(obj is IObject) && !(obj is IDictionary)) { return(ToJsonArray((obj as IEnumerable).OfType <object>())); } var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; return(Convert.ToBson(obj).ToJson <BsonDocument>(jsonWriterSettings)); }
private Dictionary <int, int> DeserializeBsonDoc(BsonDocument doc) { var bsonDoc = BsonExtensionMethods.ToJson(doc); return(JsonConvert.DeserializeObject <Dictionary <int, int> >(bsonDoc)); }
private static List <string> SerializeWithMongoDbDriver <T>(List <T> objects) { return(objects.Select(o => BsonExtensionMethods.ToJson(o)).ToList()); }