Skip to content

Commit

Permalink
Propose an implementation for #138
Browse files Browse the repository at this point in the history
  • Loading branch information
manuc66 committed Nov 27, 2023
1 parent c4e2ee2 commit 2433f00
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
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

0 comments on commit 2433f00

Please sign in to comment.