最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ruby解析處理YAML和json格式數(shù)據(jù)

 更新時間:2022年04月18日 15:22:54   作者:駿馬金龍  
這篇文章主要介紹了Ruby對YAML和json格式的數(shù)據(jù)處理方法,json序列化、json反序列化,json解析等操作,需要的朋友可以參考下

Ruby處理YAML

Ruby的標(biāo)準(zhǔn)庫YAML基于Psych:https://ruby-doc.org/stdlib-2.6.2/libdoc/psych/rdoc/Psych.html

require 'yaml' 之后,為大多數(shù)的基本數(shù)據(jù)類型都提供了 to_ yaml() 方法,用于將各數(shù)據(jù)類型的對象轉(zhuǎn)換為yaml格式。

例如:

require 'yaml'
require 'set'

p "hello world".to_yaml
p 123.to_yaml
p %w(perl shell php).to_yaml
p ({one: 1, two: 2}).to_yaml
p Set.new([1,2,3]).to_yaml

得到:

"--- hello world\n"
"--- 123\n"
"---\n- perl\n- shell\n- php\n"
"---\n:one: 1\n:two: 2\n"
"--- !ruby/object:Set\nhash:\n  1: true\n  2: true\n  3: true\n"

也可以使用YAML.dump()方法實現(xiàn)和to_yaml相同的功能,它還可以寫入文件。

users = [{name: 'Bob', permissions: ['Read']},
 {name: 'Alice', permissions:['Read', 'Write']}]

File.open("/tmp/a.yml","w") { |f| YAML.dump(users, f) }

查看文件:

---
- :name: Bob             #=> 注意,保留了hash源數(shù)據(jù)中的符號
  :permissions:
  - Read
- :name: Alice
  :permissions:
  - Read
  - Write

用YAML.load()從YAML中讀取數(shù)據(jù):

require 'yaml'

pp YAML.load(DATA)

__END__
mysql:
  passwd: P@ssword1!
  user: root
  port: 3306
  other1: nil
  other2: false
  other3: ""
  hosts: 
    - ip: 10.10.1.1
      hostname: node1
    - ip: 10.10.1.2
      hostname: node2

得到:

{"mysql"=>
  {"passwd"=>"P@ssword1!",      #=> 注意,key是String而非Symbol
   "user"=>"root",
   "port"=>3306,
   "other1"=>"nil",
   "other2"=>false,
   "other3"=>"",
   "hosts"=>
    [{"ip"=>"10.10.1.1", "hostname"=>"node1"},
     {"ip"=>"10.10.1.2", "hostname"=>"node2"}]}}

如果想讓hash的key是符號而非字符串,可以設(shè)置選項symbolize_names: true

pp YAML.load(DATA, symbolize_names: true)

需要注意,YAML可以將對象進行序列化,所以有幾方面注意事項:

  • 在反序列化的時候需要也require涉及到的文件,例如對Set類型序列化后,在反序列化時如不require 'set'則無法還原對象
  • 有些底層對象不能序列化,包括IO流、Ruby代碼對象Proc、Binding等
  • 不要反序列化不被信任的數(shù)據(jù)對象(比如用戶輸入的數(shù)據(jù)),此時可使用safe_load(),它默認(rèn)只允許加載以下幾種類型的數(shù)據(jù):
    • TrueClass
    • FalseClass
    • NilClass
    • Numeric
    • String
    • Array
    • Hash
  • 如果確實想要加載額外的數(shù)據(jù)類型,可以在safe_load()中指定參數(shù)permitted_classes: []或permitted_symbols: []

Ruby處理Json數(shù)據(jù)

轉(zhuǎn)為json格式字符串

使用JSON.generate()可以將對象或數(shù)組轉(zhuǎn)換為JSON格式的數(shù)據(jù):

require 'json'
p JSON.generate "abc"
p JSON.generate 123
p JSON.generate true
p JSON.generate nil
p JSON.generate [2,3,4]
p JSON.generate({name: "junmajinlong", age: 23})
require 'set'
p JSON.generate(Set.new([1,23,44]))

得到:

"\"abc\""
"123"
"true"
"null"
"[2,3,4]"
"{\"name\":\"junmajinlong\",\"age\":23}"
"\"#<Set: {1, 23, 44}>\""

當(dāng)require 'json'后,很多ruby類型都具備了一個to_json的方法,可以直接將該類型的數(shù)據(jù)轉(zhuǎn)換為json數(shù)據(jù):

p ({name: "junmajinlong", age: 23}).to_json
p (Set.new([1,23,44])).to_json

得到:

"{\"name\":\"junmajinlong\",\"age\":23}"
"\"#<Set: {1, 23, 44}>\""

此外,JSON.dump()也可以將對象轉(zhuǎn)換為JSON格式的字符串,而且它還支持寫入文件:

hsh = {name: "junmajinlong", age: 23}
File.open("/tmp/a.json", "w") {|f| JSON.dump(hsh, f)}

json格式字符串轉(zhuǎn)為Ruby對象

要從json格式字符串轉(zhuǎn)為ruby對象,有一些選項可設(shè)置,參考https://ruby-doc.org/stdlib-2.7.1/libdoc/json/rdoc/JSON.html#method-i-parse,比如*symbolize_names*選項表示是否將json object中的key解析為符號類型的key,如果設(shè)置為false,則解析為字符串的key。

要將json格式的字符串解析為Ruby數(shù)據(jù)類型(Hash),使用JSON.parse(),

require 'json'

hsh = '{"name": "junmajinlong", "age": 23}'

p JSON.parse(hsh)
p JSON.parse(hsh, symbolize_names: true)

注意,上面的json字符串必須是合理的json數(shù)據(jù),比如key必須使用雙引號包圍而不能使用單引號,字符串必須使用雙引號包圍,等等。比如"{'name': 'junmajinlong', 'age': 23}"就不是合理的json字符串。

要從json文件中讀取json數(shù)據(jù)并轉(zhuǎn)換為Ruby數(shù)據(jù),使用load():

data = File.open("/tmp/a.json") do |f|
  JSON.load(f)
end

pp data
#=> {"name"=>"junmajinlong", "age"=>23}

自定義對象的轉(zhuǎn)換方式

json支持的數(shù)據(jù)類型有:

  • 字符串
  • 數(shù)值
  • 對象
  • 數(shù)組
  • 布爾
  • Null

從一種語言的數(shù)據(jù)轉(zhuǎn)換為Json數(shù)據(jù)時,如果數(shù)據(jù)類型也是JSON所支持的,可直接轉(zhuǎn)換,但如果包含了JSON不支持的類型,則可能報錯,也可能以一種對象字符串的方式保存,這取決于對應(yīng)的實現(xiàn)。

可以在對象中定義as_json實例方法來決定對象如何轉(zhuǎn)換為json字符串,再定義類方法from_json()來決定如何從json字符串中恢復(fù)為一個對象。

例如,

require 'json'
require 'date'

class Person
  attr_accessor :name, :birthday
  def initialize name, birthday
    @name = name
    @birthday = DateTime.parse(birthday)
  end
end

File.open("/tmp/p.json", "w") do |f|
  JSON.dump(Person.new("junmajinlong", "1999-10-11"), f)
end

查看保存的json數(shù)據(jù):

$ cat /tmp/p.json
"#<Person:0x00007fffc7e575d0>"

定義as_jsonfrmo_json

require 'json'
require 'date'

class Person
  attr_accessor :name, :birthday
  
  def initialize name, birthday
    @name = name
    @birthday = DateTime.parse(birthday)
  end
  
  def as_json
    {
      name: @name,
      birthday: @birthday.strftime("%F")
    }
  end

  def self.from_json json
    data = JSON.parse(json)
    new(data["name"], data["birthday"])
  end
end

之后要序列化、反序列化該對象,可:

data = Person.new("junmajinlong", "1999-10-11").as_json
p data

p1=Person.from_json(JSON.dump data)
p p1.birthday

如果是讀寫json文件,可:

person1 = Person.new("junmajinlong", "1999-10-11")
File.open("/tmp/p.json", "w") do |f|
  JSON.dump(person1.as_json, f)
end

p1 = File.open("/tmp/p.json") do |f|
  Person.from_json(f.read)
  # Person.from_json(JSON.load(f).to_json)
end
p p1

幾種JSON解析工具的性能測試

測試了json標(biāo)準(zhǔn)庫、oj和fast_josnparser解析json的性能,測試項包括:

  • 從文件中加載并解析json字符串為ruby對象
  • 從內(nèi)存json字符串中解析json字符串為ruby對象
  • 帶有symbolize_keys/symbolize_names轉(zhuǎn)換時的解析
  • json標(biāo)準(zhǔn)庫和oj將ruby對象dump為json字符串
  • json標(biāo)準(zhǔn)庫和oj將ruby對象dump為json字符串保存到文件

注:

  • fast_jsonparser沒有dump功能,只有解析json字符串功能
  • oj在將對象轉(zhuǎn)換為json字符串時,可能會丟失數(shù)據(jù)的精度,比如浮點數(shù)的精度

測試的json字符串?dāng)?shù)量大約50M。

測試了ruby 2.7.1和ruby 3.0.1兩個版本,gem包的版本信息如下:

fast_jsonparser (0.5.0)
json (default: 2.5.1)
oj (3.11.7)

測試代碼:

require 'benchmark'
require 'json'
require 'oj'
require 'fast_jsonparser'

# warm
json_file='test'  # 文件大小大約50M
str = File.read(json_file)

######## JSON

puts " load file ".center(80, '-')
Benchmark.bm(30) do |x|
  x.report("JSON.load:") { File.open(json_file){ |f| JSON.load(f) } }
  x.report("Oj.load_file:") { Oj.load_file(json_file) }
  x.report("FastJsonparser.load:") { FastJsonparser.load(json_file) }
end

puts
puts " load file with symbolize_keys ".center(80, '-')
Benchmark.bm(30) do |x|
  x.report("JSON.load:") { File.open(json_file){ |f| JSON.load(f, nil, symbolize_names: true, create_additions: false) } }
  x.report("Oj.load_file:") { Oj.load_file(json_file, symbol_keys: true) }
  x.report("FastJsonparser.load:") { FastJsonparser.load(json_file, symbolize_keys: true) }
end

puts
puts " parse str ".center(80, '-')
Benchmark.bm(30) do |x|
  x.report("JSON.parse:") { JSON.parse(str) }
  x.report("Oj.load:") { Oj.load(str) }
  x.report("FastJsonparser.parse:") { FastJsonparser.parse(str) }
end

puts
puts " parse str with symbolize_keys ".center(80, '-')
Benchmark.bm(30) do |x|
  x.report("JSON.parse:") { JSON.parse(str, symbolize_names: true) }
  x.report("Oj.load:") { Oj.load(str, symbol_keys: true) }
  x.report("FastJsonparser.parse:") { FastJsonparser.parse(str, symbolize_keys: true) }
end

obj = JSON.parse(str, symbolize_names: true)

puts 
puts " dump JSON to str ".center(80, '-')
Benchmark.bm(30) do |x|
  x.report("JSON.dump:") { JSON.dump(obj) }
  x.report("Oj.dump:") { Oj.dump(obj) }
end

puts 
puts " dump JSON to file ".center(80, '-')
Benchmark.bm(30) do |x|
  x.report("JSON.dump:") { File.open('0_json_dump', 'w') {|f| JSON.dump(obj, f) } }
  x.report("Oj.to_file:") { Oj.to_file('0_oj_dump', obj) }
end

測試結(jié)果:

Ruby 2.7.1中:

---------------------------------- load file -----------------------------------
                                     user     system      total        real
JSON.load:                       1.591831   0.058021   1.649852 (  1.738119)
Oj.load_file:                    1.350385   0.057684   1.408069 (  2.434268) <-慢
FastJsonparser.load:             0.653968   0.103258   0.757226 (  0.848913) <-快

------------------------ load file with symbolize_keys -------------------------
                                     user     system      total        real
JSON.load:                       1.212617   0.039052   1.251669 (  1.349545)
Oj.load_file:                    1.432059   0.098950   1.531009 (  2.679610) <-慢
FastJsonparser.load:             0.695538   0.008384   0.703922 (  0.797081) <-快

---------------------------------- parse str -----------------------------------
                                     user     system      total        real
JSON.parse:                      1.343596   0.000000   1.343596 (  1.350368)
Oj.load:                         1.133612   0.000000   1.133612 (  1.140939)
FastJsonparser.parse:            0.701701   0.012340   0.714041 (  0.720296) <-快

------------------------ parse str with symbolize_keys -------------------------
                                     user     system      total        real
JSON.parse:                      1.250775   0.000000   1.250775 (  1.258796)
Oj.load:                         1.131296   0.000000   1.131296 (  1.138020)
FastJsonparser.parse:            0.697433   0.015962   0.713395 (  0.719439) <-快

------------------------------- dump JSON to str -------------------------------
                                     user     system      total        real
JSON.dump:                       1.374611   0.028454   1.403065 (  1.403081)
Oj.dump:                         1.025049   0.040184   1.065233 (  1.065246) <-快

------------------------------ dump JSON to file -------------------------------
                                     user     system      total        real
JSON.dump:                       1.234362   0.040246   1.274608 (  1.369214)
Oj.to_file:                      1.168707   0.000000   1.168707 (  1.270957)

Ruby 3.0.1中:

---------------------------------- load file -----------------------------------
                                     user     system      total        real
JSON.load:                       1.362151   0.083610   1.445761 (  1.569754)
Oj.load_file:                    1.343601   0.182046   1.525647 (  2.684472) <-慢
FastJsonparser.load:             2.634435   0.052734   2.687169 (  2.776105) <-慢

------------------------ load file with symbolize_keys -------------------------
                                     user     system      total        real
JSON.load:                       1.287954   0.018572   1.306526 (  1.409770)
Oj.load_file:                    1.478750   0.043847   1.522597 (  2.668882) <-慢
FastJsonparser.load:             2.717857   0.006164   2.724021 (  2.822728) <-慢

---------------------------------- parse str -----------------------------------
                                     user     system      total        real
JSON.parse:                      1.242225   0.008661   1.250886 (  1.304554)
Oj.load:                         1.097922   0.000000   1.097922 (  1.110031)
FastJsonparser.parse:            2.602679   0.017232   2.619911 (  2.634604) <-慢

------------------------ parse str with symbolize_keys -------------------------
                                     user     system      total        real
JSON.parse:                      1.368262   0.000000   1.368262 (  1.380730)
Oj.load:                         1.332349   0.000000   1.332349 (  1.346331)
FastJsonparser.parse:            2.706804   0.007238   2.714042 (  2.726935) <-慢

------------------------------- dump JSON to str -------------------------------
                                     user     system      total        real
JSON.dump:                       1.724653   0.009250   1.733903 (  1.733912)
Oj.dump:                         1.298235   0.030041   1.328276 (  1.328279) <-快

------------------------------ dump JSON to file -------------------------------
                                     user     system      total        real
JSON.dump:                       1.765664   0.040595   1.806259 (  1.905785)
Oj.to_file:                      1.228744   0.020309   1.249053 (  1.349684) <-快
=end

性能測試結(jié)論:

  • (1).ruby 3之前,fast_jsonparser非常快,但是Ruby 3中的fast_jsonparser很慢
  • (2).OJ解析本地json字符串的性能比標(biāo)準(zhǔn)庫json性能稍好,但oj從文件中加載并解析json的速度很慢
  • (3).OJ將ruby對象解析為json字符串的效率比json標(biāo)準(zhǔn)庫性能好

即:

dump:
Oj.dump > JSON.dump

ruby3 之前:
FastJsonparser.load > JSON.load > Oj.load_file
FastJsonparser.parse > Oj.load > JSON.parse

ruby3 之后:
JSON.load > Oj.load_file > FastJsonparser.load
Oj.load > JSON.parse > FastJsonparser.parse

multi_json

有一個名為multi_json的gem包,它提供多種json包的功能,默認(rèn)采用OJ作為json的適配引擎。它支持下面幾種json適配器:

  • Oj Optimized JSON by Peter Ohler
  • Yajl Yet Another JSON Library by Brian Lopez
  • JSON The default JSON gem with C-extensions (ships with Ruby 1.9+)
  • JSON Pure A Ruby variant of the JSON gem
  • NSJSONSerialization Wrapper for Apple’s NSJSONSerialization in the Cocoa Framework (MacRuby only)
  • gson.rb A Ruby wrapper for google-gson library (JRuby only)
  • JrJackson JRuby wrapper for Jackson (JRuby only)
  • OkJson A simple, vendorable JSON parser

如果oj已被require,則默認(rèn)采用oj處理json,如果oj沒有被require,而是require了yajl,則采用yajl處理json,依次類推。

它提供了load()和dump()方法:

load(json_str, options = {})
  options: 
    symbolize_keys: true, false
    adapter:  oj, json_gem, yajl, json_pure, ok_json

dump(object, options = {})

更多關(guān)于Ruby處理操作YAMLRuby處理操作json方法請查看下面的相關(guān)鏈接

相關(guān)文章

  • Ruby中的類Google Map/Reduce框架Skynet介紹

    Ruby中的類Google Map/Reduce框架Skynet介紹

    這篇文章主要介紹了Ruby中的類Google Map/Reduce框架Skynet介紹,Skynet是一款創(chuàng)建分布式應(yīng)用程序的框架,需要的朋友可以參考下
    2015-01-01
  • Linux系統(tǒng)上配置Nginx+Ruby on Rails+MySQL超攻略

    Linux系統(tǒng)上配置Nginx+Ruby on Rails+MySQL超攻略

    這篇文章主要介紹了Linux系統(tǒng)上配置Nginx+Ruby on Rails+MySQL超攻略,用到了RVM,此種服務(wù)器搭建配置極力推薦!需要的朋友可以參考下
    2015-08-08
  • CentOS 7下配置Ruby語言開發(fā)環(huán)境的方法教程

    CentOS 7下配置Ruby語言開發(fā)環(huán)境的方法教程

    對于新入門的開發(fā)者,如何安裝 Ruby, Ruby Gems 和 Rails 的運行環(huán)境可能會是個問題,下面這篇文章主要給大家分享了在CentOS 7下配置Ruby語言開發(fā)環(huán)境的方法教程,文中介紹的非常詳細,需要的朋友們下面來一起看看吧。
    2017-05-05
  • Ruby入門介紹

    Ruby入門介紹

    Ruby入門介紹...
    2007-10-10
  • Ruby語法筆記

    Ruby語法筆記

    本文給大家記錄的是本人學(xué)習(xí)ruby之后所記錄下來的部分語法知識,分享給有需要的小伙伴,希望對大家能夠有所幫助。
    2016-02-02
  • Ruby元編程小結(jié)

    Ruby元編程小結(jié)

    這篇文章主要介紹了Ruby元編程小結(jié),元編程是可以在運行時動態(tài)的操作語言結(jié)構(gòu)(如類、模塊、實例變量等)的技術(shù),需要的朋友可以參考下
    2015-01-01
  • Ruby實現(xiàn)郵件主動推送觸發(fā)程序

    Ruby實現(xiàn)郵件主動推送觸發(fā)程序

    這篇文章主要介紹了Ruby實現(xiàn)郵件主動推送觸發(fā)程序,本文給出了客戶端輪詢和服務(wù)器主動推送的代碼實例,需要的朋友可以參考下
    2015-01-01
  • Ruby設(shè)計模式編程中對外觀模式的應(yīng)用實例分析

    Ruby設(shè)計模式編程中對外觀模式的應(yīng)用實例分析

    這篇文章主要介紹了Ruby設(shè)計模式編程中對外觀模式的應(yīng)用實例分析,外觀模式在Ruby on Rails開發(fā)項目中也經(jīng)常被用到,需要的朋友可以參考下
    2016-03-03
  • 寫一個漂亮Rakefile的方法

    寫一個漂亮Rakefile的方法

    這篇文章主要介紹了寫一個漂亮Rakefile的方法,文中主要就是寫一個類,繼承自 Tasklib,然后在這個類的初始化函數(shù)里用 task 或者 file 來定義實際完成任務(wù)的子 task 即可,需要的朋友可以參考下
    2014-06-06
  • Ruby多線程庫(Thread)使用方法詳解

    Ruby多線程庫(Thread)使用方法詳解

    這篇文章主要介紹了Ruby多線程庫(Thread)使用方法詳解,需要的朋友可以參考下
    2022-04-04

最新評論

甘南县| 宜川县| 柯坪县| 留坝县| 清原| 缙云县| 云霄县| 深水埗区| 洪湖市| 汕尾市| 时尚| 中超| 五指山市| 敖汉旗| 阜康市| 阜阳市| 西城区| 嘉黎县| 调兵山市| 会昌县| 肇东市| 攀枝花市| 蒙自县| 襄垣县| 宜良县| 长垣县| 蒙阴县| 绥宁县| 余江县| 新巴尔虎左旗| 乐平市| 隆林| 定边县| 邛崃市| 徐汇区| 无极县| 德令哈市| 什邡市| 礼泉县| 建平县| 汝城县|