zig-protobuf icon indicating copy to clipboard operation
zig-protobuf copied to clipboard

In json parsing/writing, Map fields should be written/read as json objects

Open Arwalk opened this issue 1 year ago • 2 comments

They are currently encoded as a list of key:value entries, as it is their inner representation. Not directly tied to #52 but would benefit from it.

Tests written for this would still be useful even if #52 is done later.

This still requires identifying maps in the generator, which isn't as easy.

Arwalk avatar Aug 06 '24 13:08 Arwalk

Hey 👋🏼 just stumbled on this one while trying to manage K8s.io fields: the ObjectMeta API type has 2 maps (labels, annotations) and they both get generated as ArrayList.

Rather than turning the ArrayList into Maps when encoding to JSON, I would try to have the generated code has HashMap as fields. I guess one solution to this would be to use StringHashMap when keys are string (most common case) and instead generate HashContext sub-types for generated structs that also end up as maps keys. Are we able to backtrack the whole spectrum of types before writing the generated code? This would allow marking the messages that are used as map keys and add to them a blanket HashContext.

Asking cause I am not very familiar with the library design. Happy to work on this, since I have a need for it :)

inge4pres avatar Oct 29 '25 22:10 inge4pres

hey there @inge4pres, ideally i'd like to do #52 before this one, but that's not a necessary step.

Are we able to backtrack the whole spectrum of types before writing the generated code? This would allow marking the messages that are used as map keys and add to them a blanket HashContext.

I'm not sure I understand what you mean here, as i'm not that familiar with the hashmap types in the std.

The key part of this subject for me is that the generator must give us the information that a field is a map, ideally with its key type and value type. Once this is done, it should be possible to encode properly on the fly in the json

Arwalk avatar Oct 30 '25 09:10 Arwalk