Skip to content

Commit 8424c9a

Browse files
committed
Add class model and add their converter, improve Color json converter and subject model
1 parent 204303b commit 8424c9a

4 files changed

Lines changed: 139 additions & 7 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Runtime.Serialization.Formatters;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using WebUntisAPI.Client.Models;
11+
12+
namespace WebUntisAPI.Client.Converter
13+
{
14+
/// <summary>
15+
/// Convert the teachers in webuntis get classes response to int array
16+
/// </summary>
17+
public sealed class ClassJsonConverter : JsonConverter<Class>
18+
{
19+
///// <inheritdoc/>
20+
//public override int[] ReadJson(JsonReader reader, Type objectType, int[] existingValue, bool hasExistingValue, JsonSerializer serializer)
21+
//{
22+
// // Setup
23+
// JObject classObject = JObject.Load(reader);
24+
// List<int> teachers = new List<int>();
25+
26+
// // Read values
27+
// int index = 1;
28+
// while (classObject["teacher" + index++]?.ToObject<int>() is int teacher)
29+
// teachers.Add(teacher);
30+
31+
// return teachers.ToArray();
32+
//}
33+
34+
///// <inheritdoc/>
35+
//public override void WriteJson(JsonWriter writer, int[] value, JsonSerializer serializer)
36+
//{
37+
// JObject teacherObject = new JObject();
38+
39+
// for (int i = 0; i < value.Length; i++)
40+
// teacherObject.Add("teacher" + (i + 1), value[i]);
41+
42+
// teacherObject.WriteTo(writer);
43+
//}
44+
45+
/// <inheritdoc/>
46+
public override Class ReadJson(JsonReader reader, Type objectType, Class existingValue, bool hasExistingValue, JsonSerializer serializer)
47+
{
48+
JObject classObject = JObject.Load(reader);
49+
Class @class = new Class()
50+
{
51+
Id = classObject["id"].Value<int>(),
52+
Name = classObject["name"].Value<string>(),
53+
LongName = classObject["longName"].Value<string>(),
54+
Active = classObject["active"].Value<bool>()
55+
};
56+
57+
int index = 0;
58+
List<int> teachers = new List<int>();
59+
while (classObject["teacher" + ++index] is JToken teacher)
60+
teachers.Add(teacher.ToObject<int>());
61+
62+
@class.Teachers = teachers.ToArray();
63+
return @class;
64+
}
65+
66+
/// <inheritdoc/>
67+
public override void WriteJson(JsonWriter writer, Class value, JsonSerializer serializer)
68+
{
69+
writer.WriteStartObject();
70+
writer.WritePropertyName("id"); // Write Class.Id
71+
writer.WriteValue(value.Id);
72+
writer.WritePropertyName("name"); // Write Class.Name
73+
writer.WriteValue(value.Name);
74+
writer.WritePropertyName("longName"); // Wite Class.LongName
75+
writer.WriteValue(value.LongName);
76+
writer.WritePropertyName("active"); // Write Class.Active
77+
writer.WriteValue(value.Active);
78+
79+
// Write teachers
80+
for (int i = 0; i < value.Teachers.Length; i++)
81+
{
82+
writer.WritePropertyName("teacher" + (i + 1));
83+
writer.WriteValue(value.Teachers[i]);
84+
}
85+
}
86+
}
87+
}

WebUntisAPI.Client/Converter/ColorJsonConverter.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,20 @@ namespace WebUntisAPI.Client.Converter
1414
/// <summary>
1515
/// A json converter to convert a <see cref="Color"/> to the Hex color format (RRGGBB)
1616
/// </summary>
17-
public sealed class ColorJsonConverter : JsonConverter
17+
public sealed class ColorJsonConverter : JsonConverter<Color>
1818
{
1919
/// <inheritdoc/>
20-
public override bool CanConvert(Type objectType) => objectType == typeof(Color) || objectType == typeof(string);
21-
22-
/// <inheritdoc/>
23-
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
20+
public override Color ReadJson(JsonReader reader, Type objectType, Color existingValue, bool hasExistingValue, JsonSerializer serializer)
2421
{
2522
// Get value
2623
JToken jToken = JToken.Load(reader);
2724
string value = jToken.Value<string>();
2825

2926
// Convert value
30-
return new Color().FromHexColorFormat(value, false) ?? (object)value;
27+
return new Color().FromHexColorFormat(value);
3128
}
3229

3330
/// <inheritdoc/>
34-
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => ((Color)value).ToHexColorFormat();
31+
public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer) => serializer.Serialize(writer, value.ToHexColorFormat());
3532
}
3633
}

WebUntisAPI.Client/Models/Class.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using WebUntisAPI.Client.Converter;
9+
10+
namespace WebUntisAPI.Client.Models
11+
{
12+
/// <summary>
13+
/// A class
14+
/// </summary>
15+
[DebuggerDisplay("Name = {LongName}")]
16+
[JsonConverter(typeof(ClassJsonConverter))]
17+
public struct Class
18+
{
19+
/// <summary>
20+
/// Id of the class
21+
/// </summary>
22+
[JsonProperty("id")]
23+
public int Id { get; set; }
24+
25+
/// <summary>
26+
/// Untis internal name of the class
27+
/// </summary>
28+
public string Name { get; set; }
29+
30+
/// <summary>
31+
/// Name of the class
32+
/// </summary>
33+
public string LongName { get; set; }
34+
35+
/// <summary>
36+
/// Is the class active
37+
/// </summary>
38+
public bool Active { get; set; }
39+
40+
/// <summary>
41+
/// An array of all teachers of the class
42+
/// </summary>
43+
public int[] Teachers { get; set; }
44+
}
45+
}

WebUntisAPI.Client/Models/Subject.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Text;
88
using System.Threading.Tasks;
9+
using WebUntisAPI.Client.Converter;
910

1011
namespace WebUntisAPI.Client.Models
1112
{
@@ -49,12 +50,14 @@ public struct Subject
4950
/// Fore color of the subject
5051
/// </summary>
5152
[JsonProperty("foreColor")]
53+
[JsonConverter(typeof(ColorJsonConverter))]
5254
public Color ForeColor { get; set; }
5355

5456
/// <summary>
5557
/// Back color of the subject
5658
/// </summary>
5759
[JsonProperty("backColor")]
60+
[JsonConverter(typeof(ColorJsonConverter))]
5861
public Color BackColor { get; set; }
5962
}
6063
}

0 commit comments

Comments
 (0)