[
https://issues.apache.org/jira/browse/THRIFT-1528?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13714104#comment-13714104
]
Josh Hansen commented on THRIFT-1528:
-------------------------------------
For my use case, serializing optional-with-default fields when they're unset
wastes a huge amount of disk space. The behavior I expected was that optional
fields would not be serialized unless they are explicitly set, and I was
surprised to discover that in Java at least this isn't always the case.
As Bryan Duxbury mentioned above, whether or not an optional-with-default-value
field is serialized in Java actually depends on the datatype of the field.
Consider this struct, for example:
struct A {
1: optional bool a = true,
2: optional byte b = 4,
3: optional i16 c = 12,
4: optional i32 d = 49303,
5: optional i64 e = 584393,
6: optional double f = 5.5,
7: optional string g = "some value"
}
When I instantiate a plain A object and don't set any fields on it and write it
using TJSONProtocol, I get
{"7":{"str":"some value"}}
This is a horribly counterintuitive behavior. Either all of those fields should
be serialized, or none of them should be.
My vote is that we regularize this behavior and then make it controllable via a
thrift compiler flag, e.g. thrift
--serialize-unset-optionals-with-default=[true|false]
> Inconsistency in optional fields between Java/C# and python
> -----------------------------------------------------------
>
> Key: THRIFT-1528
> URL: https://issues.apache.org/jira/browse/THRIFT-1528
> Project: Thrift
> Issue Type: Bug
> Components: Python - Compiler
> Affects Versions: 0.8
> Reporter: Stefan Gmeiner
> Priority: Minor
>
> If a struct contains optional fields with default values the generated python
> code serialize differently than Java or C# code.
> In Java or C# optional fields are only serialized if a field was set by the
> client. If not the field is omited during serialization. This is possible
> because C#/Java maintains for each field a 'isset'-boolean which records if a
> field was set or not.
> However the generated python code does not have such a 'isset'-structure. It
> writes every field which is not equal None. As the constructor initialize the
> optional fields with their default value, these fields are written whether
> they are set or not.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira