safetypes icon indicating copy to clipboard operation
safetypes copied to clipboard

Unmarshalling: IsSome() == true for Option if JSON property is present with null value

Open adbenitez opened this issue 1 year ago • 0 comments

example:

package main

import (
	"fmt"
	safe "github.com/eminarican/safetypes"
	"encoding/json"
)

var jsonBlob = []byte(`[
        {"Name": "Platypus"},
        {"Name": null}
    ]`)

type Animal struct {
	Name  safe.Option[string]
}

func main() {
	var animals []Animal
	json.Unmarshal(jsonBlob, &animals)

	for _, animal := range animals {
		if animal.Name.IsSome() {
			name := animal.Name.Unwrap()
			fmt.Printf("value=%q type=%T\n", name, name)
		} else {
			println("poor option :(")
		}
	}
}

even for Name: null Name.IsSome() is true, it should be false

adbenitez avatar May 04 '23 11:05 adbenitez