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

  • Home
  • SEARCH
  • 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 8752097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:10:12+00:00 2026-06-13T13:10:12+00:00

I want to avoid reinventing the wheel when serializing data. I know some ways

  • 0

I want to avoid reinventing the wheel when serializing data. I know some ways to serialize objects which are linked to each other, but it ranges from writing some code to writing a lot of code for serialization, and I’d like to avoid that. There must be some generic solutions.

Let’s say I have a structure like this:

Person
    bro = new Person { name = "bro", pos = new Pos { x = 1, y = 5 } },
    sis = new Person { name = "sis", pos = new Pos { x = 2, y = 6 } },
    mom = new Person { name = "mom", pos = new Pos { x = 3, y = 7 }, 
        children = new List<Person> { bro, sis }
    },
    dad = new Person { name = "dad", pos = new Pos { x = 4, y = 8 }, 
        children = new List<Person> { bro, sis }, mate = mom
    };
mom.mate = dad;
Family family = new Family { persons = new List<Person> { mom, dad, bro, sis } };

I want to serialize data to something like this:

family: {
    persons: [
        { name: "bro", pos: { x: 1, y: 5 } },
        { name: "sis", pos: { x: 2, y: 6 } },
        { name: "mom", pos: { x: 3, y: 7 }, mate: "dad", children: [ "bro", "sis" ] },
        { name: "dad", pos: { x: 4, y: 8 }, mate: "mom", children: [ "bro", "sis" ] },
    ]
}

Here, links are serialized as just names, with the assumption that names are unique. Links can also be “family.persons.0” or generated unique IDs or whatever.

Requirements:

  1. Format must be human-readable and preferably human-writable too. So, in order of preference: JSON, YAML*, XML, custom. No binary formats.

  2. Serialization must support all good stuff .NET offers. Generics are a must, including types like IEnumerable<>, IDictionary<> etc. Dynamic types / untyped objects are desirable.

  3. Format must not be executable. No Lua, Python etc. scripts and things like that.

  4. If unique IDs are generated, they must be stable (persist through serialization-deserialization), as files will be put into a version control system.

* Heard about YAML, but sadly, it seems to be pretty much dead.

  • 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-13T13:10:14+00:00Added an answer on June 13, 2026 at 1:10 pm

    Solved the problem using JSON.NET (fantastic library!). Now objects are, first, serialized and referenced exactly where I want them them to; and second, without numerous “$id” and “$ref” fields. In my solution, the first property of an object is used as its identifier.

    I’ve created two JsonConvertors (for references to objects and for referenced objects):

    interface IJsonLinkable
    {
        string Id { get; }
    }
    
    class JsonRefConverter : JsonConverter
    {
        public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteValue(((IJsonLinkable)value).Id);
        }
    
        public override object ReadJson (JsonReader reader, Type type, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType != JsonToken.String)
                throw new Exception("Ref value must be a string.");
            return JsonLinkedContext.GetLinkedValue(serializer, type, reader.Value.ToString());
        }
    
        public override bool CanConvert (Type type)
        {
            return type.IsAssignableFrom(typeof(IJsonLinkable));
        }
    }
    
    class JsonRefedConverter : JsonConverter
    {
        public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value);
        }
    
        public override object ReadJson (JsonReader reader, Type type, object existingValue, JsonSerializer serializer)
        {
            var jo = JObject.Load(reader);
            var value = JsonLinkedContext.GetLinkedValue(serializer, type, (string)jo.PropertyValues().First());
            serializer.Populate(jo.CreateReader(), value);
            return value;
        }
    
        public override bool CanConvert (Type type)
        {
            return type.IsAssignableFrom(typeof(IJsonLinkable));
        }
    }
    

    and a context to hold references data (with a dictionary for each type, so IDs need to be unique only among objects of the same type):

    class JsonLinkedContext
    {
        private readonly IDictionary<Type, IDictionary<string, object>> links = new Dictionary<Type, IDictionary<string, object>>();
    
        public static object GetLinkedValue (JsonSerializer serializer, Type type, string reference)
        {
            var context = (JsonLinkedContext)serializer.Context.Context;
            IDictionary<string, object> links;
            if (!context.links.TryGetValue(type, out links))
                context.links[type] = links = new Dictionary<string, object>();
            object value;
            if (!links.TryGetValue(reference, out value))
                links[reference] = value = FormatterServices.GetUninitializedObject(type);
            return value;
        }
    }
    

    Some attributes on the properties are necessary:

    [JsonObject(MemberSerialization.OptIn)]
    class Family
    {
        [JsonProperty(ItemConverterType = typeof(JsonRefedConverter))]
        public List<Person> persons;
    }
    
    [JsonObject(MemberSerialization.OptIn)]
    class Person : IJsonLinkable
    {
        [JsonProperty]
        public string name;
        [JsonProperty]
        public Pos pos;
        [JsonProperty, JsonConverter(typeof(JsonRefConverter))]
        public Person mate;
        [JsonProperty(ItemConverterType = typeof(JsonRefConverter))]
        public List<Person> children;
    
        string IJsonLinkable.Id { get { return name; } }
    }
    
    [JsonObject(MemberSerialization.OptIn)]
    class Pos
    {
        [JsonProperty]
        public int x;
        [JsonProperty]
        public int y;
    }
    

    So, when I serialize and deserialize using this code:

    JsonConvert.SerializeObject(family, Formatting.Indented, new JsonSerializerSettings {
        NullValueHandling = NullValueHandling.Ignore,
        Context = new StreamingContext(StreamingContextStates.All, new JsonLinkedContext()),
    });
    
    JsonConvert.DeserializeObject<Family>(File.ReadAllText(@"..\..\Data\Family.json"), new JsonSerializerSettings {
        Context = new StreamingContext(StreamingContextStates.All, new JsonLinkedContext()),
    });
    

    I get this neat JSON:

    {
      "persons": [
        {
          "name": "mom",
          "pos": {
            "x": 3,
            "y": 7
          },
          "mate": "dad",
          "children": [
            "bro",
            "sis"
          ]
        },
        {
          "name": "dad",
          "pos": {
            "x": 4,
            "y": 8
          },
          "mate": "mom",
          "children": [
            "bro",
            "sis"
          ]
        },
        {
          "name": "bro",
          "pos": {
            "x": 1,
            "y": 5
          }
        },
        {
          "name": "sis",
          "pos": {
            "x": 2,
            "y": 6
          }
        }
      ]
    }
    

    What I don’t like in my solution, is that I have to use JObject, even though technically it’s unnecessary. It probably creates quite a bit of objects, so loading will be slower. But looks like this is the most widely used approach for customizing convertors of objects. Methods which could be used to avoid this are private anyway.

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

Sidebar

Related Questions

I want avoid iterating over a nil array. My bad solution: if nil!=myArr myArr.each
I totally want to avoid using inline javascript! But i have a table which
I want to avoid writing <table></table> and other stuff like that, by writing Java
I want to avoid having to override each label provider. Is there a simpler
I want to distribute a webstart application which uses RMI. I want to avoid
I'm trying to avoid reinventing the wheel here. I'm looking for a GEM that
I want to avoid compiling php with fileinfo, ereg, and parch. What is the
I want to avoid code duplication as much as possible. Suppose I have a
Because I want to avoid repetitive code, and I'm using a lot of text
As much as possible I want to avoid unnecessary duplication of code. In my

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.