7件ヒット
[1-7件を表示]
(0.597秒)
トップページ > :Object#class > :to_regexp > :to_hash > :to_proc > :2.4.0 > :instance_of? > :class_variable_get > :<=> > :json
検索結果
先頭5件
-
JSON
:: Generator :: GeneratorMethods :: String # to _ json _ raw _ object -> Hash (18346.0) -
生の文字列を格納したハッシュを生成します。
... JSON に変換する場合に使用してください。
require 'json'
"にほんご".encode("euc-jp").to_json_raw_object
# => {"json_class"=>"String", "raw"=>[164, 203, 164, 219, 164, 243, 164, 180]}
"にほんご".encode("euc-jp").to_json # source sequence is illegal/malformed (JSON::Ge... -
JSON
:: Generator :: GeneratorMethods :: Object # to _ json(state _ or _ hash = nil) -> String (9043.0) -
自身を to_s で文字列にした結果を JSON 形式の文字列に変換して返します。
...した結果を JSON 形式の文字列に変換して返します。
このメソッドはあるオブジェクトに to_json メソッドが定義されていない場合に使用する
フォールバックのためのメソッドです。
@param state_or_hash 生成する JSON 形式の文字......をカスタマイズするため
に JSON::State のインスタンスか、
JSON::State.new の引数と同じ Hash を
指定します。
//emlist[例][ruby]{
require "json"
class Person
attr :name, :age
def initialize(name, ag......e)
@name, @age = name, age
end
end
tanaka = Person.new("tanaka", 29)
tanaka.to_json # => "\"#<Person:0x00007ffdec0167c8>\""
tanaka.method(:to_json).owner # => JSON::Ext::Generator::GeneratorMethods::Object
//}... -
JSON
. [](object , options) -> object (667.0) -
文字列のように扱えるデータを受け取った場合は Ruby のオブジェクトに変換して返します。 そうでない場合は JSON に変換して返します。
... JSON に変換して返します。
@param object 任意のオブジェクト指定可能です。
@param options JSON.#parse, JSON.#generate の説明を参照してください。
//emlist[例][ruby]{
require "json"
string=<<JSON
{ "a":1, "b":2, "c":3 }
JSON
hash = { a: 1, b: 2, c: 3 }
JSON[s......# => Hash
JSON[string] # => {"a"=>1, "b"=>2, "c"=>3}
JSON[string, symbolize_names: true] # => {:a=>1, :b=>2, :c=>3}
JSON[hash].class # => String
JSON[hash] # => "{\"a\":1,\"b\":2,\"c\":3}"
//}
@see JSON.#parse, JSON.#generate... -
JSON
:: Parser # parse -> object (382.0) -
現在のソースをパースして結果を Ruby のオブジェクトとして返します。
...uby のオブジェクトとして返します。
//emlist[例][ruby]{
require 'json'
class Person
attr_accessor :name, :age
def []=(key, value)
instance_variable_set("@#{key}", value)
end
end
parser = JSON::Parser.new(DATA.read, object_class: Person)
person = parser.parse
person.class......# => Person
person.name # => "tanaka"
person.age # => 20
__END__
{
"name":"tanaka",
"age":20
}
//}
@see JSON::Parser#source... -
JSON
:: Parser . new(source , options => {}) -> JSON :: Parser (169.0) -
パーサを初期化します。
...も付加情報を生成しません。デフォルトは真です。
: :object_class
JSON のオブジェクトを変換するクラスを指定します。デフォルトは Hash です。
: :array_class
JSON の配列を変換するクラスを指定します。デフォルトは Array です......文字列を指定します。
@param options オプションを指定するためのハッシュです。
//emlist[例][ruby]{
require 'json'
parser = JSON::Parser.new(DATA.read)
print parser.source
# => {
# => "Tanaka": {
# => "name":"tanaka",
# => "age":20
# => },
# => "Suzuki":......"age":25
}
}
//}
//emlist[例 object_class を指定する][ruby]{
require 'json'
class Person
attr_accessor :name, :age
def []=(key, value)
instance_variable_set("@#{key}", value)
end
end
parser = JSON::Parser.new(DATA.read, object_class: Person)
person = parser.parse
person.class... -
JSON
. create _ id -> String (133.0) -
json_create メソッドで使用するクラスを決定するために使用する値を返します。
...
json_create メソッドで使用するクラスを決定するために使用する値を返します。
デフォルトは "json_class" です。
//emlist[例][ruby]{
require "json"
class User
attr :id, :name
def initialize(id, name)
@id, @name = id, name
end
def self.json_create(......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_... -
JSON
:: State . new(options = {}) -> JSON :: State (61.0) -
自身を初期化します。
...字列を指定します。デフォルトは空文字列です。
: :space
JSON 形式の文字列のトークン間に挿入する文字列を指定します。デフォルトは空文字列です。
: :space_before
JSON 形式の文字列中で JavaScript のオブジェクトを表す部分......。デフォルトは空文字列です。
: :object_nl
JSON 形式の文字列中に現れる JavaScript のオブジェクトの行末に挿入する文字列を指定します。
デフォルトは空文字列です。
: :array_nl
JSON 形式の文字列中に現れる JavaScript の配列......ジェクトの循環をチェックします。
この動作がデフォルトです。
: :allow_nan
真を指定した場合、JSON::NaN, JSON::Infinity,
JSON::MinusInfinity を生成することを許すようになります。
偽を指定した場合、これらの値を生成しよう...