Skip to content

Proposal: ability to marshal with flags #120

Open
@Jerska

Description

@Jerska

Hi! Me again with another proposal!

json.Parse offers a readily available alternative to json.Unmarshal with the simple addition of flags.

In comparison, json.Append takes two extra parameters compared to json.Marshal: a []byte to fill and the flags.
This means that unfortunately, you can't benefit from the encoderBufferPool if you just want to add some AppendFlags.

This would give 3 functions from most to least flexible :

  • Append(b []byte, x interface {}, flags AppendFlags) - can use tags, no extra copy, but no buffer pool
  • MarshalWithFlags(x interface{}, flags AppendFlags) - can use tags, buffer pool & one extra copy
  • Marshal(x interface{}) - cannot use tags, buffer pool & one extra copy

Having to pre-allocate a buffer for Append might be quite a challenge, because you often don't know how long the resulting buffer will be.
For instance, allocating a full page like the buffer pool does is not necessarily a great option for small payloads.
Reimplementing a similar pool in our code is definitely an option, but i'd be great to simply be able to reuse the logic that's already there in the lib.

Would this be something you'd consider adding in? Again, happy to propose a PR.

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions