-1

I have classes like this:

public abstract class EntityBase 
{
    
    public long Id { get; set; }

    public DateTimeOffset CreationTimeUtc { get; set; }
    public DateTimeOffset? ModificationTimeUtc { get; set; }

}

public class State : EntityBase 
{
    public string Name { get; set; }
    public IList<City> Cities { get; set; }
}

public class City : EntityBase 
{
    public string Name { get; set; }
    public State State { get; set; }
    public long StateId { get; set; }
}

so, I create a state variable

var state = new State()
        {
            Name = "State 1",
            CreationTimeUtc = DateTimeOffset.UtcNow,
            Id = Sequence.Generator.Next(),
            Cities = new List<City>()
            {
                new City()
                {
                    Id = Sequence.Generator.Next(),
                    CreationTimeUtc = DateTimeOffset.UtcNow,
                    Name = "City 1"
                }
            }
        };

When I serialize state variable with NewTonSoft, it returns a string like this:

{"name":"State 1","cities":[{"name":"City 1","stateId":"265439108547260417","id":"265439108547260419","creationTimeUtc":1638876015097,"modificationTimeUtc":1638876015097}],"id":"265439108547260417","creationTimeUtc":1638876015097,"modificationTimeUtc":1638876015097}

I wanna to create just root object, In this case I need to ignore Cities property. I can't use [JsonIgnore] attribute on Cities property. I want to use JsonSerializerSettings or somethings like that to generate json string.

Update

I use this code to generate json string

var jsonSerializerSettings = new JsonSerializerSettings() { 
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        };
Newtonsoft.Json.JsonConvert.SerializeObject(state, jsonSerializerSettings);

How can i do that!? thanks

Mofid.Moghimi
  • 783
  • 1
  • 5
  • 9
  • 2
    Why can't you use `JsonIgnore`? – Lasse V. Karlsen Dec 07 '21 at 11:31
  • @LasseV.Karlsen Because just in this case I want to ignore this property, not always – Mofid.Moghimi Dec 07 '21 at 11:32
  • I can not see CreationTimeUtc and Id in your State class. Your code can not be compiled. Pls post the real code that can be compiled at least. – Serge Dec 07 '21 at 11:35
  • 1
    check this - https://stackoverflow.com/questions/45010583/newtonsoft-json-way-of-ignoring-properties-without-adding-jsonignore – sanjay Dec 07 '21 at 11:38
  • You could use a custom contract resolver as shown in [Serialize only simple types using Json.Net](https://stackoverflow.com/q/15929848/3744182). Does that answer your question? If not could you [edit] your question to clarify what additional help you need? – dbc Dec 07 '21 at 16:14

1 Answers1

0

Json is just a format to move data between servers. It uselles alone. To use it you need to deserialize it. just use it

var cities=state.Cities ;
state.Cities=null;

var json= JsonConvert.SerializeObject(state,Formatting.Indented,
      new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

state.Cities=cities;

it was tested in Visual Studio. This is json

{
  "name": "State 1",
  "id": "265439108547260417",
  "creationTimeUtc": 1638876015097,
  "modificationTimeUtc": 1638876015097
}
Serge
  • 28,094
  • 4
  • 11
  • 35