0

Given this program:

using System;
using System.Collections.Generic;
using System.Text.Json;
                    
public class Program
{
    public static void Main()
    {
        var payload = new Payload();
        payload.Id = "1001";
        payload.PassthroughParameters.Add("Foo", "Bar");
        var jsonString = JsonSerializer.Serialize(payload, new JsonSerializerOptions {
            WriteIndented = true
        });
        Console.WriteLine(jsonString);
        // OUTPUTS:
        // {
        //   "Id": "1001",
        //   "PassthroughParameters": {
        //     "Foo": "Bar"
        //   }
        // }
    }
    
    public class Payload
    {
        public string Id { get; set; }
        public IDictionary<string, string> PassthroughParameters { get; set; } = new Dictionary<string, string>();
    }
}

What's the most abstract C# data structure I can replace IDictionary<string, string> with that serializes to the same JSON?

I want to make it harder to reference the contents of the data structure in the C# code (e.g. payload.PassthroughParameters["Foo"]) to avoid coupling for code maintenance (not a security concerns) while still being able to pass the entire thing to an internal process (e.g. Process(otherParams, payload.PassthroughParameters)).

gabe
  • 1,873
  • 2
  • 20
  • 36
  • So you want a dictionary that prevents reading, unless you're serializing? – gunr2171 Apr 19 '21 at 18:28
  • @gunr2171 yea, just to avoid coupling in the C# code. i know that someone could use reflection but our team won't be doing that. – gabe Apr 19 '21 at 18:31
  • @brianberns I don't think it will serialize if the property is private. Also, I don't want developers to be able to add parameters. The implementation is a server deserializes a `Payload` instance from a JSON request and doesn't allow developers to interact with the `Payload#PassthroughParameters`. – gabe Apr 19 '21 at 18:41
  • 1
    I'm confused about what you do and do not want users to access PassthroughParameters. Should they even know the property is there? Should they be able to view the keys, or the values? Maybe something like [this](https://stackoverflow.com/questions/4066947/private-setters-in-json-net)? – gunr2171 Apr 19 '21 at 18:45
  • @gunr2171 good point about how it will be used and not just _not_ be used. i'll add that clarification above. – gabe Apr 19 '21 at 19:08
  • You want to make your code harder to use? Big red flag. Also, I see no coupling issue in your example. If you want you to make your code less usable, try using an `ExpandoObject` as an `object` or `dynamic` type in place of the dictionary. It should serialize similarly. – maxbeaudoin Apr 19 '21 at 19:24
  • @maxbeaudoin I want to make it harder to _couple_. I agree it's not perfect but the rest of the abstract is so close. This is the one compromise I felt comfortable making. – gabe Apr 19 '21 at 19:40
  • The dictionary introduces no coupling, on the contrary. Do you mean encapsulation? In which case you can define a private `Dictionary` and serialize it with the `[JsonProperty]` attribute. – maxbeaudoin Apr 19 '21 at 20:11
  • allowing a developer to do something like `payload.PassthroughParameters["Foo"]` introduces implicit coupling. The dictionary has a lack of encapsulation. Even if the question is the wrong _question_ in some cases, we are digressing a bit. The question still remains. i can replace the `decoupling` SO tag with an `encapsulation` if you think that's more accurate. – gabe Apr 19 '21 at 21:34

0 Answers0