You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by "Zoltan Csizmadia (Jira)" <ji...@apache.org> on 2024/09/09 18:03:00 UTC

[jira] [Commented] (AVRO-4054) Serialization of C# Dictionary only works if type is

    [ https://issues.apache.org/jira/browse/AVRO-4054?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17880409#comment-17880409 ] 

Zoltan Csizmadia commented on AVRO-4054:
----------------------------------------

Can you use `SpecificDefaultWriter` instead of `DefaultWriter`? It looks like `SpecificDefaultWriter` is internally using `IDictionary` interface and not `IDictionary<string, object>`. In that case you dont need to cast it to a new dictionary.

> Serialization of C# Dictionary only works if type is <string, object>
> ---------------------------------------------------------------------
>
>                 Key: AVRO-4054
>                 URL: https://issues.apache.org/jira/browse/AVRO-4054
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: csharp
>    Affects Versions: 1.12.0
>         Environment: I was testing this in a .Net Standard 2.0 project.
>            Reporter: Rob Komjathy
>            Priority: Major
>
> I encountered an unexpected behavior with Avro Maps (C# Dictionaries).  If I directly try to serialize a Dictionary<string, T> where T is any type besides literally "object", an exception is thrown. 
> If you dig into the code, it's because you can't directly cast Dictionary<string, T> to Dictionary<string, object>.  This fails on at least 3 lines in DefaultWriter, as there are several attempts made to verify the Dictionary "is" <string, object> or can be cast to it directly.
> For example, here is a rudimentary example.
>  
> {code:java}
> var schema = MapSchema.CreateMap(PrimitiveSchema.Create(Schema.Type.Long));
> var ms = new MemoryStream();
> Encoder enc = new BinaryEncoder(ms);
> var writer = new DefaultWriter(schema);
> writer.Write(new Dictionary<string, long>(), enc);
> enc.Flush(); {code}
> Given this example, I would expect to be able to pass a Dictionary<string, long> given the schema I have defined.  Instead this throws an exception. 
>  
> The work around is to create a new Dictionary<string, object> and individually cast the values to "objects".  For example:
>  
> {code:java}
> public static Dictionary<string, object> ToAvroSafeDictionary<TValue>(this IDictionary<string, TValue> dictionary)
> {
>     return dictionary?.ToDictionary(k => k.Key, v => (object)v.Value);
> } {code}
> I originally encountered this with my own custom schema but as you can see, using Apache Avro's own schema objects in the rudimentary example exhibit the same issue.
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)