Hi Juliusz,

I don't know if JSON serialization is deterministic, but I know a couple of
cases when it is not.

If the type or some type inside it has a custom JSON marshaller (method
MarshalJSON), then if that function's output is not deterministic, the
whole JSON for the type is not deterministic. Custom marshaller can do
whatever it wants, e.g. traverse maps without ordering keys, resulting in
non-determinism.

Another common pitfall with JSON: nil vs empty slice. E.g. []string{} is
encoded as "[]", while []string(nil) is encoded as "null", while they both
mean an empty slice in Go (though NOT considered deep equal according to
reflect.DeepEqual). The workaround is to add `omitempty` to the JSON tag to
remove such fields from the encoding. Otherwise you need to make sure that
empty slices are always nil or always non-nil in the code producing
structures that are serialized.

Boris

On Tue, Apr 9, 2024 at 9:27 AM Juliusz Chroboczek <j...@irif.fr> wrote:

> Hi,
>
> Suppose that I call json.Marshal on two structures that are deep equal,
> or on the same structure at different times.  Are the outputs guaranteed
> to be bytewise identical?
>
> (The reason I'm asking is that I'm sending JSON over HTTP, and I need to
> know whether it is correct to send a strong ETag with my reply even
> though I'm generating new JSON every time.)
>
> Thanks,
>
> -- Juliusz
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/87h6gag1qk.fsf%40pirx.irif.fr
> .
>


-- 
Best regards,
Boris Nagaev

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAFC_Vt57F29ruTzYq4hMZ61yH2rd-6Mo3H5_KMy5cNTXxNF_Rw%40mail.gmail.com.

Reply via email to