Skip to content

Conversation

@IllusionMan1212
Copy link
Contributor

Closes #4257

This allows passing an optional map of typeid to User_Marshaller/User_Unmarshaller to json.marshal() and json.unmarshal() respectively.

I will admit I primarily focused on making this work for my usecase and hadn't considered much else. Let me know if I missed something or if something needs changing.

User_Marshaller :: #type proc(w: io.Writer, v: any) -> Marshal_Error
User_Marshaller_Map :: map[typeid]User_Marshaller

marshal :: proc(v: any, opt: Marshal_Options = {}, user_marshallers: User_Marshaller_Map = nil, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: Marshal_Error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably better to place user_marshallers inside Marshal_Options.

}

marshal :: proc(v: any, opt: Marshal_Options = {}, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: Marshal_Error) {
User_Marshaller :: #type proc(w: io.Writer, v: any) -> Marshal_Error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that User_Marshaller needs ^Marshaller_Options too. If marshaller produces a json object, then there should be a way to respect all those options.
Also makes it possible to just call json.marshal_to_writer(w, transformed_data, opt)

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.

Json should allow a way to specify how to marshal/unmarshal an object (e.g. encoding/uuid)

2 participants