ရိုးရိုးလေး ရှင်းပြမယ်
File နဲ့ JSON ကို ရိုးရိုးလေး ပြောရရင်
Program ပြီးသွားရင် variable တွေ ပျောက်သွားပါတယ်။ File ကတော့ program ထက် ပိုကြာကြာ ရှိနေတဲ့ နေရာပါ၊ JSON ကတော့ ဘာသာစကားအများစုနဲ့ web service တွေက ဖတ်နိုင်တဲ့ စာသား format ပါ။ စာတိုလေး ရေးပြီး အံဆွဲထဲ ထည့်တာနဲ့ ယှဉ်ကြည့်ပါ: JSON.generate က Hash ကို စာရွက်ပေါ်က စာသားအဖြစ် ပြောင်းပေးတယ်၊ File.write က အံဆွဲထဲ ထည့်ပေးတယ်၊ File.read နဲ့ JSON.parse က ပြန်ထုတ်ပြီး Hash ကို ပြန်တည်ဆောက်ပေးပါတယ်။
အကျင့်နှစ်ခုက ဒါကို ဘေးကင်းစေပါတယ်။ File ကို block နဲ့ ဖွင့်ပါ၊ ဒါမှ တစ်ခုခု မှားသွားရင်တောင် Ruby က ပိတ်ပေးမှာပါ။ ပြီးတော့ သင်ယူနေတုန်းမှာ ယာယီ directory ကို သုံးပါ၊ ဒါမှ စမ်းသပ်မှုတွေက ကိုယ်တိုင် ရှင်းသွားမှာပါ။
Problem → Why → Solution
ပြဿနာ: လက်နဲ့ ရေးထားတဲ့ JSON က ပျက်သွားတယ်
ကိရိယာသေးသေးလေးတစ်ခုက ဆောင်းပါးတစ်ပုဒ်စီကို JSON နဲ့ တူတဲ့ စာသားတစ်ကြောင်းအဖြစ် သိမ်းပါတယ်၊ စာသားကို string interpolation နဲ့ လက်ဖြင့် တည်ဆောက်ပါတယ်။ ခေါင်းစဉ်တစ်ခုမှာ double quote မပါမချင်း အားလုံး အလုပ်လုပ်ပါတယ်။ {"title": "Say "hi""} ဆိုတဲ့ ရလဒ်က မှန်ကန်တဲ့ JSON မဟုတ်လို့ နောက် JSON.parse က JSON::ParserError ကို raise လုပ်ပြီး ပျက်နေတဲ့ လိုင်းက file ထဲမှာ သိမ်းပြီးသား ဖြစ်နေပါပြီ။
လက်ရေး JSON က တခြားစည်းမျဉ်းတွေကိုလည်း မေ့တတ်ပါတယ်: backslash၊ line ခွဲတာ၊ Unicode နဲ့ ကိန်းဂဏန်းတွေကို သတ်မှတ်ထားတဲ့ နည်းအတိုင်း escape ဒါမှမဟုတ် format လုပ်ရပါတယ်။ ဖတ်တဲ့ဘက်မှာလည်း ထောင်ချောက်တွေ ရှိပါတယ်။ မရှိတဲ့ file က Errno::ENOENT ကို၊ တစ်ဝက်ပဲ ရေးပြီး ကျန်ခဲ့တဲ့ file က ParserError ကို raise လုပ်ပါတယ်။ နှစ်မျိုးလုံးအတွက် ကြိုမစီစဉ်ထားတဲ့ program က crash ဖြစ်ပြီး ရပ်သွားပါတယ်။
require "json"
title = 'Say "hi"'
json = "{\"title\": \"#{title}\"}" # hand-built JSON
puts json # {"title": "Say "hi""}
JSON.parse(json) # JSON::ParserError
puts JSON.generate({ title: title }) # {"title":"Say \"hi\""}Library က စည်းမျဉ်းအားလုံးကို သိတယ်
JSON library က escape နဲ့ format စည်းမျဉ်းအားလုံးကို သိလို့ အမြဲ မှန်ကန်တဲ့ စာသားကို ထုတ်ပေးပါတယ်၊ ကိုယ်တိုင် စာသားဆက်တာမျိုး မလိုပါဘူး။ File.open ကို block နဲ့ သုံးတာကလည်း ပျက်ကွက်တဲ့အခါမှာတောင် ပိတ်ပေးတာကို အာမခံပါတယ်။ ဒါကြောင့် စာသား ဖန်တီးတာကို JSON.generate နဲ့၊ file ကို block နဲ့ ကိုင်ပြီး မျှော်လင့်ထားတဲ့ error နှစ်ခုကို rescue လုပ်ပါ။
Core concept: file ကို ဖတ်ရေးခြင်းနဲ့ JSON ပြောင်းလဲခြင်း
File.write(path, text) က file ကို ဖန်တီးတယ် ဒါမှမဟုတ် ပြန်ရေးတယ်၊ ရေးလိုက်တဲ့ byte အရေအတွက်ကို ပြန်ပေးတယ်။ File.read(path) က file တစ်ခုလုံးကို String တစ်ခုအဖြစ် ပေးတယ်။ File ကြီးတွေအတွက် File.foreach(path) က လိုင်းတစ်ခုချင်းစီကို တစ်ခါတည်း memory ထဲ မတင်ဘဲ ပေးပြီး File.readlines(path, chomp: true) က array တစ်ခုအဖြစ် ပေးပါတယ်။ ပိုထိန်းချုပ်ချင်ရင် File.open(path, mode) { |file| ... } ကို သုံးပါ: block ပြီးရင် Ruby က file ကို ပိတ်ပေးပြီး block ထဲမှာ exception ပေါ်တာတောင် ပိတ်ပါတယ်။
Mode က file ကို ဘယ်လို ဖွင့်မလဲ ဆိုတာ ပြောပါတယ်။ "r" က ဖတ်ရန်၊ "w" က ရေးရန်ဖြစ်ပြီး ဖွင့်ဖွင့်ချင်း file ကို ဖြတ်ပစ်တာမို့ ဘာမှ မရေးဘဲနဲ့တောင် အရင် အကြောင်းအရာတွေ ပျောက်ပါတယ်။ "a" က အဆုံးမှာ ဆက်ရေးပါတယ်။ Path တွေကို "dir/" + name လို ဆက်မရေးဘဲ File.join(dir, name) နဲ့ တည်ဆောက်ပါ၊ ဒါမှ OS တိုင်းမှာ အလုပ်လုပ်ပါတယ်။ Dir.mktmpdir { |dir| ... } က ယာယီ directory ကို ဖန်တီးပေးပြီး block ပြီးတာနဲ့ ထဲက အရာအားလုံးနဲ့အတူ ဖျက်ပေးလို့ စမ်းသပ်ဖို့ သင့်တော်ပါတယ်။
JSON အတွက် require "json" ကို အရင်လုပ်ပါ။ JSON.generate(object) က compact စာသား၊ JSON.pretty_generate(object) က လူဖတ်ရလွယ်တဲ့ indent ပါတဲ့ စာသား ထုတ်ပေးပါတယ် (object.to_json လည်း generate နဲ့ အတူတူပါ)။ JSON.parse(text) က Hash တစ်ခုကို ပြန်ပေးပြီး key တွေက String ဖြစ်ပါတယ်၊ symbolize_names: true ထည့်ရင် key တွေ symbol ဖြစ်ပါတယ် (value တွေကိုတော့ မပြောင်းပါဘူး)။ မှန်ကန်မှု မရှိတဲ့ စာသားဆိုရင် JSON::ParserError ပေါ်ပြီး ၎င်းက JSON::JSONError ကနေ StandardError ကို ဆင်းသက်ပါတယ်။
JSON က Ruby ရဲ့ အမျိုးအစားအားလုံးကို မထောက်ပံ့ပါဘူး၊ ဒါကြောင့် သိမ်းပြီး ပြန်ယူတာ (round trip) က အမြဲ မူရင်းနဲ့ မတူပါဘူး။ JSON မှာ symbol type မရှိလို့ symbol value တွေက string ဖြစ်ပြီး String မဟုတ်တဲ့ key (7 လို) တွေလည်း string ဖြစ်ပါတယ်။ nil က null ဖြစ်ပြီး ပြန်ပါလာတယ်၊ Time က to_s စာသားဖြစ်သွားလို့ ပြန်ယူချင်ရင် iso8601 နဲ့ သိမ်းပြီး Time.iso8601 နဲ့ ပြန်ပြောင်းပါ။ NaN နဲ့ Infinity တို့က JSON::GeneratorError ပေးပါတယ်။
| Method | လုပ်ပေးတာ | သတိထားရမှာ |
|---|---|---|
| File.write(path, text) | File ကို ဖန်တီး ဒါမှမဟုတ် ပြန်ရေး | ရှိပြီးသား အကြောင်းအရာ ပျောက်မယ် |
| File.read(path) | File တစ်ခုလုံးကို String တစ်ခုအဖြစ် | File ကြီးရင် memory အများကြီး သုံးမယ် |
| File.foreach(path) | လိုင်းတစ်ခုချင်းစီ ပေးမယ် | လိုင်းတိုင်းမှာ \n ပါနေမယ်၊ chomp လုပ်ပါ |
| File.open(path, "a") { } | အဆုံးမှာ ဆက်ရေးပြီး block ပြီးရင် ပိတ် | "w" ကတော့ ဖွင့်ဖွင့်ချင်း ဖြတ်ပစ်တယ် |
- Memory ထဲက Hash
- JSON.generate က String ထုတ်
- File.write က disk ပေါ် တင်
- File.read + JSON.parse က ပြန်ယူ
Round trip တစ်ခုချင်းစီမှာ JSON မှာ မရှိတဲ့ type တွေ (symbol, Time) က string ဖြစ်သွားနိုင်လို့ ပြန်ယူပြီးရင် စစ်ကြည့်ပါ။
အထွေထွေ ပုံစံ
require "json"
File.write(path, JSON.pretty_generate(hash)) # save
hash = JSON.parse(File.read(path), symbolize_names: true) # load
File.open(path, "a") { |file| file.puts line } # append
File.foreach(path) { |line| process(line.chomp) } # stream lines
Dir.mktmpdir { |dir| path = File.join(dir, "name.json") } # scratch spaceပထမနှစ်လိုင်းက Hash ကို သိမ်းပြီး ပြန်ယူတာပါ။ File.open ရဲ့ "a" က အဆုံးမှာ ဆက်ရေးပြီး File.foreach က လိုင်းတစ်ခုချင်း ဖတ်ပါတယ်။ နောက်ဆုံးလိုင်းက စမ်းသပ်ဖို့ ယာယီနေရာကို ပေးပါတယ်။
Example: settings ကို JSON နဲ့ သိမ်းပြီး ပြန်ဖတ်ခြင်း
ဒီ example က settings Hash ကို ယာယီ directory ထဲ JSON အဖြစ် သိမ်းပြီး ပြန်ဖတ်ကာ ပြန်ရတာ မူရင်းနဲ့ တူလားကို စစ်ပါတယ်။ ပြီးတာနဲ့ မရှိတဲ့ file နဲ့ ပျက်စီးနေတဲ့ file ကို တမင် စမ်းပြီး error နှစ်မျိုးကို rescue လုပ်ပြပါတယ်။ Path ကို ဘယ်တော့မှ print မထုတ်ထားပါဘူး၊ ဒါကြောင့် output က စက်တိုင်းမှာ တူပါတယ်။
require "json"
require "tmpdir"
settings = { theme: "dark", font_size: 14, tags: ["ruby", "json"], beta: nil }
Dir.mktmpdir("settings-demo") do |dir|
path = File.join(dir, "settings.json")
File.write(path, JSON.pretty_generate(settings))
puts File.read(path)
loaded = JSON.parse(File.read(path), symbolize_names: true)
puts loaded == settings
puts loaded[:font_size] + 1
begin
File.read(File.join(dir, "missing.json"))
rescue Errno::ENOENT => e
puts "missing file: #{e.class}"
end
File.write(path, '{"theme": "dark"')
begin
JSON.parse(File.read(path))
rescue JSON::ParserError
puts "file is not valid JSON"
end
endCode ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Scratch directory
Dir.mktmpdir("settings-demo") do |dir|ယာယီ directory ကို ဖန်တီးပြီး block ပြီးတာနဲ့ အားလုံးကို ဖျက်ပေးပါတယ်၊ ဒါကြောင့် ကျန်ခဲ့တဲ့ file မရှိပါဘူး။ ရလာတဲ့ path က စက်ပေါ်မူတည်တာမို့ print မထုတ်ပါနဲ့။
- Save
File.write(path, JSON.pretty_generate(settings))Hash ကို indent ပါတဲ့ JSON စာသားအဖြစ် ပြောင်းပြီး file ထဲ ရေးပါတယ်။ pretty_generate ကို သုံးတာက လူ ဖတ်လို့လွယ်ပြီး nil က null အဖြစ် ပေါ်တာကို output မှာ မြင်ရပါတယ်။
- Load
loaded = JSON.parse(File.read(path), symbolize_names: true)File ကို String အဖြစ်ဖတ်ပြီး JSON.parse က symbol key တွေပါတဲ့ Hash အဖြစ် ပြန်ပြောင်းပါတယ်။ symbolize_names မပါရင် loaded[:theme] က nil ဖြစ်မှာပါ၊ key တွေက "theme" လို string ဖြစ်နေလို့ပါ။
- The round trip
puts loaded == settingstrue ထွက်ပါတယ်၊ ဘာကြောင့်လဲဆိုတော့ settings ထဲက value တွေ (string, integer, array, nil) အားလုံးက JSON မှာ ရှိပါတယ်။ :theme က symbol value ဖြစ်ခဲ့ရင်တော့ false ဖြစ်မှာပါ။
- Missing file
rescue Errno::ENOENT => eမရှိတဲ့ file ကို ဖတ်ရင် Errno::ENOENT ပေါ်ပါတယ်။ e.message မှာ path ပါတာမို့ class နာမည်ကိုပဲ print ထုတ်ပါတယ်၊ log မှာ path တွေ မထုတ်ချင်ရင် ဒီနည်းက ကောင်းပါတယ်။
- Corrupted file
rescue JSON::ParserErrorFile ထဲမှာ {"theme": "dark" လို တစ်ဝက်ပဲ ရေးထားရင် parse မလုပ်နိုင်ပါဘူး။ ParserError ကို rescue လုပ်ပြီး ပျက်စီးနေတယ်လို့ ပြောလို့ program ကို crash မဖြစ်စေပါဘူး။
{
"theme": "dark",
"font_size": 14,
"tags": [
"ruby",
"json"
],
"beta": null
}
true
15
missing file: Errno::ENOENT
file is not valid JSONလိုင်းအလိုက် ရေးဖတ်ခြင်း၊ append နဲ့ "w" ရဲ့ အန္တရာယ်
Log လို အဆုံးမှာ ဆက်ထည့်တဲ့ file တွေအတွက် "a" mode ကို သုံးပါတယ်။ အောက်က code က file ကို "w" လိုပဲ File.write နဲ့ စတင်ပြီး (File.write က "w" နဲ့ ညီမျှပါတယ်) "a" နဲ့ puts, write, << သုံးမျိုးနဲ့ ဆက်ရေးပါတယ်။ File.foreach(path).with_index(1) က လိုင်းနံပါတ်နဲ့ တွဲပေးတယ်၊ chomp က အဆုံးက newline ကို ဖယ်ပါတယ်။ နောက်ဆုံးမှာ "w" နဲ့ ပြန်ဖွင့်လိုက်တာနဲ့ အရင် အကြောင်းအရာအားလုံး ပျောက်သွားပြီး "reset" ပဲ ကျန်ပါတယ်၊ ဒါက အဖြစ်များဆုံး ဒေတာဆုံးရှုံးမှုထဲက တစ်ခုပါ။
require "tmpdir"
Dir.mktmpdir("lines-demo") do |dir|
path = File.join(dir, "app.log")
File.write(path, "start\n") # "w": creates or overwrites
File.open(path, "a") do |file| # "a": append
file.puts "loaded config"
file.write("ready\n")
file << "stopped" << "\n"
end
File.foreach(path).with_index(1) do |line, number|
puts "#{number}: #{line.chomp}"
end
p File.readlines(path, chomp: true).length # 4
p File.basename(path), File.extname(path) # "app.log" ".log"
File.open(path, "w") { |file| file.print "reset" } # "w" truncates the file
p File.read(path) # "reset"
p File.exist?(path), File.exist?(File.join(dir, "nope.log")) # true false
endJSON မှာ ပြန်မရတဲ့ အရာတွေ
အောက်က code က Ruby hash တစ်ခုကို JSON ဆီပို့ပြီး ပြန်ယူကာ ဘာတွေ ပြောင်းသွားလဲ ပြပါတယ်။ :active symbol က "active" string ဖြစ်သွားပြီး 7 ဆိုတဲ့ key က "7" ဖြစ်သွားလို့ symbolize_names ပါတာတောင် p နဲ့ ထုတ်ရင် "7": ဆိုပြီး ပေါ်ပါတယ်။ Time က to_s စာသားဖြစ်သွားလို့ Time မဟုတ်တော့ဘဲ format ကလည်း Ruby ကိုယ်ပိုင် ပုံစံဖြစ်နေပါတယ်၊ ဒါကြောင့် iso8601 နဲ့ သိမ်းပြီး Time.iso8601 နဲ့ ပြန်ပြောင်းရပါတယ်။ မူလ hash နဲ့ ပြန်ရတဲ့ hash ကို နှိုင်းယှဉ်ရင် false ရတာကိုလည်း သတိပြုပါ။ ဒီထက် ပိုရှုပ်ထွေးတဲ့ value တွေ (symbol value, Time, set, ကိုယ်ပိုင် object) ကို သိမ်းချင်ရင် ထွက်ခွာတဲ့အခါ ကိုယ်ပိုင် to_h နဲ့ ပြောင်းပြီး ပြန်ဝင်လာတဲ့အခါ ပြန်တည်ဆောက်ပေးရပါမယ်။
require "json"
require "time"
moment = Time.utc(2024, 1, 2, 3, 4, 5)
original = { status: :active, count: 3, started: moment, 7 => "seven" }
text = JSON.generate(original)
puts text
# {"status":"active","count":3,"started":"2024-01-02 03:04:05 UTC","7":"seven"}
back = JSON.parse(text, symbolize_names: true)
p back
# {status: "active", count: 3, started: "2024-01-02 03:04:05 UTC", "7": "seven"}
p back[:status].class # String
p back == original # false
safe = JSON.generate({ started: moment.iso8601 })
puts safe # {"started":"2024-01-02T03:04:05Z"}
restored = Time.iso8601(JSON.parse(safe)["started"])
p restored == moment # true
p JSON.parse("[1, 2.5, null, true]") # [1, 2.5, nil, true]
p JSON.parse('{"a": 1}')[:a] # nil: the key is the String "a"
begin
JSON.generate(Float::NAN)
rescue JSON::GeneratorError
puts "NaN cannot be written as JSON"
endHash ရဲ့ p ပုံစံ (back လို output) က Ruby 3.4 နဲ့ အထက်ရဲ့ ပုံစံဖြစ်ပြီး ဒီ output ကို Ruby 4.0.9 နဲ့ ထုတ်ထားပါတယ်။ ဗားရှင်းအဟောင်းတွေမှာ {:status=>"active"} လို hash rocket ပုံစံနဲ့ ပေါ်ပါလိမ့်မယ်။ ဒါပေမယ့် Hash ထဲက တန်ဖိုးတွေကတော့ အတူတူပါပဲ။
Default တန်ဖိုးနဲ့ ဘေးကင်းတဲ့ loading
Config file တစ်ခုက မရှိနိုင်သလို ပျက်စီးနေနိုင်ပါတယ်၊ ဒါကြောင့် loading code က နှစ်မျိုးလုံးကို စီမံထားသင့်ပါတယ်။ အောက်က load_settings က file မရှိရင် (Errno::ENOENT) default တွေကို ပြန်ပေးပြီး ပျက်စီးနေရင် (JSON::ParserError) သတိပေးပြီး default တွေကို ပြန်ပေးပါတယ်။ file ရှိပြီး မှန်ရင်တော့ file ထဲက တန်ဖိုးတွေကို DEFAULTS ပေါ်မှာ merge လုပ်လို့ ပျောက်နေတဲ့ key တွေက default ကို ရပါတယ်။ ဒီလို rescue ကို class နှစ်ခုပဲ ရွေးထားတာကို သတိပြုပါ၊ တခြား error တွေ (permission စတာ) ကတော့ ဆက်ပြီး ကျယ်ကျယ်လောင်လောင် ပေါ်စေပါတယ်။
require "json"
require "tmpdir"
DEFAULTS = { theme: "light", font_size: 12 }.freeze
def load_settings(path)
DEFAULTS.merge(JSON.parse(File.read(path), symbolize_names: true))
rescue Errno::ENOENT
DEFAULTS.dup
rescue JSON::ParserError
puts "settings file is corrupt, using defaults"
DEFAULTS.dup
end
Dir.mktmpdir("settings") do |dir|
path = File.join(dir, "settings.json")
p load_settings(path) # {theme: "light", font_size: 12}
File.write(path, '{"theme": "dark"}')
p load_settings(path) # {theme: "dark", font_size: 12}
File.write(path, "{oops")
p load_settings(path) # prints the notice, then the defaults
endလက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
JSON က configuration file (settings.json လို)၊ API payload (Net::HTTP ကနေ ရတဲ့ response body ကို JSON.parse လုပ်တာ၊ Rails ရဲ့ render json:)၊ log တစ်လိုင်းချင်း JSON object တစ်ခုစီရေးတဲ့ JSON Lines လို အသုံးတွေမှာ ရှိပါတယ်။ Test တွေမှာတော့ Dir.mktmpdir နဲ့ Tempfile က file တွေကို ရှင်းလင်းစွာ ဖန်တီးပြီး ပြီးရင် ဖျက်ပေးပါတယ်၊ fixture တွေက JSON နဲ့ ရေးထားတတ်ပါတယ်။
ထုတ်လုပ်မှုနေရာမှာ စဉ်းစားစရာ အချို့ ရှိပါတယ်။ အရေးကြီးတဲ့ file ကို တစ်ဝက်တစ်ပျက် မဖြစ်စေဖို့ ယာယီ file ထဲ အရင်ရေးပြီး File.rename နဲ့ နေရာချ ပေးပါတယ် (atomic write)။ JSON ရဲ့ default encoding က UTF-8 ဖြစ်လို့ မသေချာရင် encoding: "UTF-8" ကို ထင်ရှားအောင် ပေးပါ။ ကိုယ်မထိန်းချုပ်နိုင်တဲ့ input အတွက် JSON.parse ကို ဦးစားပေးပါ၊ ဘာကြောင့်လဲဆိုတော့ ၎င်းက default အားဖြင့် ရိုးရိုး Ruby type တွေကိုပဲ တည်ဆောက်ပေးလို့ပါ၊ JSON.load ကို သုံးမယ်ဆိုရင် json gem ရဲ့ documentation ကို အရင်ဖတ်ပါ။ လျှို့ဝှက်ချက် (API key) တွေပါတဲ့ JSON file တွေကို version control ထဲ မထည့်ပါနဲ့။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. "a" ကို ရည်ရွယ်ပြီး "w" ကို သုံးမိတာ
File.open(path, "w") { } ကို block အလွတ်နဲ့တောင် ဖွင့်လိုက်တာနဲ့ file အကြောင်းအရာ ပျောက်ပါတယ်၊ ပြီးတော့ File.read က "" ကို ပြန်ပါတယ်။ ရှိပြီးသားထဲ ဆက်ထည့်ချင်ရင် "a" ကို သုံးပါ၊ အစားထိုးချင်တာ အမှန်တကယ်ဆိုရင်သာ "w" သုံးပါ။
2. symbolize_names မပါဘဲ symbol key နဲ့ ဖတ်တာ
JSON.parse('{"a": 1}')[:a] က nil ပါ၊ key က "a" String ဖြစ်လို့ပါ။ error မပေးဘဲ nil ပြန်တာမို့ နောက်မှ NoMethodError ဖြစ်ပြီး ရှာရခက်ပါတယ်။ ဖတ်တဲ့နေရာမှာ symbolize_names: true ထည့်ပါ၊ ဒါမှမဟုတ် data["a"] လို string key ကို တသမတ်တည်း သုံးပါ။
3. Symbol နဲ့ Time က မပျက်ဘဲ ပြန်ရမယ်လို့ ထင်တာ
{ status: :active } ကို သိမ်းပြီး ပြန်ဖတ်ရင် value က "active" ဖြစ်သွားလို့ == :active စစ်ချက်က false ပြန်ပါတယ်။ Time လည်း စာသား ဖြစ်သွားပါတယ်။ ပြန်ယူပြီးရင် ဘာ type လဲ စစ်ဖို့၊ ဒါမှမဟုတ် သိမ်းတဲ့အခါ iso8601 လို ရှင်းလင်းတဲ့ format ကို ကိုယ်တိုင် ရွေးဖို့ လိုပါတယ်။
4. File ကို block မပါဘဲ ဖွင့်ပြီး မပိတ်မိတာ
f = File.open(path) ပြီး f.close ကို မေ့ရင် handle ဖွင့်လျက်ကျန်ပြီး ရေးထားတာတွေ buffer ထဲမှာ မသိမ်းရသေးတာ ဖြစ်နိုင်ပါတယ်၊ ကြားထဲမှာ exception ပေါ်ရင် ပိတ်ဖို့ ကုဒ်ကို ဘယ်တော့မှ မရောက်ပါဘူး။ File.open(path) { |f| ... } ပုံစံနဲ့ ရေးရင် Ruby က ပိတ်ပေးတာမို့ ဒီအမှားကို လုံးဝ ရှောင်လို့ရပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- JSON စာသားကို ကိုယ်တိုင် ဆက်မရေးဘဲ JSON.generate ဒါမှမဟုတ် to_json ကို သုံးပါ။
- File ကို block ပုံစံ (File.open, File.write, File.read) နဲ့ ကိုင်ပြီး path ကို File.join နဲ့ တည်ဆောက်ပါ။
- ဖတ်တဲ့အခါ Errno::ENOENT နဲ့ JSON::ParserError ကို သီးသန့် rescue လုပ်ပြီး ဘာဖြစ်ကြောင်း ရှင်းရှင်း ပြောပါ။
- စမ်းသပ်မှုနဲ့ test တွေအတွက် Dir.mktmpdir ကို သုံးပြီး print/log ထဲမှာ absolute path မထုတ်ပါနဲ့။
Mini challenge
Mini challenge: contacts book
ဆက်သွယ်ရန်စာရင်းကို JSON file ထဲ သိမ်းပြီး ပြန်ဖတ်တဲ့ helper နှစ်ခု ရေးပါ။ File မရှိရင် စာရင်းအလွတ်ပြန်ရမယ်၊ file ပျက်နေရင် ရှင်းလင်းတဲ့ error ပေးရမယ်။
- load_contacts(path) က JSON.parse(..., symbolize_names: true) နဲ့ ဖတ်ပြီး file မရှိရင် [] ပြန်ရမယ်
- JSON မမှန်ရင် "contacts file is corrupt" ဆိုတဲ့ ArgumentError raise လုပ်ရမယ်
- save_contacts(path, contacts) က pretty JSON ကို file ထဲ ရေးရမယ်
- Dir.mktmpdir ထဲမှာ: အလွတ်ကနေစ၊ contact နှစ်ခု ထည့်ပြီး save လုပ်၊ ပြန် load လုပ်ကာ နာမည်တွေကို စီပြီး print ထုတ်၊ ပြီးရင် file ကို ရှုပ်ထွေးအောင် ပျက်အောင်ရေးပြီး error message ကို ပြပါ
require "json"
require "tmpdir"
def load_contacts(path)
# TODO: parse the file with symbol keys
# TODO: a missing file means an empty list
# TODO: invalid JSON becomes ArgumentError "contacts file is corrupt"
end
def save_contacts(path, contacts)
# TODO: write pretty JSON
end
# TODO: inside Dir.mktmpdir: load (empty), add two contacts, save, reload and print
# TODO: overwrite the file with broken JSON and show the error messageရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: 0 contacts at start 2 contacts after reload: Aye, Ko corrupt file: contacts file is corrupt
အရိပ်အမြွက် ကြည့်ရန်
Method တစ်ခုလုံးကို begin မလိုဘဲ rescue Errno::ENOENT နဲ့ rescue JSON::ParserError ကို method အဆုံးမှာ တိုက်ရိုက် ရေးလို့ရပါတယ်။ ပျက်စီးနေတဲ့ file ကို စမ်းဖို့ File.write(path, "[{") လို့ ရေးကြည့်ပါ။
အဖြေ ကြည့်ရန်
require "json"
require "tmpdir"
def load_contacts(path)
JSON.parse(File.read(path), symbolize_names: true)
rescue Errno::ENOENT
[]
rescue JSON::ParserError
raise ArgumentError, "contacts file is corrupt"
end
def save_contacts(path, contacts)
File.write(path, JSON.pretty_generate(contacts))
end
Dir.mktmpdir("contacts") do |dir|
path = File.join(dir, "contacts.json")
contacts = load_contacts(path)
puts "#{contacts.length} contacts at start"
contacts << { name: "Ko", phone: "0912" }
contacts << { name: "Aye", phone: "0933" }
save_contacts(path, contacts)
reloaded = load_contacts(path)
names = reloaded.map { |contact| contact[:name] }.sort
puts "#{reloaded.length} contacts after reload: #{names.join(", ")}"
File.write(path, "[{")
begin
load_contacts(path)
rescue ArgumentError => e
puts "corrupt file: #{e.message}"
end
endQuick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- File ကို File.write, File.read, File.foreach နဲ့ block ပုံစံ File.open တို့နဲ့ ကိုင်ပါ၊ "w" က ဖွင့်ဖွင့်ချင်း ဖြတ်ပစ်ပြီး "a" က ဆက်ရေးပါတယ်။
- Dir.mktmpdir က ယာယီ directory ကို ဖန်တီးပြီး block ပြီးရင် ဖျက်ပေးလို့ စမ်းသပ်မှုတွေကို ရှင်းလင်းစေပါတယ်၊ path ကို print မထုတ်ပါနဲ့။
- JSON.generate / pretty_generate က Hash ကို စာသားလုပ်ပြီး JSON.parse က ပြန်ယူပါတယ်၊ symbol key လိုချင်ရင် symbolize_names: true ထည့်ပါ။
- JSON က symbol နဲ့ Time ကို ပြန်မပေးနိုင်ပါဘူး၊ Errno::ENOENT နဲ့ JSON::ParserError ကို သီးသန့် rescue လုပ်ပါ။
Official Ruby Documentation — Ruby Documentation