Moran - a JSON parser for RubyMotion Android


jazz pianist and composer Jason Moran

I’m very excited to announce the first release of moran, a simple JSON parser/generator for RubyMotion for Android.

Moran is a Ruby wrapper for the excellent Jackon JSON parser for Java, which has been a favorite in the Java community for some time. With moran, we get the power of the Jackson library, with an API that will feel familiar to anyone who’s used the standard Ruby JSON library:

json = '{ foo: "bar" }'
hash = Moran.parse(json)
hash["foo"] #=> "bar"

json = Moran.generate({ foo: "bar" })
# or
json = { foo: "bar" }.to_hash

Moran also provides a convenience method for converting org.json.JSONObject instances into Ruby hashes. JSONObject shows up a lot in the Android API, and it’s a bit of a curiosity. Its API is very similar to HashMap and its ilk, but it’s not a subclass of HashMap, nor does it implement the Map interface, so it’s not automatically converted into a Ruby hash like HashMap is. If you work with a Java library that uses it, you end up having to make lots of get and put calls, which doesn’t feel very Ruby-like.

With moran, you can easily turn a JSONObject instance into a Hash, and use the style we know and love:

json_obj = Org::Json::JSONObject.new
json_obj.put("foo", "bar")
json_obj.put("baz", true)
hash = Hash.from_json_object(some_json_object)
hash["baz"] #=> true

For more details on setup and usage, see the main project page, and please let me know if you have any suggestions for improvements. Enjoy!

comments powered by Disqus