Serialization - view a graph of an object from a stream

I am wondering if there is a way to create a tree / view of a serialized graph of objects and does anyone have pointers? EDIT . The goal is that for some reason we are faced with the problem of de-serialization so that we can actually view / prepare a report on serialized data to help us determine the cause of the problem before debugging the code. In addition, I want to expand this in the future to take two threads (version 1, version 2) and highlight the differences between them, to ensure that we do not accidentally delete interesting information during code changes. / EDIT

Traditionally, we used Soap or XML serialization, but they become too limited for our needs, and binary serialization usually does all we need. The reason this has not been accepted is that it is much more difficult to view serialized content to help fix update problems, etc.

So, I started looking for an attempt to create a representation of serialized information. I can do this from the ISERializable constructor to a certain extent:

public A(SerializationInfo info, StreamingContext context) {} 

Given serialization information, I can flip the m_data member and see the actual serialized content. The problem with this approach is

  • It only displays a branch from a tree, I want to display the whole tree from the root, and it is really impossible to do from this position.
  • This is not a convenient place for interrogating information; I would like to pass the stream to the class and do the work there.

I saw the ObjectManager class, but this works on the existing object graph, whereas I need to be able to work from the data stream. I looked at BinaryFormatted, which uses ObjectReader and __BinaryParser, connecting to the ObjectManager (which, I think, will contain all the contents, possibly in a flat list), but to reproduce this or call it all through reflection (2 of these three classes are internal ) seems to be quite a bit of work, so I'm wondering if there is a better approach.

+6
source share
2 answers

You can put a List<Child class> in each parent class (even if there is one)

and when you create a child, you immediately put him on this list or even better declare him by adding him to the list

for instance

 ListName.Add(new Child(Constructer args)); 

Using this, you will serialize them as a single file that contains a hierarchy of objects and the objects themselves.

If the parent and child classes are the same, there is no reason why you cannot have a dynamic and multi-level hierarchy.

+1
source

To achieve what you described, you would have to deserialize the entire object graph from the stream, not knowing the type from which it was serialized. But this is not possible because the serializer does not store such information. AFAIK works as follows. Suppose you have several types:

 class A { bool p1 } class B { string p1; string p2; A p3} // instantiate them: var b = new B { p1 = "ppp1", p2 = "ppp2", p3 = new A { p1 = true} }; 

When the serializer writes this object, it starts to draw a graph of the object in a certain order (I guess in alphabetical order) and writes the type of the object, and then its contents. So your binary stream will look like this:

 [B:[string:ppp1][string:ppp2][A:[bool:true]]] 

You see, there are only values ​​and their types. But the order is implicit - as it is written. So, if you change your object B, suppose that

 class B { A p1; string p3; string p3;} 

Serialzer will not work because it will try to determine the instance of the string (which was first serialized) for a pointer to A. You can try to reproduce the process of creating a binary serialization, then you can create a dynamic tree of serialized objects. But this will require considerable effort.

For this purpose, I would create a class like this:

 class Node { public string NodeType; public List<Node> Children; public object NodeValue; } 

Then, while you are reading from the stream, you can create these nodes and recreate the entire serialized tree and analyze it.

0
source

Source: https://habr.com/ru/post/901204/


All Articles