在名称中转义“@”
Escape '@' in names
我使用 Rails 4.2.0 和 ActiveResource 来实现服务器 API 的网络客户端。
我遇到的问题是服务器资源在属性名称中包含“@”和“@@”。因此在创建 ARes 对象时,ActiveResource 无法正确处理这些属性。
示例:服务器 returns JSON 数据:
{"@@artist"=>
{"@id"=>"some_text_id",
"@name"=>"Carman",
"@publisher"=>"Carmen radio",
"@category"=>"Music",
"@automaticallyGenerated"=>true,
"@@options"=>{"@public"=>true, "@enabled"=>false},
"_extended"=>{"@container"=>"Music box", "region"=>"Europe"}}}
在创建 ARes 对象之前重命名属性的变体:(从名称中删除“@”或将其替换为另一个符号)。在那种情况下,我需要在发送 POST
、PUT
请求之前返回重命名属性(例如调用 resource.save
),例如在名称的开头添加“@”或“@@”。
你能推荐更灵活、更漂亮的变体吗?
我真的不知道你的输出是什么,但你可以使用类似这样的东西。
这只是示例。
input = {
"@@artist"=>
{"@id"=>"some_text_id",
"@name"=>"Carman",
"@publisher"=>"Carmen radio",
"@category"=>"Music",
"@automaticallyGenerated"=>true,
"@@options"=>{"@public"=>true, "@enabled"=>false},
"_extended"=>{"@container"=>"Music box", "region"=>"Europe"}
}
}
def transcode(hash)
result = {}
hash.each do |k, v|
result[k.gsub(/@*/, "")] = ( Hash === v ? transcode(v) : v )
end
result
end
output = transcode(input)
#{
# "artist" => {
# "id" => "some_text_id",
# "name" => "Carman",
# "publisher" => "Carmen radio",
# "category" => "Music",
# "automaticallyGenerated" => true,
# "options" => {
# "public" => true,
# "enabled" => false
# },
# "_extended" => {
# "container" => "Music box",
# "region" => "Europe"
# }
# }
#}
返回
input = {
"@@artist"=>
{"@id"=>"some_text_id",
"@name"=>"Carman",
"@publisher"=>"Carmen radio",
"@category"=>"Music",
"@automaticallyGenerated"=>true,
"@@options"=>{"@public"=>true, "@enabled"=>false},
"_extended"=>{"@container"=>"Music box", "region"=>"Europe"}
}
}
def transcode(hash)
result = {}
hash.each do |k, v|
result[k.gsub(/@*/, "")] = ( Hash === v ? transcode(v) : v )
end
result
end
def transcode_back(hash, output)
result = {}
hash.each do |k, v|
value = output[k.gsub(/@*/, "")]
result[k] = ( Hash === value ? transcode_back(v, value) : value )
end
result
end
output = transcode(input)
# you can modify values
#output["artist"]["name"] = "CarmanNew"
result = transcode_back(input, output)
output == result # is same ?
# true
我使用 Rails 4.2.0 和 ActiveResource 来实现服务器 API 的网络客户端。 我遇到的问题是服务器资源在属性名称中包含“@”和“@@”。因此在创建 ARes 对象时,ActiveResource 无法正确处理这些属性。 示例:服务器 returns JSON 数据:
{"@@artist"=>
{"@id"=>"some_text_id",
"@name"=>"Carman",
"@publisher"=>"Carmen radio",
"@category"=>"Music",
"@automaticallyGenerated"=>true,
"@@options"=>{"@public"=>true, "@enabled"=>false},
"_extended"=>{"@container"=>"Music box", "region"=>"Europe"}}}
在创建 ARes 对象之前重命名属性的变体:(从名称中删除“@”或将其替换为另一个符号)。在那种情况下,我需要在发送 POST
、PUT
请求之前返回重命名属性(例如调用 resource.save
),例如在名称的开头添加“@”或“@@”。
你能推荐更灵活、更漂亮的变体吗?
我真的不知道你的输出是什么,但你可以使用类似这样的东西。 这只是示例。
input = {
"@@artist"=>
{"@id"=>"some_text_id",
"@name"=>"Carman",
"@publisher"=>"Carmen radio",
"@category"=>"Music",
"@automaticallyGenerated"=>true,
"@@options"=>{"@public"=>true, "@enabled"=>false},
"_extended"=>{"@container"=>"Music box", "region"=>"Europe"}
}
}
def transcode(hash)
result = {}
hash.each do |k, v|
result[k.gsub(/@*/, "")] = ( Hash === v ? transcode(v) : v )
end
result
end
output = transcode(input)
#{
# "artist" => {
# "id" => "some_text_id",
# "name" => "Carman",
# "publisher" => "Carmen radio",
# "category" => "Music",
# "automaticallyGenerated" => true,
# "options" => {
# "public" => true,
# "enabled" => false
# },
# "_extended" => {
# "container" => "Music box",
# "region" => "Europe"
# }
# }
#}
返回
input = {
"@@artist"=>
{"@id"=>"some_text_id",
"@name"=>"Carman",
"@publisher"=>"Carmen radio",
"@category"=>"Music",
"@automaticallyGenerated"=>true,
"@@options"=>{"@public"=>true, "@enabled"=>false},
"_extended"=>{"@container"=>"Music box", "region"=>"Europe"}
}
}
def transcode(hash)
result = {}
hash.each do |k, v|
result[k.gsub(/@*/, "")] = ( Hash === v ? transcode(v) : v )
end
result
end
def transcode_back(hash, output)
result = {}
hash.each do |k, v|
value = output[k.gsub(/@*/, "")]
result[k] = ( Hash === value ? transcode_back(v, value) : value )
end
result
end
output = transcode(input)
# you can modify values
#output["artist"]["name"] = "CarmanNew"
result = transcode_back(input, output)
output == result # is same ?
# true