简体   繁体   中英

Generate ruby classes from json document

Consuming a ruby json API, I want to save me some work and generate ruby objects off the bat. Any way to do this?

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

If you're looking to turn a JSON string into a Ruby Hash you can do

my_hash = JSON.parse('{"foo":"bar"}')
puts my_hash['foo']

There is a wonderful gem for doing this. 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.

If you want "methodized" hashes which use accessor methods (not a bad idea at all!)

 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.

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.

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).

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.

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.

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).

Note that what I wrote above is not strictly true: there are specifications for JSON schemas (eg JSON-Schema ) and there are Ruby implementations of those (eg Ruby/JSONSchema , validation only, doesn't generate classes), but nobody is using them, so they might just as well not exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM