Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e3dfb4760b9d
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4400657b6ea5
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Apr 11, 2018

  1. Add examples for parse(IO) to JSON, YAML

    also fixes syntax for class methods in docs
    straight-shoota committed Apr 11, 2018
    Copy the full SHA
    89cb674 View commit details

Commits on Apr 12, 2018

  1. Merge pull request #5936 from straight-shoota/jm/docs/json-yaml-read-…

    …file
    
    Docs: Add examples for parse(IO) to JSON, YAML
    sdogruyol authored Apr 12, 2018
    Copy the full SHA
    4400657 View commit details
Showing with 29 additions and 7 deletions.
  1. +17 −6 src/json.cr
  2. +12 −1 src/yaml.cr
23 changes: 17 additions & 6 deletions src/json.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# The JSON module allows parsing and generating [JSON](http://json.org/) documents.
#
# ### Parsing and generating with `JSON#mapping`
# ### Parsing and generating with `JSON.mapping`
#
# Use `JSON#mapping` to define how an object is mapped to JSON, making it
# Use `JSON.mapping` to define how an object is mapped to JSON, making it
# the recommended easy, type-safe and efficient option for parsing and generating
# JSON. Refer to that module's documentation to learn about it.
#
# ### Parsing with `JSON#parse`
# ### Parsing with `JSON.parse`
#
# `JSON#parse` will return an `Any`, which is a convenient wrapper around all possible JSON types,
# `JSON.parse` will return an `Any`, which is a convenient wrapper around all possible JSON types,
# making it easy to traverse a complex JSON structure but requires some casts from time to time,
# mostly via some method invocations.
#
@@ -26,7 +26,18 @@
# value[0].as_i + 10 # => 11
# ```
#
# The above is useful for dealing with a dynamic JSON structure but is slower than using `JSON#mapping`.
# `JSON.parse` can read from an `IO` directly (such as a file) which saves
# allocating a string:
#
# ```
# require "json"
#
# json = File.open("path/to/file.json") do |file|
# JSON.parse(file)
# end
# ```
#
# Parsing with `JSON.parse` is useful for dealing with a dynamic JSON structure but is slower than using `JSON.mapping`.
#
# ### Generating with `JSON.build`
#
@@ -55,7 +66,7 @@
#
# `to_json`, `to_json(IO)` and `to_json(JSON::Builder)` methods are provided
# for primitive types, but you need to define `to_json(JSON::Builder)`
# for custom objects, either manually or using `JSON#mapping`.
# for custom objects, either manually or using `JSON.mapping`.
module JSON
# Generic JSON error.
class Error < Exception
13 changes: 12 additions & 1 deletion src/yaml.cr
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ require "base64"
#
# ### Parsing with `#parse` and `#parse_all`
#
# `YAML#parse` will return an `Any`, which is a convenient wrapper around all possible
# `YAML.parse` will return an `Any`, which is a convenient wrapper around all possible
# YAML core types, making it easy to traverse a complex YAML structure but requires
# some casts from time to time, mostly via some method invocations.
#
@@ -25,6 +25,17 @@ require "base64"
# data["foo"]["bar"]["baz"][1].as_s # => "fox"
# ```
#
# `YAML.parse` can read from an `IO` directly (such as a file) which saves
# allocating a string:
#
# ```
# require "yaml"
#
# yaml = File.open("path/to/file.yml") do |file|
# YAML.parse(file)
# end
# ```
#
# ### Parsing with `from_yaml`
#
# A type `T` can be deserialized from YAML by invoking `T.from_yaml(string_or_io)`.