Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:10:37+00:00 2026-06-06T04:10:37+00:00

I’ve a problem with serializing an object using protobuf.net. I’ve used it on other

  • 0

I’ve a problem with serializing an object using protobuf.net. I’ve used it on other classes and it works very well, but using this it doesn’t.

Could you help me saying why. Thanks.

I want to use protobuf because BinaryFormatter is very slow in serializing/deserializing.

This is the class:

using System.Collections.Generic;
using System;
using ProtoBuf;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace RadixTree
{
[Serializable, DataContract, ProtoContract]
public class Node<T>
{
    private readonly List<Node<T>> children = new List<Node<T>>();
    private readonly string key;
    private T value;
    public Node(string key, T value)
    {
        this.key = key;
        this.value = value;
    }
    private Node()
    {
    }
    protected bool HasChildren
    {
        get { return children.Count > 0; }
    }
    public void Insert(string key, T value)
    {
        var potentialChild = new Node<T>(key, value);
        Add(potentialChild);
    }
    private bool Add(Node<T> theNewChild)
    {
        if (Contains(theNewChild))
            throw new DuplicateKeyException(string.Format("Duplicate key: '{0}'", theNewChild.key));

        if (!IsParentOf(theNewChild))
            return false;

        bool childrenObligedRequest = RequestChildrenToOwn(theNewChild);
        if (childrenObligedRequest) return true;

        AcceptAsOwnChild(theNewChild);
        return true;
    }
    private bool RequestChildrenToOwn(Node<T> newChild)
    {
        return
            children.Exists(
                existingChild =>
                existingChild.MergeIfSameAs(newChild) || existingChild.Add(newChild) ||
                ForkANewChildAndAddChildren(existingChild, newChild));
    }
    private bool MergeIfSameAs(Node<T> potentialChild)
    {
        if (!IsTheSameAs(potentialChild) || IsNotUnrealNode())
            return false;

        value = potentialChild.value;
        return true;
    }
    private bool IsNotUnrealNode()
    {
        return !IsUnrealNode();
    }
    private void Disown(Node<T> existingChild)
    {
        children.Remove(existingChild);
    }
    private Node<T> AcceptAsOwnChild(Node<T> child)
    {
        if (NotItself(child)) children.Add(child);
        return this;
    }
    private bool NotItself(Node<T> child)
    {
        return !Equals(child);
    }
    private bool ForkANewChildAndAddChildren(Node<T> existingChild, Node<T> newChild)
    {
        if (existingChild.IsNotMySibling(newChild))
            return false;

        var surrogateParent = MakeASurrogateParent(existingChild, newChild);
        if (surrogateParent.IsTheSameAs(this))
            return false;

        SwapChildren(existingChild, newChild, surrogateParent);
        return true;
    }
    private bool IsNotMySibling(Node<T> newChild)
    {
        return !IsMySibling(newChild);
    }
    private void SwapChildren(Node<T> existingChild, Node<T> newChild, Node<T> surrogateParent)
    {
        surrogateParent.AcceptAsOwnChild(existingChild)
            .AcceptAsOwnChild(newChild);

        AcceptAsOwnChild(surrogateParent);
        Disown(existingChild);
    }
    private Node<T> MakeASurrogateParent(Node<T> existingChild, Node<T> newChild)
    {
        string keyForNewParent = existingChild.CommonBeginningInKeys(newChild);
        keyForNewParent = keyForNewParent.Trim();
        var surrogateParent = new Node<T>(keyForNewParent, default(T));

        return surrogateParent.IsTheSameAs(newChild) ? newChild : surrogateParent;
    }
    private bool IsTheSameAs(Node<T> parent)
    {
        return Equals(parent);
    }
    private bool IsMySibling(Node<T> potentialSibling)
    {
        return CommonBeginningInKeys(potentialSibling).Length > 0;
    }
    private string CommonBeginningInKeys(Node<T> potentialSibling)
    {
        return key.CommonBeginningWith(potentialSibling.key);
    }
    internal virtual bool IsParentOf(Node<T> potentialChild)
    {
        return potentialChild.key.StartsWith(key);
    }
    public bool Delete(string key)
    {
        Node<T> nodeToBeDeleted = children.Find(child => child.Find(key) != null);
        if (nodeToBeDeleted == null) return false;

        if (nodeToBeDeleted.HasChildren)
        {
            nodeToBeDeleted.MarkAsUnreal();
            return true;
        }

        children.Remove(nodeToBeDeleted);
        return true;
    }
    private void MarkAsUnreal()
    {
        value = default(T);
    }
    public T Find(string key)
    {
        var childBeingSearchedFor = new Node<T>(key, default(T));
        return Find(childBeingSearchedFor);
    }
    private T Find(Node<T> childBeingSearchedFor)
    {
        if (Equals(childBeingSearchedFor)) return value;
        T node = default(T);
        children.Find(child =>
                          {
                              node = child.Find(childBeingSearchedFor);
                              return node != null;
                          });
        if (node == null) return default(T);
        return node;
    }
    public bool Contains(string key)
    {
        return Contains(new Node<T>(key, default(T)));
    }
    private bool Contains(Node<T> child)
    {
        if (Equals(child) && IsUnrealNode()) return false;

        if (Equals(child)) return true;

        return children.Exists(node => node.Contains(child));
    }
    private bool IsUnrealNode()
    {
        return value == null;
    }
    public List<T> Search(string keyPrefix)
    {
        var nodeBeingSearchedFor = new Node<T>(keyPrefix, default(T));
        return Search(nodeBeingSearchedFor);
    }
    private List<T> Search(Node<T> nodeBeingSearchedFor)
    {
        if (IsTheSameAs(nodeBeingSearchedFor))
            return MeAndMyDescendants();

        return SearchInMyChildren(nodeBeingSearchedFor);
    }
    private List<T> SearchInMyChildren(Node<T> nodeBeingSearchedFor)
    {
        List<T> searchResults = null;

        children.Exists(existingChild => (searchResults = existingChild.SearchUpAndDown(nodeBeingSearchedFor)).Count > 0);

        return searchResults;
    }
    private List<T> SearchUpAndDown(Node<T> node)
    {
        if (node.IsParentOf(this))
            return MeAndMyDescendants();

        return IsParentOf(node) ? Search(node) : new List<T>();

    }
    private List<T> MeAndMyDescendants()
    {
        var meAndMyDescendants = new List<T>();
        if (!IsUnrealNode())
            meAndMyDescendants.Add(value);

        children.ForEach(child => meAndMyDescendants.AddRange(child.MeAndMyDescendants()));
        return meAndMyDescendants;
    }
    public long Size()
    {
        const long size = 0;
        return Size(size);
    }
    private long Size(long size)
    {
        if (!IsUnrealNode())
            size++;

        children.ForEach(node => size += node.Size());
        return size;
    }
    public override string ToString()
    {
        return key;
    }
    public bool Equals(Node<T> other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(other.key, key);
    }
    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != typeof (Node<T>)) return false;
        return Equals((Node<T>) obj);
    }
    public override int GetHashCode()
    {
        return (key != null ? key.GetHashCode() : 0);
    }
    public static Node<T> Root()
    {
        return new RootNode<T>();
    }
    public List<Node<T>> getChildren()
    {
        return children;
    }
    [Serializable, DataContract, ProtoContract]
    private class RootNode<T> : Node<T>
    {
        public RootNode() { }
        internal override bool IsParentOf(Node<T> potentialChild)
        {
            return true;
        }
    }

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:10:40+00:00Added an answer on June 6, 2026 at 4:10 am

    Because protobuf-net, along with things like DataContractSerializer and XmlSerializer etc, does not simply work on fields. It needs information about which fields to serialize and how to identify them (although there is an implicit option, I try not to recommend it). For example:

    [ProtoMember(3)] private readonly List<Node<T>> children = new List<Node<T>>();
    [ProtoMember(1)] private readonly string key;
    [ProtoMember(2)] private T value;
    

    Which should then work fine.

    (there are other ways of indicating which members to serialize – the attributes are just the most convenient; for info, the same would work with [DataMember(Order=n)], since your type is also marked as a [DataContract])


    The following works fine for me:

    [Test]
    public void Main()
    {
        Node<int> tree = new Node<int>("abc", 1), clone;
        var children = tree.getChildren();
        children.Add(new Node<int>("abc/def", 2));
        children.Add(new Node<int>("abc/ghi", 3));
        Assert.AreEqual(2, tree.getChildren().Count);
    
        using(var ms = new MemoryStream())
        {
            Serializer.Serialize(ms, tree);
            Assert.Greater(1, 0); // I always get these args the wrong way around, 
            Assert.Greater(ms.Length, 0); // so I always double-check!
            ms.Position = 0;
            clone = Serializer.Deserialize<Node<int>>(ms);
        }
    
        Assert.AreEqual("abc", clone.Key);
        Assert.AreEqual(1, clone.Value);
        children = clone.getChildren();
        Assert.AreEqual(2, children.Count);
    
        Assert.IsFalse(children[0].HasChildren);
        Assert.AreEqual("abc/def", children[0].Key);
        Assert.AreEqual(2, children[0].Value);
    
        Assert.IsFalse(children[1].HasChildren);
        Assert.AreEqual("abc/ghi", children[1].Key);
        Assert.AreEqual(3, children[1].Value);
    }
    

    Edit following project example:

    Firstly, note that RootNode<T> should actually be just RootNode – a nested type already inherits the T from the containing type.

    There are two issues here:

    First, there’s the issue of RootNode – you were right that this relationship would need to be declared, but the C# compiler has no love for generics in attributes. Frankly, I would say encapsulate the root rather than inherit. If you must inherit, it is a pain, and you would have to declare it at runtime, i.e.

    RuntimeTypeModel.Default.Add(typeof(Node<MyDto>), true)
         .AddSubType(4, typeof(Node<MyDto>.RootNode));
    

    The second issue is nested lists/arrays; children would be a List<List<YourType>>. At the current time, that scenario is not supported, although I am a bit confused why it didn’t raise an exception – it is meant to throw a NotSupportedException citing:

    Nested or jagged lists and arrays are not supported

    I will investigate why it didn’t raise that!

    The problem here is that the protobuf spec (outside my control) has no way of representing such data unless there is a message in the middle, i.e.

    [ProtoContract]
    class SomeNewType {
        [ProtoMember(1)]
        public List<MyDto> Items {get {return items;}}
        private readonly List<MyDto> items = new List<MyDto>();
    }
    

    and use Node<SomeNewType> rather than Node<List<MyDto>>.

    In theory, protobuf-net could pretend that layer was in the middle, and just carry on anyway – but simply: I haven’t had time to design/write/test the code required to do that yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.