• 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

71.05
/SerialBox/ExtensionMethods.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 Microsoft.Extensions.DependencyInjection;
18
using SerialBox.Enums;
19
using System;
20
using System.ComponentModel;
21
using System.Diagnostics.CodeAnalysis;
22

23
namespace SerialBox
24
{
25
    /// <summary>
26
    /// Extension methods dealing with serialization
27
    /// </summary>
28
    [EditorBrowsable(EditorBrowsableState.Never)]
29
    public static class ExtensionMethods
30
    {
31
        /// <summary>
32
        /// The service provider lock
33
        /// </summary>
34
        private static readonly object _ServiceProviderLock = new();
1✔
35

36
        /// <summary>
37
        /// The service provider
38
        /// </summary>
39
        private static IServiceProvider? _ServiceProvider;
40

41
        /// <summary>
42
        /// Deserializes the data based on the MIME content type specified (defaults to json)
43
        /// </summary>
44
        /// <typeparam name="TR">Return type expected</typeparam>
45
        /// <typeparam name="T">Object type</typeparam>
46
        /// <param name="data">Data to deserialize</param>
47
        /// <param name="contentType">Content type (MIME type)</param>
48
        /// <returns>The deserialized object</returns>
49
        public static TR? Deserialize<TR, T>(this T data, string contentType = "application/json") => data.Deserialize<TR?, T>((SerializationType)contentType);
2✔
50

51
        /// <summary>
52
        /// Deserializes the data based on the content type specified (defaults to json)
53
        /// </summary>
54
        /// <typeparam name="TR">Return type expected</typeparam>
55
        /// <typeparam name="T">Object type</typeparam>
56
        /// <param name="data">Data to deserialize</param>
57
        /// <param name="contentType">Content type</param>
58
        /// <returns>The deserialized object</returns>
59
        [return: MaybeNull]
60
        public static TR? Deserialize<TR, T>(this T data, SerializationType contentType)
61
        {
62
            contentType ??= SerializationType.JSON;
4!
63
            SerialBox? TempManager = GetServiceProvider()?.GetService<SerialBox>();
4!
64
            return (TR?)TempManager?.Deserialize(data, typeof(TR?), contentType)! ?? default;
4!
65
        }
66

67
        /// <summary>
68
        /// Serializes the data based on the MIME content type specified (defaults to json)
69
        /// </summary>
70
        /// <typeparam name="TR">Return type expected</typeparam>
71
        /// <typeparam name="T">Object type</typeparam>
72
        /// <param name="serializationObject">Object to serialize</param>
73
        /// <param name="contentType">Content type (MIME type)</param>
74
        /// <returns>The serialized object</returns>
75
        public static TR? Serialize<TR, T>(this T serializationObject, string contentType = "application/json") => serializationObject.Serialize<TR?, T>((SerializationType)contentType);
2✔
76

77
        /// <summary>
78
        /// Serializes the data based on the type specified (defaults to json)
79
        /// </summary>
80
        /// <typeparam name="TR">Return type expected</typeparam>
81
        /// <typeparam name="T">Object type</typeparam>
82
        /// <param name="serializationObject">Object to serialize</param>
83
        /// <param name="contentType">Content type</param>
84
        /// <returns>The serialized object</returns>
85
        public static TR? Serialize<TR, T>(this T serializationObject, SerializationType contentType)
86
        {
87
            contentType ??= SerializationType.JSON;
4!
88
            SerialBox? TempManager = GetServiceProvider()?.GetService<SerialBox>();
4!
89
            return TempManager == null ? default : TempManager.Serialize<T, TR?>(serializationObject, contentType);
4!
90
        }
91

92
        /// <summary>
93
        /// Gets the service provider.
94
        /// </summary>
95
        /// <returns></returns>
96
        private static IServiceProvider? GetServiceProvider()
97
        {
98
            if (_ServiceProvider is not null)
8✔
99
                return _ServiceProvider;
7✔
100
            lock (_ServiceProviderLock)
1✔
101
            {
102
                if (_ServiceProvider is not null)
1!
NEW
103
                    return _ServiceProvider;
×
104
                _ServiceProvider = new ServiceCollection().AddCanisterModules()?.BuildServiceProvider();
1!
105
            }
1✔
106
            return _ServiceProvider;
1✔
NEW
107
        }
×
108
    }
109
}
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