1

I converted my JSON to a class in C#, with VisualStudio's "Paste JSON as classes" tool, and this was the result:

public class Root
{
  public string orderId { get; set; }
  public int orderUpdateId { get; set; }
  public PositionXYT positionXYT { get; set; }

  public class PositionXYT
  {
    public int x { get; set; }
    public int y { get; set; }
    public int theta { get; set; }
  }
}

The PositionXYT class is being used as an attribute of Root, as you can see, however I can access the PositionXYT class from other classes outside of Root.

Is there a way to encapsulate PositionXYT in a way that only Root can use it, but the positionXYT attribute remains public?

fuba
  • 23
  • 5
  • 1
    `public` means you can access it outside of root. Why are you demanding the condition that attribute remain public? That is its defined purpose. – possum Oct 27 '22 at 13:14
  • 2
    If outside code should be able to use it (e.g. by accessing `root.PositionXYT`), then the class must be known to the outside. Otherwise, why have the (public) property? You could define and expose an interface though to hide the implementation. – knittl Oct 27 '22 at 13:19
  • I would like to use the positionXYT attribute as public in my Root class, so I can still serialize/deserialize it as a JSON object, and use the attribute through my program, but I don't want to allow the nested PositionXYT class to be used anywhere other than the Root class. – fuba Oct 27 '22 at 13:22
  • @knittl Please, could you exemplify how to use the interface in this case? – fuba Oct 27 '22 at 13:28
  • 1
    @fuba `public IPositionXYT positionXYT { get; set; } private class PositionXYT : IPositionXYT { …}`. I'm not sure however how or if this will affect serialization/deserialization from/to JSON. – knittl Oct 27 '22 at 13:36
  • "that only Root can use it, but the positionXYT attribute remains public?" - What are you asking, how to make `positionXYT` readonly to everything except `root` (which is possible)? Or to make `positionXYT` private, but require `public` scope (not possible)? – BurnsBA Oct 27 '22 at 14:11
  • @BurnsBA I am asking if there is a way to keep the PositionXYT nested class private for Root's use only, because I don't want anyone else to use it. And keep Root's positionXYT attribute public for use by its instances, because Root is a serialized/deserialized JSON object – fuba Oct 27 '22 at 14:16
  • No, you cannot declare something both `private` and `public`. But you can make something readonly to outside classes, or you can change how something is serialized, maybe that's what you are trying to do? – BurnsBA Oct 27 '22 at 14:30
  • @BurnsBA If it helps improve encapsulation, I could change the way it is being serialized, I honestly don't know many, would you have any suggestions for changing the way serialization is done and improving the encapsulation of the nested class in that way? – fuba Oct 27 '22 at 14:40
  • 1
    It depends how you serialize, xmlserializer, binary serializer, native json, newtonsoft json. See the answers on [this question](https://stackoverflow.com/a/32010248/1462295) for examples how to serialize private members using json serialization. – BurnsBA Oct 27 '22 at 14:43

0 Answers0