Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose an implementation for #138 #174

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions JsonSubTypes/JsonSubtypesByDiscriminatorValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace JsonSubTypes
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

internal class JsonSubtypesByDiscriminatorValueConverter : JsonSubtypesConverter
public class JsonSubtypesByDiscriminatorValueConverter : JsonSubtypesConverter
{
[ThreadStatic] private static bool _isInsideWrite;
[ThreadStatic] private static bool _allowNextWrite;
Expand All @@ -37,13 +37,14 @@ internal class JsonSubtypesByDiscriminatorValueConverter : JsonSubtypesConverter
private readonly Dictionary<Type, object> _supportedTypes = new Dictionary<Type, object>();
private readonly NullableDictionary<object, Type> _subTypeMapping;

internal JsonSubtypesByDiscriminatorValueConverter(Type baseType, string discriminatorProperty,
// this constructor is part of the public api since it's protected and this class is public
protected internal JsonSubtypesByDiscriminatorValueConverter(Type baseType, string discriminatorProperty,
NullableDictionary<object, Type> subTypeMapping, bool serializeDiscriminatorProperty, bool addDiscriminatorFirst, Type fallbackType) : base(baseType, discriminatorProperty, fallbackType)
{
_serializeDiscriminatorProperty = serializeDiscriminatorProperty;
_subTypeMapping = subTypeMapping;
_addDiscriminatorFirst = addDiscriminatorFirst;
foreach (var type in _subTypeMapping.Entries())
foreach (KeyValuePair<object, Type> type in _subTypeMapping.Entries())
{
if (_supportedTypes.ContainsKey(type.Value))
{
Expand Down Expand Up @@ -106,11 +107,11 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
_isInsideWrite = false;
}

if (!_supportedTypes.TryGetValue(value.GetType(), out var supportedType))
if (!_supportedTypes.TryGetValue(value.GetType(), out object supportedType))
{
throw new JsonSerializationException("Impossible to serialize type: " + value.GetType().FullName + " because there is no registered mapping for the discriminator property");
}
var typeMappingPropertyValue = JToken.FromObject(supportedType, serializer);
JToken typeMappingPropertyValue = JToken.FromObject(supportedType, serializer);
if (_addDiscriminatorFirst)
{
jsonObj.AddFirst(new JProperty(JsonDiscriminatorPropertyName, typeMappingPropertyValue));
Expand Down
6 changes: 3 additions & 3 deletions JsonSubTypes/JsonSubtypesConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ namespace JsonSubTypes
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
internal class JsonSubtypesConverter : JsonSubtypes
public class JsonSubtypesConverter : JsonSubtypes
{
private readonly Type _baseType;
private readonly Type _fallbackType;

public JsonSubtypesConverter(Type baseType, Type fallbackType) : base()
internal JsonSubtypesConverter(Type baseType, Type fallbackType) : base()
{
_baseType = baseType;
_fallbackType = fallbackType;
}

public JsonSubtypesConverter(Type baseType, string jsonDiscriminatorPropertyName, Type fallbackType) : base(jsonDiscriminatorPropertyName)
internal JsonSubtypesConverter(Type baseType, string jsonDiscriminatorPropertyName, Type fallbackType) : base(jsonDiscriminatorPropertyName)
{
_baseType = baseType;
_fallbackType = fallbackType;
Expand Down