Skip to content
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

Unions serialization. #61

Closed
balabuev opened this issue Nov 16, 2017 · 7 comments
Closed

Unions serialization. #61

balabuev opened this issue Nov 16, 2017 · 7 comments

Comments

@balabuev
Copy link

Please clarify somewhere how Union of Union is serialized. Also, how Union of Struct with subtypes is serialized.
As I undersnand, there should be several ".tag" properties in the same JSON object, which is not so easy to deduct.

@balabuev
Copy link
Author

Its also not understood, why simplified Union serialization (as a string value in case of void tag) is not really used (even in deserializers) in generated SDK code.
And also, its quite confusing of how to use such simplified serialization, in case of a Union inside Union.

@braincore
Copy link
Contributor

The .tag properties become nested under the appropriate field names. For example, given the following spec:

union U
    var1 V

union V
    varA

Serializing U outputs:

{
  ".tag": "var1",
  "var1: {
    ".tag": "varA"
  }
}

The compact union serialization format isn't understood by our client SDKs in any language besides Python because Dropbox server always sends down the more general form. That's done because we see the compact form as beneficial for writing requests by hand in a terminal, rather than for programmatic ease.

Why do we support it in Python? Because Dropbox's API servers are written in Python and the deserializers needs to support the compact form for incoming client requests: https://github.com/dropbox/stone/blob/master/stone/backends/python_rsrc/stone_serializers.py#L642

Ideally, all of our deserializers would support the compact form, but I suspect that will only happen once we/someone requires the use of a target language other than Python on their server-side.

@balabuev
Copy link
Author

Are you sure? It seems to me that .Net SDK serializes your example as:

{
  ".tag": "var1",
  ".tag": "varA"
}

I didn't run the code, but I reviewed it. I'll check it more...

However, anyway, do unions has to inline struct fields or this is also only a compact format; for example, whether this:

union U
    var1 V

struct V
    x integer
    y integer

have to be serialized to this:

{
  ".tag": "var1",
  "x": 7,
  "y": 9
}

or general form is also valid:

{
  ".tag": "var1",
  "var1: {
    "x": 7,
    "y": 9
  }
}

@braincore
Copy link
Contributor

I'm certain that's our intended behavior. @qimingyuan can verify the .NET SDK.

Unions have to inline struct fields, there's no compact/general form distinction.

@balabuev
Copy link
Author

Ok, but what about structs with subtypes? To inline their fields inside a union still two ".tag" properties are required...

@braincore
Copy link
Contributor

Structs with subtypes cannot be inlined. Instead, the serialization looks just like the first nested union example I provided. Let me know if you want an example.

@braincore
Copy link
Contributor

By the way, thanks for the feedback. I've updated our documentation to be more explicit: https://github.com/dropbox/stone/blob/master/docs/json_serializer.rst#union

Feel free to re-open if you have further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants