Articles in this section
Category / Section

How to serialize the appointment color using JSON in the .NET MAUI Scheduler (SfScheduler)?

2 mins read

The Syncfusion® .NET MAUI Scheduler (SfScheduler) supports serializing and deserializing appointment colors using JSON. This article demonstrates how to serialize and deserialize appointment colors with JSON.

C#:

Use a JsonConverter to convert a Brush object to and from JSON. This custom converter will handle color serialization.

public class JsonColorConverter : JsonConverter<Brush>
{
   public override Brush ReadJson(JsonReader reader, Type objectType, Brush existingValue, bool hasExistingValue, JsonSerializer serializer)
   {
       string s = (string)reader.Value;
       return new SolidColorBrush(Color.FromArgb(s));
   }

   public override void WriteJson(JsonWriter writer, Brush value, JsonSerializer serializer)
   {
       writer.WriteValue(((SolidColorBrush)value).Color.ToHex());
   }
}

C#:

Use the JsonColorConverter to serialize and deserialize appointment colors.

private void IntializeAppoitments()
{
   this.Events = new ObservableCollection<Event>();

   Event clientMeeting = new Event();
   DateTime currentDate = DateTime.Now.AddDays(1);
   DateTime startTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 10, 0, 0);
   DateTime endTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, 12, 0, 0);
   clientMeeting.From = startTime;
   clientMeeting.To = endTime;
   clientMeeting.Background = new SolidColorBrush(Colors.Red);
   clientMeeting.EventName = "ClientMeeting";


   var json = JsonConvert.SerializeObject(clientMeeting, new JsonSerializerSettings
   {
       Converters = new JsonConverter[] { new JsonColorConverter() }
   });

   Event test = JsonConvert.DeserializeObject<Event>(json, new JsonSerializerSettings
   {
       Converters = new JsonConverter[] { new JsonColorConverter() }
   });

   this.Events.Add(test);
}

Output:

Serialization.png

Download the complete sample on GitHub

Conclusion:
I hope you enjoyed learning how to serialize the appointment colors using JSON in the .NET MAUI Scheduler.

You can refer to our .NET MAUI Scheduler feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. Explore our .NET MAUI Scheduler example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our other controls.

Please let us know in the following comments section if you have any queries or require clarifications. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied