json.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ##
  2. # = JavaScript Object Notation (JSON)
  3. #
  4. # JSON is a lightweight data-interchange format. It is easy for us
  5. # humans to read and write. Plus, equally simple for machines to generate or parse.
  6. # JSON is completely language agnostic, making it the ideal interchange format.
  7. #
  8. # Built on two universally available structures:
  9. # 1. A collection of name/value pairs. Often referred to as an _object_, hash table, record, struct, keyed list, or associative array.
  10. # 2. An ordered list of values. More commonly called an _array_, vector, sequence or list.
  11. #
  12. # To read more about JSON visit: http://json.org
  13. #
  14. # == Parsing JSON
  15. #
  16. # To parse a JSON string received by another application or generated within
  17. # your existing application:
  18. #
  19. # require 'json'
  20. #
  21. # my_hash = JSON.parse('{"hello": "goodbye"}')
  22. # puts my_hash["hello"] => "goodbye"
  23. #
  24. # Notice the extra quotes <tt>''</tt> around the hash notation. Ruby expects
  25. # the argument to be a string and can't convert objects like a hash or array.
  26. #
  27. # Ruby converts your string into a hash
  28. #
  29. # == Generating JSON
  30. #
  31. # Creating a JSON string for communication or serialization is
  32. # just as simple.
  33. #
  34. # require 'json'
  35. #
  36. # my_hash = {:hello => "goodbye"}
  37. # puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"
  38. #
  39. # Or an alternative way:
  40. #
  41. # require 'json'
  42. # puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}"
  43. #
  44. # <tt>JSON.generate</tt> only allows objects or arrays to be converted
  45. # to JSON syntax. <tt>to_json</tt>, however, accepts many Ruby classes
  46. # even though it acts only as a method for serialization:
  47. #
  48. # require 'json'
  49. #
  50. # 1.to_json => "1"
  51. #
  52. require 'json/common'
  53. module JSON
  54. require 'json/version'
  55. begin
  56. require 'json/ext'
  57. rescue LoadError
  58. require 'json/pure'
  59. end
  60. end