@@ -235,13 +235,6 @@ func marshalRESTTask(task *a2a.Task) ([]byte, error) {
235235 return json .Marshal (taskToWire (task ))
236236}
237237
238- func marshalRESTMessage (msg * a2a.Message ) ([]byte , error ) {
239- if msg == nil {
240- return json .Marshal (map [string ]any {})
241- }
242- return json .Marshal (messageToWire (msg ))
243- }
244-
245238// marshalRESTSendMessageResult encodes either a Task or a Message as the
246239// on-message-send response body, wrapped in a SendMessageResponse envelope
247240// (either {"message": Message} or {"task": Task}).
@@ -604,8 +597,14 @@ func taskPushNotificationConfigToWire(pc *a2a.PushConfig) map[string]any {
604597
605598// ---- core decoders ------------------------------------------------------
606599
600+ // isJSONNull reports whether raw is empty or represents the JSON literal `null`
601+ // (with any surrounding whitespace).
602+ func isJSONNull (raw json.RawMessage ) bool {
603+ return len (raw ) == 0 || strings .TrimSpace (string (raw )) == "null"
604+ }
605+
607606func unmarshalMessage (raw json.RawMessage ) (* a2a.Message , error ) {
608- if len (raw ) == 0 || string ( raw ) == "null" {
607+ if isJSONNull (raw ) {
609608 return nil , nil
610609 }
611610 var wire struct {
@@ -656,7 +655,7 @@ func unmarshalPart(raw json.RawMessage) (*a2a.Part, error) {
656655 switch {
657656 case wire .Text != nil :
658657 part .Content = a2a .Text (* wire .Text )
659- case len (wire .File ) > 0 && string ( wire . File ) != "null" :
658+ case ! isJSONNull (wire .File ):
660659 var f struct {
661660 FileWithURI string `json:"fileWithUri"`
662661 FileWithBytes string `json:"fileWithBytes"`
@@ -683,7 +682,7 @@ func unmarshalPart(raw json.RawMessage) (*a2a.Part, error) {
683682 }
684683 part .Content = a2a .Raw (inner )
685684 }
686- case len (wire .Data ) > 0 && string ( wire . Data ) != "null" :
685+ case ! isJSONNull (wire .Data ):
687686 var d struct {
688687 Data any `json:"data"`
689688 }
@@ -692,13 +691,13 @@ func unmarshalPart(raw json.RawMessage) (*a2a.Part, error) {
692691 }
693692 part .Content = a2a.Data {Value : d .Data }
694693 default :
695- return nil , fmt .Errorf ("Part has no known oneof key" )
694+ return nil , fmt .Errorf ("part has no known oneof key" )
696695 }
697696 return part , nil
698697}
699698
700699func unmarshalTask (raw json.RawMessage ) (* a2a.Task , error ) {
701- if len (raw ) == 0 || string ( raw ) == "null" {
700+ if isJSONNull (raw ) {
702701 return nil , nil
703702 }
704703 var wire struct {
@@ -751,7 +750,7 @@ func unmarshalTaskStatus(raw json.RawMessage) (a2a.TaskStatus, error) {
751750 return a2a.TaskStatus {}, fmt .Errorf ("failed to decode TaskStatus: %w" , err )
752751 }
753752 status := a2a.TaskStatus {State : decodeTaskState (wire .State )}
754- if len (wire .Message ) > 0 && string ( wire . Message ) != "null" {
753+ if ! isJSONNull (wire .Message ) {
755754 m , err := unmarshalMessage (wire .Message )
756755 if err != nil {
757756 return a2a.TaskStatus {}, err
@@ -843,7 +842,7 @@ func unmarshalArtifactUpdate(raw json.RawMessage) (*a2a.TaskArtifactUpdateEvent,
843842 LastChunk : wire .LastChunk ,
844843 Metadata : wire .Metadata ,
845844 }
846- if len (wire .Artifact ) > 0 && string ( wire . Artifact ) != "null" {
845+ if ! isJSONNull (wire .Artifact ) {
847846 a , err := unmarshalArtifact (wire .Artifact )
848847 if err != nil {
849848 return nil , err
@@ -870,7 +869,7 @@ func unmarshalSendMessageConfig(raw json.RawMessage) (*a2a.SendMessageConfig, er
870869 if wire .Blocking != nil {
871870 cfg .ReturnImmediately = ! * wire .Blocking
872871 }
873- if len (wire .PushNotification ) > 0 && string ( wire . PushNotification ) != "null" {
872+ if ! isJSONNull (wire .PushNotification ) {
874873 pc , err := unmarshalPushNotificationConfig (wire .PushNotification )
875874 if err != nil {
876875 return nil , err
0 commit comments