-
-
Notifications
You must be signed in to change notification settings - Fork 154
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
Serialization of interface typed members #513
Comments
Hi. Thanks a lot for the contribution! You can serialize interfaces, but they should be explicitly set in the IEnumerable parameter. And to deserialize, you need to specify the result class type. Here is an example: interface IInterface {
int Id { get; set; }
}
class InterfaceImpl : IInterface {
public int Id { get; set; }
}
[Fact]
public async Task Interface_Serialize() {
// explicit IInterface[] type, but elements can be any subtype
var data = new IInterface[] {
new InterfaceImpl { Id = 1 },
new InterfaceImpl { Id = 2 },
};
using var ms = new MemoryStream();
await ParquetSerializer.SerializeAsync(data, ms);
ms.Position = 0;
// explicit subtype
IList<InterfaceImpl> data2 = await ParquetSerializer.DeserializeAllAsync<InterfaceImpl>(ms).ToArrayAsync();
Assert.Equivalent(data, data2);
} |
My pleasure. Thanks for your answer but my issue is not with the type of the root objects but one of their properties. To follow up with your model if
|
I see! Sorry for misunderstanding. I've made a fix for this, which allows to serialize interface properties, so this works: interface IInterface {
int Id { get; set; }
}
interface IRootInterface {
IInterface Child { get; set; }
}
class InterfaceImpl : IInterface {
public int Id { get; set; }
}
class RootInterfaceImpl : IRootInterface {
public IInterface Child { get; set; } = new InterfaceImpl();
}
var data = new IRootInterface[] {
new RootInterfaceImpl { Child = new InterfaceImpl { Id = 1 } },
new RootInterfaceImpl { Child = new InterfaceImpl { Id = 2 } },
};
using var ms = new MemoryStream();
await ParquetSerializer.SerializeAsync(data, ms); Deserialization won't work though, as we don't know the implementation type of class ConcreteRootInterfaceImpl {
public InterfaceImpl Child { get; set; } = new InterfaceImpl();
}
IList<ConcreteRootInterfaceImpl> data2 = await ParquetSerializer.DeserializeAllAsync<ConcreteRootInterfaceImpl>(ms).ToArrayAsync(); Please try |
Thanks for the fix, indeed serialization is now working with It would be great if we could configure or hook into the deserialization process to tell the serializer which implementation to use when we know it. |
Agreed, I'll sort this out soon. |
Ideally the choice of the implementation to use would be dynamic by calling into a callback that could decide based on the other properties of the Parquet record. |
Issue description
Hello,
Is there a way to serialize types whose any member is interface typed?
I get a
NotImplementedException
inMakeField
.I think I understand the logical issue: in a set of objects we could have different implementations hence Parquet columns/schemas.
But in the case there is a single implementation is there a way to force the serialization?
Thanks in advance,
Mickael
The text was updated successfully, but these errors were encountered: