るりまサーチ

最速Rubyリファレンスマニュアル検索!
33件ヒット [1-33件を表示] (0.133秒)
トップページ > クエリ:i[x] > クエリ:end[x] > クエリ:to_json[x]

別のキーワード

  1. _builtin end
  2. ripper end_seen?
  3. _builtin exclude_end?
  4. _builtin end_with?
  5. zlib end

ライブラリ

クラス

検索結果

Exception#to_json(*args) -> String (21231.0)

自身を JSON 形式の文字列に変換して返します。

...ds::Hash#to_json を呼び出しています。

@param args 引数はそのまま JSON::Generator::GeneratorMethods::Hash#to_json に渡されます。

//emlist[例][ruby]{
require "json/add/core"

begin
0/0
rescue => e
e.to_json # => "{\"json_class\":\"ZeroDivisionError\",\"m\":\"divided by 0...
...\",\"b\":[\"/path/to/test.rb:4:in `/'\",\"/path/to/test.rb:4:in `<main>'\"]}"
end

//}

@see JSON::Generator::GeneratorMethods::Hash#to_json...

JSON::Generator::GeneratorMethods::Object#to_json(state_or_hash = nil) -> String (18231.0)

自身を to_s で文字列にした結果を JSON 形式の文字列に変換して返します。

...身を to_s で文字列にした結果を JSON 形式の文字列に変換して返します。

このメソッドはあるオブジェクトに to_json メソッドが定義されていない場合に使用する
フォールバックのためのメソッドです。

@param state_or_hash 生成...
...指定します。

//emlist[例][ruby]{
require "json"

class Person
attr :name, :age

def initialize(name, age)
@name, @age = name, age
end

end


tanaka = Person.new("tanaka", 29)

tanaka.to_json # => "\"#<Person:0x00007ffdec0167c8>\""
tanaka.method(:to_json).owner # => JSON::Ext::Gene...

JSON.create_id -> String (6142.0)

json_create メソッドで使用するクラスを決定するために使用する値を返します。

...

//emlist[例][ruby]{
require "json"

class User
attr :id, :name
def initialize(id, name)
@id, @name = id, name
end


def self.json_create(object)
new(object['id'], object["name"])
end


def as_json(*)
{
JSON.create_id => self.class.name,
"id" => id,
"name...
..." => name,
}
end


def to_json(*)
as_json.to_json
end

end


json = JSON.generate(User.new(1, "tanaka"))
json # => "{\"json_class\":\"User\",\"id\":1,\"name\":\"tanaka\"}"
JSON.parse(json, create_additions: true)
# => #<User:0x0000557709b269e0 @id=1, @name="tanaka">
//}...