Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,18 @@ public Func<IEnumerable<Tuple<TKey, TValue>>, TFSharpMap> CreateFSharpMapConstru
}

private Attribute? GetFSharpCompilationMappingAttribute(Type type)
=> type.GetCustomAttribute(_compilationMappingAttributeType, inherit: true);
{
object[] attributes = type.GetCustomAttributes(_compilationMappingAttributeType, inherit: false);
return attributes.Length == 0 ? null : (Attribute)attributes[0];
}

private SourceConstructFlags GetSourceConstructFlags(Attribute compilationMappingAttribute)
=> _sourceConstructFlagsGetter is null ? SourceConstructFlags.None : (SourceConstructFlags)_sourceConstructFlagsGetter.Invoke(compilationMappingAttribute, null)!;

// If the provided type is generated by the F# compiler, returns the runtime FSharp.Core assembly.
private static Assembly? GetFSharpCoreAssembly(Type type)
{
foreach (Attribute attr in type.GetCustomAttributes(inherit: true))
foreach (Attribute attr in type.GetCustomAttributes(inherit: false))
{
Type attributeType = attr.GetType();
if (attributeType.FullName == CompilationMappingAttributeTypeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,22 @@ let ``Recursive struct records are supported``() =
Assert.Equal("""{"Next":[{"Next":[null]}]}""", json)
let deserializedValue = JsonSerializer.Deserialize<RecursiveStructRecord>(json)
Assert.Equal(value, deserializedValue)


[<JsonDerivedType(typeof<DerivedClass>, "derived")>]
type BaseClass(x : int) =
member _.X = x

and DerivedClass(x : int, y : bool) =
inherit BaseClass(x)
member _.Y = y

[<Fact>]
let ``Support F# class hierarchies`` () =
let value = DerivedClass(42, true) :> BaseClass
let json = JsonSerializer.Serialize(value)
Assert.Equal("""{"$type":"derived","Y":true,"X":42}""", json)
let deserializedValue = JsonSerializer.Deserialize<BaseClass>(json)
let derived = Assert.IsType<DerivedClass>(deserializedValue)
Assert.Equal(42, deserializedValue.X)
Assert.Equal(true, derived.Y)
Loading