views:

73

answers:

2

If I'm returning ["foo"] from a RESTful web query, Which of these is more proper? Will pedantic REST parsing die on the newline?

["foo"]\n     (with newline, Content-Length=8)
["foo"]       (no newline, Content-Length=7)

For easy regression testing I like the form with the newline, but I want to make sure I won't be breaking any application frameworks that might have a more strict view of the REST format.

+1  A: 

JSON is supposed to be whitespace-insensitive. I suppose technically the spec at JSON.org says "Whitespace can be inserted between any pair of tokens," but a parser would have to be very pedantic indeed to complain of whitespace before the first token or after the last.

Weston C
+3  A: 

Quoted from RFC 4627, bolded relevant section for emphasis.

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

A JSON text is a serialized object or array.

 JSON-text = object / array

These are the six structural characters:

 begin-array     = ws %x5B ws  ; [ left square bracket

 begin-object    = ws %x7B ws  ; { left curly bracket

 end-array       = ws %x5D ws  ; ] right square bracket

 end-object      = ws %x7D ws  ; } right curly bracket

 name-separator  = ws %x3A ws  ; : colon

 value-separator = ws %x2C ws  ; , comma

Insignificant whitespace is allowed before or after any of the six structural characters.

hobodave