Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom TypeDescriptor field #1982 #1983

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LiteDB/Client/Mapper/BsonMapper.Deserialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public object Deserialize(Type type, BsonValue value)
var doc = value.AsDocument;

// test if value is object and has _type
if (doc.TryGetValue("_type", out var typeField) && typeField.IsString)
if (doc.TryGetValue(BsonMapper.Global.TypeDescriptor, out var typeField) && typeField.IsString)
{
type = _typeNameBinder.GetType(typeField.AsString);

Expand Down
3 changes: 2 additions & 1 deletion LiteDB/Client/Mapper/BsonMapper.Serialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private BsonDocument SerializeObject(Type type, object obj, int depth)
// adding _type only where property Type is not same as object instance type
if (type != t)
{
doc["_type"] = new BsonValue(_typeNameBinder.GetName(t));
doc[BsonMapper.Global.TypeDescriptor] = new BsonValue(_typeNameBinder.GetName(t));
}

foreach (var member in entity.Members.Where(x => x.Getter != null))
Expand All @@ -194,6 +194,7 @@ private BsonDocument SerializeObject(Type type, object obj, int depth)
var value = member.Getter(obj);

if (value == null && this.SerializeNullValues == false && member.FieldName != "_id") continue;
// Console.WriteLine(member.FieldName + " " + member.DataType.Name);

// if member has a custom serialization, use it
if (member.Serialize != null)
Expand Down
11 changes: 7 additions & 4 deletions LiteDB/Client/Mapper/BsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public partial class BsonMapper
/// </summary>
public int MaxDepth { get; set; }

public string TypeDescriptor { get; set; }

/// <summary>
/// A custom callback to change MemberInfo behavior when converting to MemberMapper.
/// Use mapper.ResolveMember(Type entity, MemberInfo property, MemberMapper documentMappedField)
Expand All @@ -119,6 +121,7 @@ public BsonMapper(Func<Type, object> customTypeInstantiator = null, ITypeNameBin
this.ResolveCollectionName = (t) => Reflection.IsEnumerable(t) ? Reflection.GetListItemType(t).Name : t.Name;
this.IncludeFields = false;
this.MaxDepth = 20;
this.TypeDescriptor = "_type";

_typeInstantiator = customTypeInstantiator ?? ((Type t) => null);
_typeNameBinder = typeNameBinder ?? DefaultTypeNameBinder.Instance;
Expand Down Expand Up @@ -524,7 +527,7 @@ private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, IT
doc["_id"] = idRef;
if (doc.ContainsKey("$type"))
{
doc["_type"] = bson["$type"];
doc[BsonMapper.Global.TypeDescriptor] = bson["$type"];
}

return m.Deserialize(entity.ForType, doc);
Expand All @@ -534,7 +537,7 @@ private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, IT
{
return m.Deserialize(entity.ForType,
doc.ContainsKey("$type") ?
new BsonDocument { ["_id"] = idRef, ["_type"] = bson["$type"] } :
new BsonDocument { ["_id"] = idRef, [BsonMapper.Global.TypeDescriptor] = bson["$type"] } :
new BsonDocument { ["_id"] = idRef }); // if has $id, deserialize object using only _id object
}

Expand Down Expand Up @@ -609,7 +612,7 @@ private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, IT
item["_id"] = idRef;
if (item.AsDocument.ContainsKey("$type"))
{
item["_type"] = item["$type"];
item[BsonMapper.Global.TypeDescriptor] = item["$type"];
}

result.Add(item);
Expand All @@ -620,7 +623,7 @@ private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, IT

if (item.AsDocument.ContainsKey("$type"))
{
bsonDocument["_type"] = item["$type"];
bsonDocument[BsonMapper.Global.TypeDescriptor] = item["$type"];
}

result.Add(bsonDocument);
Expand Down