るりまサーチ

最速Rubyリファレンスマニュアル検索!
77件ヒット [1-77件を表示] (0.043秒)

別のキーワード

  1. argf.class lines
  2. argf.class each_line
  3. argf.class each
  4. argf.class to_a
  5. argf.class gets

検索結果

JSON::Generator::GeneratorMethods::String#to_json_raw_object -> Hash (6115.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 (3014.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, age)...
...@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 (222.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...
....class # => 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 (127.0)

現在のソースをパースして結果を Ruby のオブジェクトとして返します。

...ます。

//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 (56.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":...
...//}

//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 # => Person
pers...

絞り込み条件を変える

JSON.create_id -> String (44.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(...
...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">
//}...

JSON::State.new(options = {}) -> JSON::State (20.0)

自身を初期化します。

...字列を指定します。デフォルトは空文字列です。
: :space
JSON
形式の文字列のトークン間に挿入する文字列を指定します。デフォルトは空文字列です。
: :space_before
JSON
形式の文字列中で JavaScript のオブジェクトを表す部分...
...す。デフォルトは空文字列です。
: :object_nl
JSON
形式の文字列中に現れる JavaScript のオブジェクトの行末に挿入する文字列を指定します。
デフォルトは空文字列です。
: :array_nl
JSON
形式の文字列中に現れる JavaScript の配...
...ジェクトの循環をチェックします。
この動作がデフォルトです。
: :allow_nan
真を指定した場合、JSON::NaN, JSON::Infinity,
JSON
::MinusInfinity を生成することを許すようになります。
偽を指定した場合、これらの値を生成しよう...