Skip to content

Instantly share code, notes, and snippets.

@stefl
Created February 5, 2011 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefl/812700 to your computer and use it in GitHub Desktop.
Save stefl/812700 to your computer and use it in GitHub Desktop.
Json gem users take note...
# Deal with the usual JSON gem idiocy. Guys, come on. I want to serialize an integer, surely this can't be that hard
class Fixnum
def to_json(options = nil)
to_s
end
end
@DRMacIver
Copy link

Alas, it's actually behaving in compliance to the (rather bizarre) spec. See http://www.ietf.org/rfc/rfc4627.txt?number=4627

Quoted:

A JSON text is a serialized object or array.

  JSON-text = object / array

  A JSON generator produces JSON text.  The resulting text MUST strictly conform to the JSON grammar.

@stefl
Copy link
Author

stefl commented Feb 7, 2011

Reading that I still don't quite understand why I have to turn an integer into a string to get my very basic datamapper model instance to serialize it's Serial ID. It seems from the spec that you're allowed to have integers... Guess I'm being dumb.

@DRMacIver
Copy link

Integers are JSON values, not JSON text. JSON generators are required to output JSON text, which is just objects and arrays. (Yes, it's stupid).

What's the actual code you have that's failing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment