json.Encoder adds trailing newlines. WTF?

1,485 views
Skip to first unread message

OpenNota

unread,
Apr 12, 2014, 12:07:22 AM4/12/14
to golan...@googlegroups.com
http://play.golang.org/p/skOwJv7qZR

Any reason for this? Can this behaviour be changed by configuring the encoder? It is kind of unexpected to me. The encoding/json documentation does not mention it also.

David Symonds

unread,
Apr 12, 2014, 12:21:49 AM4/12/14
to OpenNota, golang-nuts

What does it matter? It is still valid JSON, right?

Benjamin R. Haskell

unread,
Apr 12, 2014, 12:34:23 AM4/12/14
to golan...@googlegroups.com
It's not configurable, AFAICT.  From https://code.google.com/p/go/source/browse/src/pkg/encoding/json/stream.go#165

        // Terminate each value with a newline.
        // This makes the output look a little nicer
        // when debugging, and some kind of space
        // is required if the encoded value was a number,
        // so that the reader knows there aren't more
        // digits coming.
        e.WriteByte('\n')


On Sat, Apr 12, 2014 at 12:07 AM, OpenNota <open...@gmail.com> wrote:
http://play.golang.org/p/skOwJv7qZR

Any reason for this? Can this behaviour be changed by configuring the encoder? It is kind of unexpected to me. The encoding/json documentation does not mention it also.

--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

OpenNota

unread,
Apr 12, 2014, 12:43:48 AM4/12/14
to golan...@googlegroups.com, OpenNota
On Saturday, April 12, 2014 11:21:49 AM UTC+7, David Symonds wrote:

What does it matter? It is still valid JSON, right?


 I was writing unit tests for MarshalJSON, and it came as a surprise to me.

OpenNota

unread,
Apr 12, 2014, 12:46:47 AM4/12/14
to golan...@googlegroups.com
On Saturday, April 12, 2014 11:34:23 AM UTC+7, Benjamin R. Haskell wrote:
It's not configurable, AFAICT.  From https://code.google.com/p/go/source/browse/src/pkg/encoding/json/stream.go#165

        // Terminate each value with a newline.
        // This makes the output look a little nicer
        // when debugging, and some kind of space
        // is required if the encoded value was a number,
        // so that the reader knows there aren't more
        // digits coming.
        e.WriteByte('\n')

Does not the reader already knows that, because numbers will be followed by ',', '}', ']', or EOF?

OpenNota

unread,
Apr 12, 2014, 1:00:14 AM4/12/14
to golan...@googlegroups.com

OpenNota

unread,
Apr 12, 2014, 5:23:08 AM4/12/14
to golan...@googlegroups.com
On Saturday, April 12, 2014 1:24:10 PM UTC+7, Islan Dberry wrote:
The decoder handles streams of JSON values. See http://godoc.org/encoding/json#example-Decoder. The decoder requires a whitespace separator between a number and the JSON value that follows it.  The encoder adds whitespace to accommodate the decoder. 

Interesting.
 

Dan Kortschak

unread,
Apr 12, 2014, 5:32:05 AM4/12/14
to Islan Dberry, golan...@googlegroups.com
The \n are not necessary for that.

http://play.golang.org/p/HVyS_WZ4xK

OpenNota

unread,
Apr 12, 2014, 5:33:51 AM4/12/14
to golan...@googlegroups.com, Islan Dberry

On Saturday, April 12, 2014 4:32:05 PM UTC+7, kortschak wrote:
The \n are not necessary for that.

I suppose they are necessary for streams of json integers.
 

Dan Kortschak

unread,
Apr 12, 2014, 5:41:16 AM4/12/14
to OpenNota, golan...@googlegroups.com, Islan Dberry
Yes, that's true.
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages