Consuming a ruby json API, I want to save me some work and generate ruby objects off the bat. Any way to do this?
使用ruby json API,我希望节省一些工作并立即生成ruby对象。有什么办法吗?
so you could transform this:
所以你可以对它进行变换
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
to this:
:
class Menu
attr_accessor :id
attr_accessor :file
attr_accessor :popup
end
4 个解决方案
#1
2
There is a wonderful gem for doing this. https://github.com/apotonick/representable/
这是一个很好的例子。https://github.com/apotonick/representable/
Here's what your representable would look like
这是你的可被表征物的样子
module MenuRepresenter
include Representable::JSON
property :id
property :value
property :popup
end
Create your model
创建你的模型
class Menu
attr_accessor :id, :value, :popup
end
menu = Menu.new.extend(MenuRepresenter).from_json(json)
# You can convert it back into json via .to_json
# expect(menu.to_json).to eq(json)
The example above shows only the basic implementation, you would want to create another class for the menu item, take a look at the documentation at the github repo for more detailed information.
上面的示例只显示了基本实现,您可能希望为菜单项创建另一个类,请查看github repo上的文档以获得更详细的信息。
#2
3
If you're looking to turn a JSON string into a Ruby Hash you can do
如果希望将JSON字符串转换为Ruby散列,可以这样做
my_hash = JSON.parse('{"foo":"bar"}')
puts my_hash['foo']
#3
1
If you want "methodized" hashes which use accessor methods (not a bad idea at all!)
如果你想要“methodized”的散列,使用accessor方法(完全不是一个坏主意!)
require "ostruct"
object_like_hash = OpenStruct.new(some_hash_with_string_or_symbol_keys)
object_like_hash.foo #=> same as some_hash["foo"]
Nonexistent keys will return nil and not raise unfortunately.
不存在的键将返回nil,不幸的是不会引发。
#4
1
I think you are a little bit confused. In the question, you ask how to turn a JSON document into classes. In the comments, you say you want a JSON version of the RXSD XML tool, which however, turns XML schemas into Ruby classes.
我觉得你有点困惑。在这个问题中,您将询问如何将JSON文档转换为类。在评论中,您说您想要RXSD XML工具的JSON版本,但是它将XML模式转换为Ruby类。
Turning JSON documents into classes doesn't really make sense. If you compare the world of document markup to programming, documents correspond to objects and schemas correspond to classes (well, types, actually, but since we're talking about Ruby, let's not open that can of worms and stick with classes).
将JSON文档转换为类并没有什么意义。如果将文档标记的世界与编程相比较,文档对应的对象和模式对应于类(实际上,类型实际上是这样的,但既然我们讨论的是Ruby,那么我们就不要打开这种蠕虫,并继续使用类)。
So, it makes sense to generate Ruby objects from JSON documents and it makes sense to generate Ruby classes from JSON schemas, but it doesn't make sense to generate Ruby classes from JSON documents. The bad news is of course that in order to be able to automatically generate Ruby classes from JSON schema is that in order for that to work, the JSON schema has to be in an automatically processable (IOW machine-readable) format.
因此,从JSON文档生成Ruby对象是有意义的,从JSON模式生成Ruby类也是有意义的,但是从JSON文档生成Ruby类是没有意义的。当然,坏消息是,为了能够从JSON模式自动生成Ruby类,JSON模式必须采用自动可处理(IOW机器可读)格式。
Unfortunately, there is no such thing as a JSON schema, and thus JSON schemas tend to generally not be machine-readable, but rather are just a blurb of human-oriented English text on the API documentation page of the web service provider. If you're lucky. More often than not, there is no documentation at all about the JSON schema.
不幸的是,没有JSON模式这种东西,因此JSON模式通常不是机器可读的,而是web服务提供者的API文档页面上面向人的英语文本的简介。如果你是幸运的。通常,根本没有关于JSON模式的文档。
So, since there is no standardized way of describing JSON schemas, there cannot be a standardized tool for processing JSON schemas. Unlike XML, where there is a limited number of standardized schemas (DTD, XSD, RelaxNG).
因此,由于没有标准化的方式来描述JSON模式,因此不能使用标准化的工具来处理JSON模式。与XML不同,XML中只有有限数量的标准化模式(DTD、XSD、弛豫)。
Note that what I wrote above is not strictly true: there are specifications for JSON schemas (e.g. JSON-Schema) and there are Ruby implementations of those (e.g. Ruby/JSONSchema, validation only, doesn't generate classes), but nobody is using them, so they might just as well not exist.
请注意,我上面所写的不是严格正确的:有JSON模式的规范(例如JSON- schema),也有它们的Ruby实现(例如Ruby/JSONSchema,只进行验证,不生成类),但是没有人在使用它们,所以它们也可能不存在。
#1
2
There is a wonderful gem for doing this. https://github.com/apotonick/representable/
这是一个很好的例子。https://github.com/apotonick/representable/
Here's what your representable would look like
这是你的可被表征物的样子
module MenuRepresenter
include Representable::JSON
property :id
property :value
property :popup
end
Create your model
创建你的模型
class Menu
attr_accessor :id, :value, :popup
end
menu = Menu.new.extend(MenuRepresenter).from_json(json)
# You can convert it back into json via .to_json
# expect(menu.to_json).to eq(json)
The example above shows only the basic implementation, you would want to create another class for the menu item, take a look at the documentation at the github repo for more detailed information.
上面的示例只显示了基本实现,您可能希望为菜单项创建另一个类,请查看github repo上的文档以获得更详细的信息。
#2
3
If you're looking to turn a JSON string into a Ruby Hash you can do
如果希望将JSON字符串转换为Ruby散列,可以这样做
my_hash = JSON.parse('{"foo":"bar"}')
puts my_hash['foo']
#3
1
If you want "methodized" hashes which use accessor methods (not a bad idea at all!)
如果你想要“methodized”的散列,使用accessor方法(完全不是一个坏主意!)
require "ostruct"
object_like_hash = OpenStruct.new(some_hash_with_string_or_symbol_keys)
object_like_hash.foo #=> same as some_hash["foo"]
Nonexistent keys will return nil and not raise unfortunately.
不存在的键将返回nil,不幸的是不会引发。
#4
1
I think you are a little bit confused. In the question, you ask how to turn a JSON document into classes. In the comments, you say you want a JSON version of the RXSD XML tool, which however, turns XML schemas into Ruby classes.
我觉得你有点困惑。在这个问题中,您将询问如何将JSON文档转换为类。在评论中,您说您想要RXSD XML工具的JSON版本,但是它将XML模式转换为Ruby类。
Turning JSON documents into classes doesn't really make sense. If you compare the world of document markup to programming, documents correspond to objects and schemas correspond to classes (well, types, actually, but since we're talking about Ruby, let's not open that can of worms and stick with classes).
将JSON文档转换为类并没有什么意义。如果将文档标记的世界与编程相比较,文档对应的对象和模式对应于类(实际上,类型实际上是这样的,但既然我们讨论的是Ruby,那么我们就不要打开这种蠕虫,并继续使用类)。
So, it makes sense to generate Ruby objects from JSON documents and it makes sense to generate Ruby classes from JSON schemas, but it doesn't make sense to generate Ruby classes from JSON documents. The bad news is of course that in order to be able to automatically generate Ruby classes from JSON schema is that in order for that to work, the JSON schema has to be in an automatically processable (IOW machine-readable) format.
因此,从JSON文档生成Ruby对象是有意义的,从JSON模式生成Ruby类也是有意义的,但是从JSON文档生成Ruby类是没有意义的。当然,坏消息是,为了能够从JSON模式自动生成Ruby类,JSON模式必须采用自动可处理(IOW机器可读)格式。
Unfortunately, there is no such thing as a JSON schema, and thus JSON schemas tend to generally not be machine-readable, but rather are just a blurb of human-oriented English text on the API documentation page of the web service provider. If you're lucky. More often than not, there is no documentation at all about the JSON schema.
不幸的是,没有JSON模式这种东西,因此JSON模式通常不是机器可读的,而是web服务提供者的API文档页面上面向人的英语文本的简介。如果你是幸运的。通常,根本没有关于JSON模式的文档。
So, since there is no standardized way of describing JSON schemas, there cannot be a standardized tool for processing JSON schemas. Unlike XML, where there is a limited number of standardized schemas (DTD, XSD, RelaxNG).
因此,由于没有标准化的方式来描述JSON模式,因此不能使用标准化的工具来处理JSON模式。与XML不同,XML中只有有限数量的标准化模式(DTD、XSD、弛豫)。
Note that what I wrote above is not strictly true: there are specifications for JSON schemas (e.g. JSON-Schema) and there are Ruby implementations of those (e.g. Ruby/JSONSchema, validation only, doesn't generate classes), but nobody is using them, so they might just as well not exist.
请注意,我上面所写的不是严格正确的:有JSON模式的规范(例如JSON- schema),也有它们的Ruby实现(例如Ruby/JSONSchema,只进行验证,不生成类),但是没有人在使用它们,所以它们也可能不存在。