|
9 | 9 | #nullable enable
|
10 | 10 |
|
11 | 11 | using Hl7.Fhir.Introspection;
|
| 12 | +using System; |
12 | 13 |
|
13 | 14 | namespace Hl7.Fhir.Serialization
|
14 | 15 | {
|
@@ -41,44 +42,94 @@ public abstract class SerializationFilter
|
41 | 42 | /// <summary>
|
42 | 43 | /// Construct a new filter that conforms to the `_summary=true` summarized form.
|
43 | 44 | /// </summary>
|
44 |
| - public static SerializationFilter ForSummary() => new BundleFilter(new ElementMetadataFilter() { IncludeInSummary = true }); |
| 45 | + [Obsolete("Use CreateSummaryFactory() instead to ensure thread-safety when reusing JsonSerializerOptions instances. This method will be removed in a future version.")] |
| 46 | + public static SerializationFilter ForSummary() => CreateSummaryFactory()(); |
45 | 47 |
|
46 | 48 | /// <summary>
|
47 | 49 | /// Construct a new filter that conforms to the `_summary=text` summarized form.
|
48 | 50 | /// </summary>
|
49 |
| - public static SerializationFilter ForText() => new BundleFilter(new TopLevelFilter( |
50 |
| - new ElementMetadataFilter() |
51 |
| - { |
52 |
| - IncludeNames = new[] { "text", "id", "meta" }, |
53 |
| - IncludeMandatory = true |
54 |
| - })); |
55 |
| - |
56 |
| - public static SerializationFilter ForCount() => new BundleFilter(new TopLevelFilter( |
57 |
| - new ElementMetadataFilter() |
58 |
| - { |
59 |
| - IncludeMandatory = true, |
60 |
| - IncludeNames = new[] { "id", "total", "link" } |
61 |
| - })); |
| 51 | + [Obsolete("Use CreateTextFactory() instead to ensure thread-safety when reusing JsonSerializerOptions instances. This method will be removed in a future version.")] |
| 52 | + public static SerializationFilter ForText() => CreateTextFactory()(); |
| 53 | + |
| 54 | + [Obsolete("Use CreateCountFactory() instead to ensure thread-safety when reusing JsonSerializerOptions instances. This method will be removed in a future version.")] |
| 55 | + public static SerializationFilter ForCount() => CreateCountFactory()(); |
62 | 56 |
|
63 | 57 | /// <summary>
|
64 | 58 | /// Construct a new filter that conforms to the `_summary=data` summarized form.
|
65 | 59 | /// </summary>
|
66 |
| - public static SerializationFilter ForData() => new BundleFilter(new TopLevelFilter( |
67 |
| - new ElementMetadataFilter() |
68 |
| - { |
69 |
| - IncludeNames = new[] { "text" }, |
70 |
| - Invert = true |
71 |
| - })); |
| 60 | + [Obsolete("Use CreateDataFactory() instead to ensure thread-safety when reusing JsonSerializerOptions instances. This method will be removed in a future version.")] |
| 61 | + public static SerializationFilter ForData() => CreateDataFactory()(); |
72 | 62 |
|
73 | 63 | /// <summary>
|
74 | 64 | /// Construct a new filter that conforms to the `_elements=...` summarized form.
|
75 | 65 | /// </summary>
|
76 |
| - public static SerializationFilter ForElements(string[] elements) => new BundleFilter(new TopLevelFilter( |
77 |
| - new ElementMetadataFilter() |
78 |
| - { |
79 |
| - IncludeNames = elements, |
80 |
| - IncludeMandatory = true |
81 |
| - })); |
| 66 | + [Obsolete("Use CreateElementsFactory() instead to ensure thread-safety when reusing JsonSerializerOptions instances. This method will be removed in a future version.")] |
| 67 | + public static SerializationFilter ForElements(string[] elements) => CreateElementsFactory(elements)(); |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Create a factory function that produces new filter instances conforming to the `_summary=true` summarized form. |
| 71 | + /// Using this factory ensures thread-safety when reusing JsonSerializerOptions instances. |
| 72 | + /// </summary> |
| 73 | + public static Func<SerializationFilter> CreateSummaryFactory() |
| 74 | + { |
| 75 | + return () => new BundleFilter(new ElementMetadataFilter() { IncludeInSummary = true }); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Create a factory function that produces new filter instances conforming to the `_summary=text` summarized form. |
| 80 | + /// Using this factory ensures thread-safety when reusing JsonSerializerOptions instances. |
| 81 | + /// </summary> |
| 82 | + public static Func<SerializationFilter> CreateTextFactory() |
| 83 | + { |
| 84 | + return () => new BundleFilter(new TopLevelFilter( |
| 85 | + new ElementMetadataFilter() |
| 86 | + { |
| 87 | + IncludeNames = new[] { "text", "id", "meta" }, |
| 88 | + IncludeMandatory = true |
| 89 | + })); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Create a factory function that produces new filter instances conforming to the `_summary=count` summarized form. |
| 94 | + /// Using this factory ensures thread-safety when reusing JsonSerializerOptions instances. |
| 95 | + /// </summary> |
| 96 | + public static Func<SerializationFilter> CreateCountFactory() |
| 97 | + { |
| 98 | + return () => new BundleFilter(new TopLevelFilter( |
| 99 | + new ElementMetadataFilter() |
| 100 | + { |
| 101 | + IncludeMandatory = true, |
| 102 | + IncludeNames = new[] { "id", "total", "link" } |
| 103 | + })); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Create a factory function that produces new filter instances conforming to the `_summary=data` summarized form. |
| 108 | + /// Using this factory ensures thread-safety when reusing JsonSerializerOptions instances. |
| 109 | + /// </summary> |
| 110 | + public static Func<SerializationFilter> CreateDataFactory() |
| 111 | + { |
| 112 | + return () => new BundleFilter(new TopLevelFilter( |
| 113 | + new ElementMetadataFilter() |
| 114 | + { |
| 115 | + IncludeNames = new[] { "text" }, |
| 116 | + Invert = true |
| 117 | + })); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Create a factory function that produces new filter instances conforming to the `_elements=...` summarized form. |
| 122 | + /// Using this factory ensures thread-safety when reusing JsonSerializerOptions instances. |
| 123 | + /// </summary> |
| 124 | + public static Func<SerializationFilter> CreateElementsFactory(string[] elements) |
| 125 | + { |
| 126 | + return () => new BundleFilter(new TopLevelFilter( |
| 127 | + new ElementMetadataFilter() |
| 128 | + { |
| 129 | + IncludeNames = elements, |
| 130 | + IncludeMandatory = true |
| 131 | + })); |
| 132 | + } |
82 | 133 | }
|
83 | 134 | }
|
84 | 135 |
|
|
0 commit comments