Share via

Serialization

Rajarajacholan Krishnamurthy 31 Reputation points
2023-04-24T16:43:04.5766667+00:00

I am not sure why is both Serialized and Deserialized not called! Can anyone help? I am trying serialize JSON string it works fine except that these methods never called!


using System.Runtime.Serialization;


    [Serializable()]
    internal class ParkingFee 
    {
        float? intervalMin;
        float? intervalMax;
        int fee;
        string feeType;
        TypeVehicleType vehicleType;
        TypeParkinglotType parkinglotType;
        [NonSerialized]
        public int TotalParkingSpots;

        public ParkingFee()
        {
            vehicleType = new TypeVehicleType();
            parkinglotType = new TypeParkinglotType();
        }

        public float? IntervalMin
        {
            get { return intervalMin ; }
            set { intervalMin = value ; }
        }

        public float? IntervalMax
        {
            get { return intervalMax ; }
            set { intervalMax = value ; }
        }

        public int Fee
        {
            get { return fee ; }
            set { fee = value ; }
        }

        public string FeeType
        {
            get { return feeType ; }
            set { feeType = value ; }
        }

        public TypeVehicleType VehicleType
        {
            get { return vehicleType ; }
            set { vehicleType = value ; }
        }

        public TypeParkinglotType ParkinglotType
        {
            get { return parkinglotType ; }
            set { parkinglotType = value ; }
        }

	  [OnSerialized()]
 	  internal void OnSerializeMethod(StreamingContext context)
        {
            TotalParkingSpots = 100;
            System.Console.WriteLine("Setting default park spot");
        }

        [OnDeserialized()]
        internal void OnDeserializedMethod(StreamingContext context)
        {
            TotalParkingSpots = 100;
            System.Console.WriteLine("Setting default park spot");
        }
    }

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,086 Reputation points
    2023-04-24T19:00:15.88+00:00

    you are specify the .net runtime serialization attributes for the runtime binary formatter.

    you don't specify the json serializer you are using, so I'll assume System.Text.Json. in they case you use an interface rather than an attribute:

    https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.ijsonondeserialized.ondeserialized?view=net-7.0#system-text-json-serialization-ijsonondeserialized-ondeserialized.

    if you are using newtonsoft, it supports the attributes.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.