views:

68

answers:

3

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
+1  A: 

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']
aNoble
I believe he is trying to generate a class from the json as opposed to paring it?
steve
I think you're right. I read "object" in the question before the link to RXSD was posted. So I didn't quite understand what he was looking for.
aNoble
+1  A: 

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.

Julik
+1  A: 

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

Jörg W Mittag
It doesn't have to be a schema to do code generation on it...
scottschulthess