edn icon indicating copy to clipboard operation
edn copied to clipboard

struct encoding and whitespace

Open benjiqq opened this issue 5 years ago • 1 comments

was decoding a struct and the resulting map does not have whitespace between the keyword and value and between the next value?

in this case I was expecting {:name \"Hans\" :born 1990}

func TestMapEncoding(t *testing.T) {

	type Person struct {
		Name      string `edn:"name"`
		Birthyear int    `edn:"born"`
	}
	user := Person{Name: "Hans", Birthyear: 1990}

	dat, _ := Marshal(user)
	if string(dat) != "{:name \"Hans\" :born 1990}" {
		t.Error("fails", string(dat)) 
	}
}

benjiqq avatar May 08 '20 06:05 benjiqq

@benjyz: I'm a bit late to this one, but go-edn's Marshal only writes whitespace when it needs to. " is only allowed at the start and end of strings (except when quoted inside strings), which means there's no reason to add a whitespace there.

Note that Marshal is not designed to create output read by humans. If you want human-readable output, you may want to look into edn.MarshallIndent or edn.MarshallPPrint, which both write out data in a more approachable manner for humans.

hypirion avatar Oct 15 '20 08:10 hypirion