moran 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +90 -0
- data/lib/moran.rb +19 -0
- data/motion/moran/ext/hash.rb +11 -0
- data/motion/moran/java_to_ruby.rb +85 -0
- data/motion/moran/moran.rb +20 -0
- data/motion/moran/ruby_to_java.rb +30 -0
- data/motion/moran/version.rb +4 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cd22b6c7bccaf84ff993dff93e21a1b35a20df32
|
4
|
+
data.tar.gz: a958ab3d4f5a2ba69b70ab9a334d83d2fdf5c5b6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e52219e5738a76446903561926a3933b6f20b848e21f9bc1f043d8e513fec17a56f9cb7c521a69a502181db8d312a603d2a8ceff5b4e6a486b69d5fa30b3730f
|
7
|
+
data.tar.gz: c1936ff87402385cd8089aa7bbde49e71030ab76f06cf897b513511c708c22f6a8b981c7fdc3c8b83c726b11d7b7cb05aa1853f0edcd9006fa1362e76de333dc
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Silvertop Studio, LLC
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# moran
|
2
|
+
|
3
|
+
moran is a simple JSON parser and generator for [RubyMotion Android](http://rubymotion.com). It provides a Ruby wrapper around the [Jackson JSON parser for Java.](https://github.com/FasterXML/jackson)
|
4
|
+
|
5
|
+
moran emulates the standard Ruby JSON API, providing `parse` and `generate` methods:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
hash = Moran.parse(json_string)
|
9
|
+
json_string = Moran.generate(hash)
|
10
|
+
```
|
11
|
+
|
12
|
+
It also adds convenience methods to `Hash` for generating JSON, and converting `org.json.JSONObject` instances into Hashes, which is handy for integrating with the Java libraries that use them:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
json_string = hash.to_json
|
16
|
+
hash = Hash.from_json_object(some_json_object)
|
17
|
+
```
|
18
|
+
|
19
|
+
## Setup
|
20
|
+
|
21
|
+
### Prerequisite
|
22
|
+
|
23
|
+
This gem uses [motion-gradle](https://github.com/HipByte/motion-gradle) to manage the Java dependencies, so you need to have Gradle installed. For full details, see the [motion-gradle README](https://github.com/HipByte/motion-gradle), but the basics are:
|
24
|
+
|
25
|
+
```shell
|
26
|
+
brew install gradle
|
27
|
+
```
|
28
|
+
|
29
|
+
then install `Extras/Android Support Repository` with the Android SDK Manager.
|
30
|
+
|
31
|
+
### Project Setup
|
32
|
+
|
33
|
+
Gemfile:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
gem "moran"
|
37
|
+
```
|
38
|
+
|
39
|
+
Install Gradle dependencies:
|
40
|
+
|
41
|
+
```shell
|
42
|
+
rake gradle:install
|
43
|
+
```
|
44
|
+
|
45
|
+
## Usage
|
46
|
+
|
47
|
+
### Parsing
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
json = '{ my_dog: "has_fleas" }'
|
51
|
+
hash = Moran.parse(json)
|
52
|
+
hash["my_dog"] => "has_fleas"
|
53
|
+
```
|
54
|
+
|
55
|
+
### Generating
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
hash = { dog: { name: "Rex", fleas: true } }
|
59
|
+
json = Moran.generate(hash)
|
60
|
+
# or...
|
61
|
+
json = hash.to_json
|
62
|
+
```
|
63
|
+
|
64
|
+
### Converting org.json.JSONObject instances
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
json_obj = Org::Json::JSONObject.new
|
68
|
+
json_obj.put("string", "dog")
|
69
|
+
json_obj.put("boolean", true)
|
70
|
+
|
71
|
+
hash = Hash.from_json_object(json_obj)
|
72
|
+
hash["string"] => "dog"
|
73
|
+
hash["boolean"] => true
|
74
|
+
```
|
75
|
+
|
76
|
+
## Development
|
77
|
+
|
78
|
+
### Tests
|
79
|
+
|
80
|
+
There is a small suite of tests that covers the basics, and can be run the usual way:
|
81
|
+
|
82
|
+
```
|
83
|
+
rake spec
|
84
|
+
```
|
85
|
+
or
|
86
|
+
```
|
87
|
+
rake spec:device
|
88
|
+
```
|
89
|
+
|
90
|
+
|
data/lib/moran.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
unless defined?(Motion::Project::App)
|
2
|
+
raise "This must be required from within a RubyMotion Rakefile"
|
3
|
+
end
|
4
|
+
|
5
|
+
Motion::Project::App.setup do |app|
|
6
|
+
require "motion-gradle"
|
7
|
+
|
8
|
+
parent = File.join(File.dirname(__FILE__), '..')
|
9
|
+
files = [File.join(parent, 'motion/moran/version.rb')]
|
10
|
+
files << Dir.glob(File.join(parent, "motion/**/*.rb"))
|
11
|
+
files.flatten!.uniq!
|
12
|
+
app.files.unshift files
|
13
|
+
|
14
|
+
app.gradle do
|
15
|
+
dependency "com.fasterxml.jackson.core", artifact: "jackson-core"
|
16
|
+
dependency "com.fasterxml.jackson.core", artifact: "jackson-annotations"
|
17
|
+
dependency "com.fasterxml.jackson.core", artifact: "jackson-databind"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# Utility class that converts a java.util.HashMap instance to a Ruby Hash. RubyMotion will
|
2
|
+
# start supporting this out of the box at some point, but until then, this can be used
|
3
|
+
# as a workaround: http://hipbyte.myjetbrains.com/youtrack/issue/RM-725
|
4
|
+
#
|
5
|
+
# Usage:
|
6
|
+
# JavaToRuby.convert_hashmap(hashmap)
|
7
|
+
#
|
8
|
+
# This supports nested hashes and should assure that all Java-based values are properly converted
|
9
|
+
# to their Ruby counterparts (i.e. java.lang.String => String).
|
10
|
+
#
|
11
|
+
# This has not been extensively tested, or optimized for performance. It should work well enough
|
12
|
+
# for simple cases, but may well fall over with a large HashMap.
|
13
|
+
#
|
14
|
+
class JavaToRuby
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def convert_hashmap(hashmap)
|
18
|
+
JavaToRuby.new.hashmap_to_hash(hashmap)
|
19
|
+
end
|
20
|
+
|
21
|
+
def convert_json_object(json_obj)
|
22
|
+
JavaToRuby.new.json_object_to_hash(json_obj)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def hashmap_to_hash(hm)
|
27
|
+
return nil if hm.nil?
|
28
|
+
Hash.new.tap do |h|
|
29
|
+
it = hm.entrySet().iterator();
|
30
|
+
while it.hasNext() do
|
31
|
+
entry = it.next()
|
32
|
+
h[entry.getKey()] = convert_value(entry.getValue())
|
33
|
+
it.remove()
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def json_object_to_hash(json_obj)
|
39
|
+
return nil if json_obj.nil?
|
40
|
+
Hash.new.tap do |h|
|
41
|
+
it = json_obj.keys()
|
42
|
+
while it.hasNext() do
|
43
|
+
key = it.next()
|
44
|
+
h[key] = convert_value(json_obj.get(key))
|
45
|
+
it.remove()
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
private
|
50
|
+
|
51
|
+
def convert_value(value)
|
52
|
+
new_value ||= hashmap_to_hash(value) if hashmap?(value)
|
53
|
+
new_value ||= to_array(value) if array?(value)
|
54
|
+
new_value ||= to_boolean(value) if boolean?(value)
|
55
|
+
new_value ||= nil if null?(value)
|
56
|
+
new_value ||= value
|
57
|
+
end
|
58
|
+
|
59
|
+
def hashmap?(value)
|
60
|
+
value.class.to_s.end_with?("HashMap")
|
61
|
+
end
|
62
|
+
|
63
|
+
def array?(value)
|
64
|
+
value.is_a?(Array)
|
65
|
+
end
|
66
|
+
|
67
|
+
def boolean?(value)
|
68
|
+
["true","false"].include?(value.to_s.downcase)
|
69
|
+
end
|
70
|
+
|
71
|
+
def null?(value)
|
72
|
+
value.nil? || value.to_s.downcase == "null"
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_array(array)
|
76
|
+
# currently, Java arrays are correctly converted to Array objects - we just need to make
|
77
|
+
# sure that the values are correctly converted
|
78
|
+
array.map { |value| convert_value(value) }
|
79
|
+
end
|
80
|
+
|
81
|
+
def to_boolean(value)
|
82
|
+
value.to_s.downcase == "true"
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Moran
|
2
|
+
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def parse(json)
|
6
|
+
mapper = Com::Fasterxml::Jackson::Databind::ObjectMapper.new
|
7
|
+
mapper.configure(Com::Fasterxml::Jackson::Core::JsonParser::Feature::ALLOW_UNQUOTED_FIELD_NAMES,
|
8
|
+
true)
|
9
|
+
java_hash = mapper.readValue(json, Hash)
|
10
|
+
# we still don't have a completely smooth conversion of types between JavaLand and RubyLand,
|
11
|
+
# so this next step seems to be necessary
|
12
|
+
JavaToRuby.convert_hashmap(java_hash)
|
13
|
+
end
|
14
|
+
|
15
|
+
def generate(hash)
|
16
|
+
mapper = Com::Fasterxml::Jackson::Databind::ObjectMapper.new
|
17
|
+
mapper.writeValueAsString(RubyToJava.convert_hash(hash));
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Type conversion between Ruby and Java is still not as seamless as one would like. Strings
|
2
|
+
# in particular get mangled when passed into Jackson
|
3
|
+
class RubyToJava
|
4
|
+
|
5
|
+
def self.convert_hash(hash)
|
6
|
+
RubyToJava.new.to_hashmap(hash)
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_hashmap(old_hash)
|
10
|
+
Hash.new.tap do |h|
|
11
|
+
old_hash.keys.each do |key|
|
12
|
+
h[key] = convert_value(old_hash[key])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def convert_value(value)
|
20
|
+
new_value ||= to_hashmap(value) if value.is_a?(Hash)
|
21
|
+
new_value ||= convert_array(value) if value.is_a?(Array)
|
22
|
+
new_value ||= value.toString if value.is_a?(String)
|
23
|
+
new_value ||= value
|
24
|
+
end
|
25
|
+
|
26
|
+
def convert_array(array)
|
27
|
+
array.map { |v| v.is_a?(String) ? v.toString : v }
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moran
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Darin Wilson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: motion-gradle
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bacon
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Moran is a simple JSON parser/outputter for RubyMotion Android
|
56
|
+
email: darinwilson@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- LICENSE
|
62
|
+
- README.md
|
63
|
+
- lib/moran.rb
|
64
|
+
- motion/moran/ext/hash.rb
|
65
|
+
- motion/moran/java_to_ruby.rb
|
66
|
+
- motion/moran/moran.rb
|
67
|
+
- motion/moran/ruby_to_java.rb
|
68
|
+
- motion/moran/version.rb
|
69
|
+
homepage: http://github.com/darinwilson/moran
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 2.2.2
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: Simple JSON parsing and generation for RubyMotion Android
|
93
|
+
test_files: []
|