|
From: Jeff S. <je...@in...> - 2011-09-08 19:51:24
|
On Thu, Sep 8, 2011 at 4:43 AM, Matthew Gilliard <mat...@gm...> wrote: > > The rfc for application/json (http://www.ietf.org/rfc/rfc4627.txt) > states that JSON text must be an object or an array, which are fully > defined at http://json.org/. UGH. Seriously, in this case the the spec is broken - or at least ambiguous, because it doesn't address how to turn a simple String into JSON. Large swaths of the internet properly interpret simple strings, numbers, and booleans as JSON - including Facebook (yeah, no shining example but we have to live with it) which will produce 'false' on numerous occasions. On Thu, Sep 8, 2011 at 5:38 AM, Bill Burke <bb...@re...> wrote: > I don't get it. Why not just quote the damn string? Current behavior violates the principle of least astonishment. And is the least useful option. Look at this: // Produces '{ field: value }', which parses nicely by browsers and ObjectMapper Object getThing() { return new ComplexObject(); } // Produces '[ 123 ]', which parses nicely by browsers and ObjectMapper Object getThing() { return new int[] { 123 }; } // Produces '123', which parses nicely by browsers and ObjectMapper Object getThing() { return 123; } // Produces 'false', which parses nicely by browsers and ObjectMapper Object getThing() { return false; } // Produces 'hello', which produces parse errors in browsers and ObjectMapper Object getThing() { return "hello"; } In the current system String behaves radically different from *nearly every other type* you can possibly return from a method, in that it alone is not JSON encoded. At least three people on this list (counting myself) have voiced the desire to return a simple String to the client from a method, and have that String get JSON encoded properly. Right now you cannot construct a REST method that returns a simple String without putting JSON formatting logic in the Resource, which means I have weird formatting logic mixed with my business logic. The JSON spec is at the very least ambiguous about how to encode a simple String (and boolean and number). Resteasy does the "expected thing" with numbers and booleans, I want it to do the "expected thing" with strings as well. Jeff |