/// <summary> /// Deserializes a <c>Document</c> /// </summary> /// <returns>A new object deserialized.</returns> /// <param name="db">The <c>Database</c> where the Document is stored.</param> /// <param name="doc">The document to deserialize.</param> /// <param name = "serializer">The serializer to use when deserializing the object</param> /// <typeparam name="T">The 1st type parameter.</typeparam> internal static object DeserializeObject(Type type, Document doc, Database db, IDReferenceResolver resolver = null) { JObject jo = JObject.FromObject (doc.Properties); JsonSerializer serializer = GetSerializer (type, doc.CurrentRevision, db, resolver, GetLocalTypes (type)); return jo.ToObject (type, serializer); }
/// <summary> /// Deserializes a <c>Document</c> /// </summary> /// <returns>A new object deserialized.</returns> /// <param name="db">The <c>Database</c> where the Document is stored.</param> /// <param name="doc">The document to deserialize.</param> /// <param name = "serializer">The serializer to use when deserializing the object</param> /// <typeparam name="T">The 1st type parameter.</typeparam> internal static object DeserializeObject(Type type, Document doc, Database db, IDReferenceResolver resolver = null) { JObject jo = JObject.FromObject(doc.Properties); JsonSerializer serializer = GetSerializer(type, doc.CurrentRevision, db, resolver, GetLocalTypes(type)); return(jo.ToObject(type, serializer)); }
public StorablesConverter(Database db, IDReferenceResolver idResolver, List <Type> localTypes) { this.db = db; this.idResolver = idResolver; this.localTypes = localTypes; if (this.localTypes == null) { this.localTypes = new List <Type> (); } }
/// <summary> /// Serializes an object into a <c>JObject</c>. /// </summary> /// <returns>A new object serialized.</returns> /// <param name="obj">The <c>IStorable</c> to serialize.</param> /// <param name="rev">The document revision to serialize.</param> /// <param name="localStorables">A list of <see cref="LongoMatch.Core.Interfaces.IStorable"/> /// types that should be serialized as local referencies instead of by document ID.</param> internal static JObject SerializeObject(IStorable obj, Revision rev, Database db, IDReferenceResolver resolver = null) { JsonSerializer serializer = GetSerializer(obj.GetType(), rev, db, resolver, GetLocalTypes(obj.GetType())); JObject jo = JObject.FromObject(obj, serializer); jo ["DocType"] = obj.GetType().Name; return(jo); }
public static void SaveObject(IStorable obj, Database db, IDReferenceResolver resolver = null) { Document doc = db.GetDocument (obj.ID.ToString ()); doc.Update ((UnsavedRevision rev) => { JObject jo = SerializeObject (obj, rev, db, resolver); IDictionary<string, object> props = jo.ToObject<IDictionary<string, object>> (); /* SetProperties sets a new properties dictionary, removing the attachments we * added in the serialization */ if (rev.Properties.ContainsKey ("_attachments")) { props ["_attachments"] = rev.Properties ["_attachments"]; } rev.SetProperties (props); return true; }); }
public static void SaveObject(IStorable obj, Database db, IDReferenceResolver resolver = null) { Document doc = db.GetDocument(obj.ID.ToString()); doc.Update((UnsavedRevision rev) => { JObject jo = SerializeObject(obj, rev, db, resolver); IDictionary <string, object> props = jo.ToObject <IDictionary <string, object> > (); /* SetProperties sets a new properties dictionary, removing the attachments we * added in the serialization */ if (rev.Properties.ContainsKey("_attachments")) { props ["_attachments"] = rev.Properties ["_attachments"]; } rev.SetProperties(props); return(true); }); }
static JsonSerializer GetSerializer(Type serType, Revision rev, Database db, IDReferenceResolver resolver, List <Type> localTypes) { if (resolver == null) { resolver = new IDReferenceResolver(); } if (localTypes == null) { localTypes = new List <Type> (); } localTypes.Add(serType); JsonSerializerSettings settings = new JsonSerializerSettings(); settings.Formatting = Formatting.Indented; settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects; settings.TypeNameHandling = TypeNameHandling.Objects; settings.Converters.Add(new ImageConverter(rev)); settings.Converters.Add(new VersionConverter()); settings.Converters.Add(new StorablesConverter(db, resolver, localTypes)); settings.Converters.Add(new LongoMatchConverter(false)); settings.ReferenceResolver = resolver; return(JsonSerializer.Create(settings)); }
public static object LoadObject(Type objType, Guid id, Database db, IDReferenceResolver resolver = null) { Document doc = db.GetExistingDocument(id.ToString()); return(DeserializeObject(objType, doc, db, resolver)); }
public static object LoadObject(Type objType, Guid id, Database db, IDReferenceResolver resolver = null) { Document doc = db.GetExistingDocument (id.ToString ()); return DeserializeObject (objType, doc, db, resolver); }
public StorablesConverter(Database db, IDReferenceResolver idResolver, List<Type> localTypes) { this.db = db; this.idResolver = idResolver; this.localTypes = localTypes; if (this.localTypes == null) { this.localTypes = new List<Type> (); } }
static JsonSerializer GetSerializer(Type serType, Revision rev, Database db, IDReferenceResolver resolver, List<Type> localTypes) { if (resolver == null) { resolver = new IDReferenceResolver (); } if (localTypes == null) { localTypes = new List<Type> (); } localTypes.Add (serType); JsonSerializerSettings settings = new JsonSerializerSettings (); settings.Formatting = Formatting.Indented; settings.PreserveReferencesHandling = PreserveReferencesHandling.Objects; settings.TypeNameHandling = TypeNameHandling.Objects; settings.Converters.Add (new ImageConverter (rev)); settings.Converters.Add (new VersionConverter ()); settings.Converters.Add (new StorablesConverter (db, resolver, localTypes)); settings.Converters.Add (new LongoMatchConverter (false)); settings.ReferenceResolver = resolver; return JsonSerializer.Create (settings); }
/// <summary> /// Serializes an object into a <c>JObject</c>. /// </summary> /// <returns>A new object serialized.</returns> /// <param name="obj">The <c>IStorable</c> to serialize.</param> /// <param name="rev">The document revision to serialize.</param> /// <param name="localStorables">A list of <see cref="LongoMatch.Core.Interfaces.IStorable"/> /// types that should be serialized as local referencies instead of by document ID.</param> internal static JObject SerializeObject(IStorable obj, Revision rev, Database db, IDReferenceResolver resolver = null) { JsonSerializer serializer = GetSerializer (obj.GetType (), rev, db, resolver, GetLocalTypes (obj.GetType ())); JObject jo = JObject.FromObject (obj, serializer); jo ["DocType"] = obj.GetType ().Name; return jo; }