A test of the technique illustrated by Simon Willison at https://til.simonwillison.net/llms/docs-from-tests
# Parse JSON string into Ruby objects
JSON.parse('{"name": "John", "age": 30}') # => {"name"=>"John", "age"=>30}
# Parse JSON array
JSON.parse('[1, 2, 3]') # => [1, 2, 3]
# Parse from IO objects
File.open('data.json') do |file|
JSON.parse(file.read)
end
# Generate JSON from Ruby objects
JSON.generate({name: "John", age: 30}) # => '{"name":"John","age":30}'
# Alternative syntax using #to_json
{name: "John", age: 30}.to_json # => '{"name":"John","age":30}'
# Pretty print JSON with nice formatting
JSON.pretty_generate({name: "John", age: 30})
# {
# "name": "John",
# "age": 30
# }
# Symbolize names (keys become symbols)
JSON.parse('{"name": "John"}', symbolize_names: true) # => {:name=>"John"}
# Allow NaN, Infinity in JSON
JSON.parse('{"value": NaN}', allow_nan: true)
# Set maximum nesting depth
JSON.parse(json, max_nesting: 100) # Raises error if nesting > 100
# Allow trailing commas in arrays/objects
JSON.parse('[1,2,3,]', allow_trailing_comma: true) # => [1,2,3]
# Parse numeric values as BigDecimal
JSON.parse('{"price": 9.99}', decimal_class: BigDecimal)
# ASCII encode all non-ASCII characters
JSON.generate(data, ascii_only: true)
# Control spacing and indentation
JSON.generate(data, space: ' ', indent: ' ')
# Allow NaN/Infinity in output
JSON.generate(data, allow_nan: true)
# Escape forward slashes
JSON.generate(data, escape_slash: true)
# Make output safe for all JavaScript engines
JSON.generate(data, script_safe: true)
class Person
def initialize(name)
@name = name
end
# Define how object should be serialized to JSON
def to_json(*args)
{
'json_class' => self.class.name,
'name' => @name
}.to_json(*args)
end
# Define how to create object from JSON
def self.json_create(hash)
new(hash['name'])
end
end
# Usage
person = Person.new("John")
json = person.to_json
parsed = JSON.parse(json, create_additions: true) # Creates Person object
begin
JSON.parse(invalid_json)
rescue JSON::ParserError => e
puts "Invalid JSON: #{e.message}"
end
begin
JSON.generate(invalid_object)
rescue JSON::GeneratorError => e
puts "Cannot generate JSON: #{e.message}"
end
-
Always use exception handling when parsing untrusted JSON input
-
Use
JSON.parse
withsymbolize_names: true
when working with symbol keys -
Use
JSON.pretty_generate
for human-readable output -
Set appropriate
max_nesting
limits when parsing untrusted data -
Use
create_additions: true
carefully as it can execute arbitrary code -
Consider using
decimal_class: BigDecimal
when precision is important -
Test JSON generation/parsing with various character encodings
This documentation covers the main functionality of the JSON library based on the test files. The library provides many additional options and features that can be found in the source code and official documentation.