0

I've a problem... I have a class which contains a list of customers and a list with orders and every order holds the customer as reference. If I serialize the object like this I get:

{
  "Customers": [
    {
      "$id": "1",
      "FirstName": "A",
      "LastName": "B"
    }
  ],
  "Orders": [
    {
      "Owner": {
        "$ref": "1"
      },
      "Desc": "Soup"
    }
  ]
}

That's fine.. but if I remove the customer I get:

{
  "Customers": [],
  "Orders": [
    {
      "Owner": {
        "$id": "1",
        "FirstName": "A",
        "LastName": "B"
      },
      "Desc": "Soup"
    }
  ]
}

But what I wish to have is:

{
  "Customers": [],
  "Orders": [
    {
      "Owner": null,
      "Desc": "Soup"
    }
  ]
}

The code I used:

[JsonObject(IsReference = true)]
public class Customer
{
    public String FirstName;
    public String LastName;
}

public class Order
{
    public Order(Customer T, String What)
    {
        Owner = T;
        Desc = What;
    }

    public Customer Owner;

    public String Desc;
}
public class Settings
{
    public List<Customer> Customers = new List<Customer>();

    public List<Order> Orders = new List<Order>();
}
public MainWindow()
{
    InitializeComponent();

    Settings Data = new Settings();

    Customer Customer = new Customer() { FirstName = "A", LastName = "B" };
    Data.Customers.Add(Customer);

    Order PlaceOrder = new Order(Data.Customers.First(), "Soup");
    Data.Orders.Add(PlaceOrder);

    Console.WriteLine(JsonConvert.SerializeObject(Data, Formatting.Indented));

    Data.Customers.Remove(Customer);

    Console.WriteLine(JsonConvert.SerializeObject(Data, Formatting.Indented));
    Close();
}

Can someone help me figure out how to do this?

dbc
  • 104,963
  • 20
  • 228
  • 340
QSpot
  • 31
  • 1
  • 2
  • Was `SecondaryReferenceOnlyJsonConverter ` from [this answer](https://stackoverflow.com/a/61685022/3744182) useful at all? Json.NET's requirement for the primary reference to always come first may mean that it wasn't, and I should delete the answer. – dbc May 17 '20 at 20:27

1 Answers1

0

Right now you are only removing the Customer from the list:

Data.Customers.Remove(Customer);

But the Customer still exists and is still referenced by the PlaceOrder. If you debug set a break point after the line above, you can see that Data.Orders[0].Owner still contains the Customer object.

If you don't want the order in the JSON string to point to a customer, then you should set that reference to null:

Data.Orders[0].Owner = null;
// or...
PlaceOrder.Owner = null;

That should result in your desired JSON output.

Xerillio
  • 4,855
  • 1
  • 17
  • 28
  • Thanks for your reply :) - You're right if I also set that prop to zero i get the result I would like to have. I thought there would be nicer solution using IReferenceResolver or something else to define a "root" object perhaps with an attribute and if its gone all other references will be destroyed. For example like: [JsonObject(OnlyIfRefExists = true)] public Customer Owner; – QSpot May 07 '20 at 18:02
  • @QSpot there's no way for the serializer to know that the list `Data.Customers` and the object `Data.Orders[0].Owner` has anything to do with each other unless you make your own implementation of e.g. the [JsonConverter](https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm). Doing this would however be very counter intuitive from a general programming perspective as serializing an actual object to "null" makes little sense. – Xerillio May 07 '20 at 18:20