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

[#405] Convert object to/from JSON #411

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jabolina
Copy link
Member

The field IDs for Instant and Date were ignored during the conversion so the generated JSON was an empty string. The fix was a bit more convoluted. I'll explain all I have and maybe there's a better solution.

  • An Instant is converted by the WrappedMessage to byte[] by writing one int64 field for seconds and int32 for nanos.
  • The Proto to JSON conversion needs to read two fields now. The message-wrapping.proto file describes the seconds part as a oneOf which writes the key as "_value". We still need to write to fields, so we end with {"_value": 0, "wrappedInstantNanos": 0} without any typing.
  • We could use the type as a WrappedMessage, but the conversion from JSON to Instant would fail as it creates the Instant inside the WrappedMessage object. So we need an explicit type for the conversion to create the correct object.

Since we need this conversion from JSON to Instant to work, I've created an adapter in the types module. During the Proto to JSON conversion, we require this adapter to be registered so we can write the field names and the type correctly.

We are still left with converting JSON to Proto byte[]. We need to verify in the WrappedMessage class whether this object corresponds to a known type. Otherwise, the generated byte[] will be entirely different from the one produced by Instant to Proto byte[] serialization.

The Date object suffers from the same thing. The difference is that:

  • We serialize Date as an int64 from the Date#getTime method.
  • This would create a JSON as {"_type": "int64", "_value": 12345}. Which loses any type parameter that this value is a Date object.
  • The conversion from JSON to Date will fail as it returns a primitive long number instead of the Date object.

Closes #405.

* Instant and Date objects not handled during JSON conversion.
* Additionally, these objects try to convert as primitive types. This
  loses all the type context for the conversion back from JSON.
* Require adapters from the types module to perform the conversion to
  JSON.
@jabolina jabolina requested a review from a team as a code owner January 27, 2025 19:36
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

Successfully merging this pull request may close these issues.

Failed to convert serialized Instant to JSON
1 participant