elixir-protobuf / protobuf

A pure Elixir implementation of Google Protobuf.
https://hexdocs.pm/protobuf/readme.html
MIT License
809 stars 141 forks source link

DateTime not encodable in JSON #373

Open skbolton opened 1 month ago

skbolton commented 1 month ago

Calling encode with a datetime properly casts to a Google.Timestamp

Protobuf.encode(%MyProto{created_at: DateTime.utc_now()})

But calling the JSON module to encode results in an error

Protobuf.JSON.encode(%MyProto{created_at: DateTime.utc_now()})
# function DateTime.__message_props__/0 is undefined or private

It seems that Protobuf.JSON.Encode.encodable doesn't have a clause to handle DateTimes. Is there a reason for this? Would a PR be accepted to handle DateTimes?

skbolton commented 1 month ago

I might be wrong on how the ergonomics of how the JSON module should work. It just feels interesting that the json module can only encode if working with a proto that might have been encoded and decoded using the top level encoder api and then ran through the json encoder.