[英]Custom serilization of a complex C# object using simple objects
I am using a utility class from QuickGraph called AdjacencyGraph
that has a complex internal structure. 我正在使用QuickGraph的一个名为AdjacencyGraph
的实用程序类,它具有复杂的内部结构。
I do not care for all the internal properties of the complex class and I cant change it as its from a nuget package - at most in my database I am just concerned in storing an array of Dependency objects that can be used to reconstruct the AdacencyGraph: 我不关心复杂类的所有内部属性,我不能将其更改为来自nuget包 - 最多在我的数据库中我只关心存储可用于重建AdacencyGraph的Dependency对象数组:
public class Dependency
{
public Dependency(string source, string target)
{
this.Source = source;
this.Target = target;
}
public string Source { get; set; }
public string Target { get; set; }
}
On serialization, all i need to do is the following code: 在序列化时,我需要做的就是以下代码:
void Serialize(Dependencies toSerialize)
{
var toBeStored = toSerialize.GetAllDependencies();
// write this to mongodb somehow
}
And deserialization build the Dependencies object: 反序列化构建Dependencies对象:
Dependencies Deserialize(IEnumerable<Dependency> hydrateFrom)
{
var dependencies = new Dependencies();
foreach(var d in hydrateFrom)
{
dependencies.SetRequiresFor(d.Source, d.Target);
}
}
Question 题
How would I go about intercepting the serialization process and output? 我该如何拦截序列化过程和输出?
Other Information 其他信息
I have wrapped the AdjacencyGraph around in a class that lists all Dependency objects, and can accept a list of Dependency objects too. 我已经在一个列出所有Dependency对象的类中包装了AdjacencyGraph,并且也可以接受一个Dependency对象列表。
class Dependencies
{
private AdjacencyGraph<string, Edge<string>> relationshipGraph = new AdjacencyGraph<string, Edge<string>>();
public void SetRequiresFor(SkuId on, SkuId requires)
{
var toAdd = new Edge<string>(on.Value, requires.Value);
this.relationshipGraph.AddVerticesAndEdge(toAdd);
}
public IEnumerable<Dependency> GetAllDependencies()
{
foreach(var edge in this.relationshipGraph.Edges)
{
yield return new Dependency(edge.Source, edge.Target);
}
}
}
There are 3 things you need to do: 您需要做三件事:
BsonSerializer
. 实现BsonSerializer
。 BsonSerializationProvider
that will point Dependencies
to your serializer. 实现一个BsonSerializationProvider
,它将Dependencies
指向序列化程序。 * * public class DependenciesBsonSerializer : BsonBaseSerializer
{
public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
{
// implement using bsonWriter
}
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
{
// implement using bsonReader
}
}
public sealed class BsonSerializationProvider : IBsonSerializationProvider
{
public IBsonSerializer GetSerializer(Type type)
{
if (type == typeof(Dependncies)
{
return new DependenciesBsonSerializer ();
}
return null;
}
}
BsonSerializer.RegisterSerializationProvider(new BsonSerializationProvider());
* You could cut the provider and register the serializer directly using BsonSerializer.RegisterSerializer
but I still recommend grouping all your serializers in a single provider. *您可以使用BsonSerializer.RegisterSerializer
直接剪切提供程序并注册序列化程序,但我仍然建议将所有序列化程序分组到一个提供程序中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.