encoding/json: error handling for custom (un)marshallers

This commit is contained in:
Hisham Aburaqibah
2025-11-06 09:02:05 +02:00
committed by IllusionMan1212
parent 4b9e15786d
commit 72d65603eb
2 changed files with 4 additions and 4 deletions

View File

@@ -62,7 +62,7 @@ Marshal_Options :: struct {
mjson_skipped_first_braces_end: bool,
}
User_Marshaller :: #type proc(w: io.Writer, v: any)
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) {
@@ -97,7 +97,7 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options, user_mars
if user_marshallers != nil {
marshaller := user_marshallers[v.id]
if marshaller != nil {
marshaller(w, v)
marshaller(w, v) or_return
return
}
}

View File

@@ -26,7 +26,7 @@ Unmarshal_Error :: union {
Unsupported_Type_Error,
}
User_Unmarshaller :: #type proc(p: ^Parser, v: any)
User_Unmarshaller :: #type proc(p: ^Parser, v: any) -> Unmarshal_Error
User_Unmarshaller_Map :: map[typeid]User_Unmarshaller
unmarshal_any :: proc(data: []byte, v: any, spec := DEFAULT_SPECIFICATION, user_unmarshallers: User_Unmarshaller_Map, allocator := context.allocator) -> Unmarshal_Error {
@@ -277,7 +277,7 @@ unmarshal_value :: proc(p: ^Parser, v: any, user_unmarshallers: User_Unmarshalle
if user_unmarshallers != nil {
unmarshaller := user_unmarshallers[v.id]
if unmarshaller != nil {
unmarshaller(p, v)
unmarshaller(p, v) or_return
return
}
}