Ruby
Decoding a json object while converting keys to symbols
1
2
3
4
5
6
7
8
9
10
11
12
def schema
raw_schema_json = ActiveSupport::JSON.decode(self.schema_json)
schema = []
raw_schema_json.each do |raw_item|
item = {}
raw_item.each do |key, value|
item[key.to_sym] = value
end
schema.push(item)
end
schema
end
def schema
raw_schema_json = ActiveSupport::JSON.decode(self.schema_json)
schema = []
raw_schema_json.each do |raw_item|
item = {}
raw_item.each do |key, value|
item[key.to_sym] = value
end
schema.push(item)
end
schema
end
Refactorings
No refactoring yet !
June 10, 2009,
June 10, 2009 18:16,
permalink
2 ratings.
Login to rate!
1
2
3
4
5
6
7
def schema(json = ActiveSupport::JSON.decode(schema_json))
case json
when Array; json.map { |element| schema(element) }
when Hash; HashWithIndifferentAccess.new(json)
else; json
end
end
D Clapp
June 11, 2009,
June 11, 2009 17:53,
permalink
No rating.
Login to rate!
June 22, 2009,
June 22, 2009 19:01,
permalink
No rating.
Login to rate!
I'm decoding a json object. The first line would be sufficient - but then I can only access the items with item["foo"]. I appreciate item[:foo]. Is there a more elegant way to do that?