SimpleREST.NET 0.1
A simple and minimal api framework for .net base on Express.js
Loading...
Searching...
No Matches
Extensions.cs
Go to the documentation of this file.
1using System.Net;
2using System.Text.Json;
3using Uri = UriTemplate.Core;
4
6
7public static class SimpleRestExtensions
8{
9 public static object? SafeDeserialize(this string json, object fallbackValue, JsonSerializerOptions options)
10 {
11 try
12 {
13 return JsonSerializer.Deserialize<object?>(json);
14 }
15 catch (JsonException)
16 {
17 return fallbackValue;
18 }
19 }
20
21 public static Uri.UriTemplate IgnoreTrailingSlash(this Uri.UriTemplate uriTemplate)
22 {
23 return new Uri.UriTemplate(uriTemplate.Template.TrimEnd('/'));
24 }
25
26 public static WebHeaderCollection ToWebHeaderCollection(
27 this Dictionary<string, string> dictionary
28 )
29 {
30 WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
31 foreach (KeyValuePair<string, string> kvp in dictionary)
32 {
33 webHeaderCollection.Add(kvp.Key, kvp.Value);
34 }
35 return webHeaderCollection;
36 }
37
38 public static void Merge<Tkey, TValue>(
39 this Dictionary<Tkey, TValue> dictionary,
40 Dictionary<Tkey, TValue> dictionaryToMerge
41 )
42 where Tkey : notnull
43 {
44 foreach (var kvp in dictionaryToMerge)
45 {
46 // If the key exists, update the value; otherwise, add the new key-value pair
47 dictionary[kvp.Key] = kvp.Value;
48 }
49 }
50
55 public static void NonDistructiveUnion<TKey, TValue>(
56 this Dictionary<TKey, TValue> target,
57 Dictionary<TKey, TValue> source
58 )
59 where TKey : notnull
60 {
61 foreach (var kvp in source)
62 {
63 // Only add the key-value pair if the key does not already exist in the target dictionary
64 if (!target.ContainsKey(kvp.Key))
65 {
66 target[kvp.Key] = kvp.Value;
67 }
68 }
69 }
70}
UriTemplate.Core Uri