Home > Net >  How to deserialize JSON Data with same storage adress for same objects in 2 different lists
How to deserialize JSON Data with same storage adress for same objects in 2 different lists

Time:07-11

My following problem is, that I have two classes.

One for Food which has following attributes :

private string name;
private double protein, calories, carbohydrates, fat;

And one for FoodWithGrams which has following attributes:

private Food food;
private string name;
private int gram;
private double proteinBasedOnGrams, caloriesBasedOnGrams, carbohydratesBasedOnGrams, 
fatBasedOnGrams;

The second class is used to fill the object created from the first class with gram values. This allows me to use the object from class "Food" multiple times. My program is already so far that the object which is created using the "Food" class and is used in "FoodWithGrams" share the same memory address. So I can edit the "Food" object and at the same time the "Food" object in "FoodWithGrams" also gets edited. The "FoodWithGrams" and "Food" objects are stored in two different lists in my controller object and serialized using Json in two different files. Now when I deserialize the json file, the "Food" object that is inside the "FoodWithGrams" object no longer shares the same memory address as the object created only by "Food". How can I make it so that they still share the same memory address after deserialization?`

FoodWithGrams JSON:

[
      {
        "Food": {
          "Name": "Milch",
          "Protein": 3.4,
          "Calories": 49.0,
          "Carbohydrates": 7.0,
          "Fat": 1.5
        },
        "Gram": 300,
        "ProteinBasedOnGrams": 10.2,
        "CaloriesBasedOnGrams": 147.0,
        "CarbohydratesBasedOnGrams": 21.0,
        "FatBasedOnGrams": 4.5,
        "Name": "Milch"
      },
      {
        "Food": {
          "Name": "Käse",
          "Protein": 7.0,
          "Calories": 60.0,
          "Carbohydrates": 3.0,
          "Fat": 4.2
        },
        "Gram": 200,
        "ProteinBasedOnGrams": 14.0,
        "CaloriesBasedOnGrams": 120.0,
        "CarbohydratesBasedOnGrams": 6.0,
        "FatBasedOnGrams": 8.4,
        "Name": "Käse"
      }
    ],

Food JSON:
[
  {
    "Name": "Milch",
    "Protein": 3.4,
    "Calories": 49.0,
    "Carbohydrates": 7.0,
    "Fat": 1.5
  },
  {
    "Name": "Käse",
    "Protein": 7.0,
    "Calories": 60.0,
    "Carbohydrates": 3.0,
    "Fat": 4.2
  }
]



CodePudding user response:

you can use this code for example

using Newtonsoft.Json;

List<FoodWithGramms> foodWithGramsList=JsonConvert.DeserializeObject<List<FoodWithGramms>>(FoodWithGramsJSON);

List<Food>  foodList =JsonConvert.DeserializeObject<List<Food>>(FoodJSON);

foreach(var item in foodWithGramsList)
{
    item.Food = foodList.Where(f=> f.Name==item.Name).FirstOrDefault();
}

CodePudding user response:

class FoodWithGrams
    {
        #region Attributes
        private Food food;
        private string name;
        private int gram;
        private double proteinBasedOnGrams, caloriesBasedOnGrams, carbohydratesBasedOnGrams, fatBasedOnGrams;
        #endregion

        #region Setter / Getter
        public Food Food { get => food; set { if (value == food) return; food = value; calculateFoodNutritionalValues(); } }
        public int Gram { get => gram; set => gram = value; }
        public double ProteinBasedOnGrams { get => proteinBasedOnGrams; set => proteinBasedOnGrams = value; }
        public double CaloriesBasedOnGrams { get => caloriesBasedOnGrams; set => caloriesBasedOnGrams = value; }
        public double CarbohydratesBasedOnGrams { get => carbohydratesBasedOnGrams; set => carbohydratesBasedOnGrams = value; }
        public double FatBasedOnGrams { get => fatBasedOnGrams; set => fatBasedOnGrams = value; }
        public string Name { get => name; set => name = value; }
        #endregion

        #region Constructors
        public FoodWithGrams()
        {
        }
        public FoodWithGrams(Food f, int g)
        {
            name = f.Name;
            gram = g;
            Food = f;
        }
        #endregion

        #region Methods
        public void calculateFoodNutritionalValues()
        {
            name = food.Name;
            proteinBasedOnGrams = food.Protein * gram / 100;
            caloriesBasedOnGrams = food.Calories * gram / 100;
            carbohydratesBasedOnGrams = food.Carbohydrates * gram / 100;
            FatBasedOnGrams = food.Fat * gram / 100;
        }
        #endregion
    }

public class Food
    {
        #region Attributes
        private string name;
        private double protein, calories, carbohydrates, fat;
        #endregion

        #region Setter / Getter
        public string Name { get => name; set => name = value; }
        public double Protein { get => protein; set => protein = value; }
        public double Calories { get => calories; set => calories = value; }
        public double Carbohydrates { get => carbohydrates; set => carbohydrates = value; }
        public double Fat { get => fat; set => fat = value; }
        #endregion
    }
  • Related