• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JaCraig / SerialBox / 11905976443

19 Nov 2024 03:59AM UTC coverage: 69.118%. First build
11905976443

push

github

JaCraig
feat: Updating to .Net 8/9 and workflow updates

- Migrating to .Net 8/9 due to issues with new MS packages.
- Updating workflows.
- Updated code to take advantage of .Net 8+ constructs.

BREAKING CHANGE: Removing .Net 6/7 support as .Net 9 dependencies say they work with it in the package but then throw warnings. I don't know why this approach was taken but it breaks a bunch of automation.

47 of 88 branches covered (53.41%)

Branch coverage included in aggregate %.

94 of 115 new or added lines in 8 files covered. (81.74%)

94 of 116 relevant lines covered (81.03%)

5.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.79
/SerialBox/SerialBox.cs
1
/*
2
Copyright 2016 James Craig
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
using SerialBox.Enums;
18
using SerialBox.Interfaces;
19
using System;
20
using System.Collections.Generic;
21
using System.Linq;
22
using System.Text;
23

24
namespace SerialBox
25
{
26
    /// <summary>
27
    /// SerialBox's main class
28
    /// </summary>
29
    public class SerialBox
30
    {
31
        /// <summary>
32
        /// Constructor
33
        /// </summary>
34
        /// <param name="serializers">The serializers.</param>
35
        public SerialBox(IEnumerable<ISerializer> serializers)
5✔
36
        {
37
            serializers ??= [];
5!
38
            Serializers = serializers.Where(x => !x.GetType().Namespace?.StartsWith("SERIALBOX", StringComparison.OrdinalIgnoreCase) ?? false)
15!
39
                                          .ToDictionary(x => x.ContentType);
5✔
40
            foreach (ISerializer? Serializer in serializers.Where(x => x.GetType()
40!
41
                                                                       .Namespace
15✔
42
                                                                       ?.StartsWith("SERIALBOX", StringComparison.OrdinalIgnoreCase) ?? false))
15✔
43
            {
44
                if (!Serializers.ContainsKey(Serializer.ContentType))
10✔
45
                    Serializers.Add(Serializer.ContentType, Serializer);
10✔
46
            }
47
        }
5✔
48

49
        /// <summary>
50
        /// Serializers
51
        /// </summary>
52
        protected IDictionary<string, ISerializer> Serializers { get; }
52✔
53

54
        /// <summary>
55
        /// Determines if the system can serialize/deserialize the content type
56
        /// </summary>
57
        /// <param name="contentType">Content type</param>
58
        /// <returns>True if it can, false otherwise</returns>
59
        public bool CanSerialize(string contentType) => !string.IsNullOrEmpty(contentType) && Serializers.ContainsKey(contentType.Split(';')[0]);
1!
60

61
        /// <summary>
62
        /// Deserializes the data to an object
63
        /// </summary>
64
        /// <typeparam name="T">Data type</typeparam>
65
        /// <typeparam name="TR">Return object type</typeparam>
66
        /// <param name="data">Data to deserialize</param>
67
        /// <param name="contentType">Content type (MIME type)</param>
68
        /// <returns>The deserialized object</returns>
69
        public TR? Deserialize<T, TR>(T data, SerializationType? contentType = null)
70
        {
NEW
71
            contentType ??= SerializationType.JSON;
×
NEW
72
            return (TR)Deserialize(data, typeof(TR), contentType)!;
×
73
        }
74

75
        /// <summary>
76
        /// Deserializes the data to an object
77
        /// </summary>
78
        /// <typeparam name="T">Type of the data</typeparam>
79
        /// <param name="data">Data to deserialize</param>
80
        /// <param name="objectType">Object type requested</param>
81
        /// <param name="contentType">Content type (MIME type)</param>
82
        /// <returns>The deserialized object</returns>
83
        public object? Deserialize<T>(T data, Type objectType, SerializationType? contentType = null)
84
        {
85
            contentType ??= SerializationType.JSON;
5✔
86
            if (string.IsNullOrEmpty(contentType.ToString()) || objectType == null)
5!
NEW
87
                return null;
×
88
            contentType = (SerializationType)contentType.ToString().Split(';')[0];
5✔
89
            return !Serializers.ContainsKey(contentType) || Serializers[contentType].ReturnType != typeof(T)
5!
90
                ? null
5✔
91
                : ((ISerializer<T>)Serializers[contentType]).Deserialize(objectType, data);
5✔
92
        }
93

94
        /// <summary>
95
        /// File type to content type
96
        /// </summary>
97
        /// <param name="fileType">File type</param>
98
        /// <returns>Content type</returns>
99
        public string FileTypeToContentType(string fileType)
100
        {
101
            return Serializers.FirstOrDefault(x => string.Equals(x.Value.FileType, fileType, StringComparison.OrdinalIgnoreCase))
2!
102
                              .Value
1✔
103
                              ?.ContentType ?? "";
1✔
104
        }
105

106
        /// <summary>
107
        /// Serializes the object based on the content type specified
108
        /// </summary>
109
        /// <typeparam name="T">Object type</typeparam>
110
        /// <param name="data">Object to serialize</param>
111
        /// <param name="contentType">Content type (MIME type)</param>
112
        /// <typeparam name="TR">Return type</typeparam>
113
        /// <returns>The serialized object as a string</returns>
114
        public TR? Serialize<T, TR>(T data, SerializationType? contentType = null)
115
        {
116
            if (data == null)
4!
NEW
117
                return default!;
×
118
            contentType ??= SerializationType.JSON;
4!
119
            return Serialize<TR>(data, typeof(T), contentType);
4✔
120
        }
121

122
        /// <summary>
123
        /// Serializes the object based on the content type specified
124
        /// </summary>
125
        /// <typeparam name="T">Return type</typeparam>
126
        /// <param name="data">Object to serialize</param>
127
        /// <param name="objectType">Object type</param>
128
        /// <param name="contentType">Content type (MIME type)</param>
129
        /// <returns>The serialized object as a string</returns>
130
        public T? Serialize<T>(object data, Type? objectType, SerializationType? contentType = null)
131
        {
132
            contentType ??= SerializationType.JSON;
5✔
133
            if (string.IsNullOrEmpty(contentType) || objectType == null)
5!
NEW
134
                return default!;
×
135
            contentType = (SerializationType)contentType.ToString().Split(';')[0];
5✔
136
            return !Serializers.ContainsKey(contentType) || Serializers[contentType].ReturnType != typeof(T)
5!
137
                ? default
5✔
138
                : ((ISerializer<T>)Serializers[contentType]).Serialize(objectType, data);
5✔
139
        }
140

141
        /// <summary>
142
        /// Outputs information about the serializers the system is using
143
        /// </summary>
144
        /// <returns>String version of the object</returns>
145
        public override string ToString()
146
        {
NEW
147
            var Builder = new StringBuilder();
×
NEW
148
            _ = Builder.Append("Serializers: ");
×
149
            var Separator = "";
×
NEW
150
            foreach (var Key in Serializers.Keys.Order())
×
151
            {
NEW
152
                _ = Builder.AppendFormat("{0}{1}", Separator, Serializers[Key].Name);
×
NEW
153
                Separator = ",";
×
154
            }
NEW
155
            return Builder.ToString();
×
156
        }
157
    }
158
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc