Encoding Strings for Embedding in JSON with Ruby on the Command-Line

One of the difficult things about working with JSON, especially when it is used as a format for storing content, such as when working with the Microformats2 standard.
Because I was finding it quite difficult to hand-craft a string that correctly escaped quotes, newlines, etc, I crafted the following one-liner with Ruby:
echo "A string" | ruby -e 'p ARGF.read'
ruby -e 'p ARGF.read' < file.txt