0

I need ignore some properties only for first level during serialization to JSON by means of Newtonsoft, but for example the same properties of inner object must be serialized as usual.

class A
{
  string P1;
}

class B: A
{
  B B1;
}

var b = new B
{
  P1 = "p",  // must be ignored
  B1 = new B // 2nd level
  {
    P1 = "p2" // must be serialized
  }
}

var serializer = new JsonSerializer();
using (var textWriter = new JsonTextWriter(new StreamWriter(fullFileName)))
  serializer.Serialize(textWriter, value);

I assume that JsonTextWriter could be overridden or custom converter added. But it looks weird a little. Maybe there is another way?

dbc
  • 104,963
  • 20
  • 228
  • 340
Andrej B.
  • 187
  • 1
  • 5
  • 14
  • Seems very similar to [Json.NET serialize by depth and attribute](http://stackoverflow.com/q/36159424/3744182), though there they want to omit properties at deeper levels while you want to omit properties at shallower levels. Does that answer your question or do you need something more specific? – dbc Dec 12 '16 at 16:27
  • Yes, thank you, it's possible way, but I don't like that static stuff in using. – Andrej B. Dec 13 '16 at 14:49
  • Is your problem with using a static, or with needing a tracker at all? – dbc Dec 13 '16 at 22:31
  • With static construction of the tracker. So, I selected JsonTextWriter solution. – Andrej B. Dec 15 '16 at 08:28

0 Answers0