kilza 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b05924a821f14b41a1f7984647a84d8999dbea02
4
+ data.tar.gz: 9b90b35ca83ea74bca7a9efe896768e498cb903d
5
+ SHA512:
6
+ metadata.gz: c5eb842506a49a80ed0f05646c9e389856fccf84dd308868ab46ab72c0e147b57d7bba0d581e5c530029a857b2c05e550f6139cdb0f2d0d60e2ccda7ed679912
7
+ data.tar.gz: c3318727a6ce67804d09a8f1b27f1fb6a75c3d73a6780d6d9f5207f944e0feabb28a0026e90520b46fa83de22924d05dabcceba20b14fb98ef799bc14cb971f2
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kilza.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Toshiro Sugii
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.
@@ -0,0 +1,172 @@
1
+ # Kilza
2
+
3
+ Ruby gem that can convert JSON strings in Objects.
4
+
5
+ It supports Objective-C and Java classes. Contribuition would be appreciate.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kilza'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kilza
22
+
23
+ ## Binary
24
+
25
+ Just call:
26
+
27
+ kilza
28
+
29
+ And the Kilza will guide you.
30
+
31
+ ## Usage in code
32
+
33
+ Just
34
+
35
+ require 'kilza'
36
+
37
+ And then
38
+
39
+ ```
40
+ json_string = "..."
41
+
42
+ java = Kilza::Java.new(json_string)
43
+ java.classes("MyBaseClass").each { |c|
44
+ c.sources.each{ |s|
45
+ File.write(File.join("my/target/path", s.file_name), s.source)
46
+ }
47
+ }
48
+ ```
49
+
50
+ ## Example
51
+
52
+ Let's see in action.
53
+ Suppose our variable **json_string** have the following string:
54
+
55
+ ```
56
+ {
57
+
58
+ "Code": ​110,
59
+ "Message": ""
60
+
61
+ }
62
+ ```
63
+
64
+ Calling the code above:
65
+
66
+ ```
67
+ java = Kilza::Java.new(json_string)
68
+ java.classes("MyBaseClass").each { |c|
69
+ c.sources.each{ |s|
70
+ File.write(File.join("my/target/path", s.file_name), s.source)
71
+ }
72
+ }
73
+ ```
74
+
75
+ Will generate:
76
+
77
+ ```
78
+ package ;
79
+
80
+ import org.json.*;
81
+ import java.io.Serializable;
82
+
83
+ import com.google.gson.Gson;
84
+ import com.google.gson.GsonBuilder;
85
+ import com.google.gson.annotations.SerializedName;
86
+ import com.google.gson.annotations.Expose;
87
+
88
+ public class Mybaseclass implements Serializable
89
+ {
90
+ private static final String FIELD_CODE = "Code";
91
+ private static final String FIELD_MESSAGE = "Message";
92
+
93
+ @Expose
94
+ @SerializedName(FIELD_CODE)
95
+ private Long code;
96
+ @Expose
97
+ @SerializedName(FIELD_MESSAGE)
98
+ private String message;
99
+
100
+ public Mybaseclass() {
101
+
102
+ }
103
+
104
+ public Mybaseclass(JSONObject jsonObject) {
105
+ parseObject(jsonObject);
106
+ }
107
+
108
+ public Mybaseclass(String jsonString) {
109
+ try {
110
+ parseString(jsonString);
111
+ } catch (JSONException e) {
112
+ e.printStackTrace();
113
+ }
114
+ }
115
+
116
+ protected void parseString(String jsonString) throws JSONException {
117
+ JSONObject jsonObject = new JSONObject(jsonString);
118
+ parseObject(jsonObject);
119
+ }
120
+
121
+ protected void parseObject(JSONObject object)
122
+ {
123
+ this.code = object.optLong(FIELD_CODE);
124
+ this.message = object.optString(FIELD_MESSAGE);
125
+ }
126
+
127
+ public void setCode(Long value) {
128
+ this.code = value;
129
+ }
130
+
131
+ public Long getCode() {
132
+ return this.code;
133
+ }
134
+
135
+ public void setMessage(String value) {
136
+ this.message = value;
137
+ }
138
+
139
+ public String getMessage() {
140
+ return this.message;
141
+ }
142
+
143
+ @Override
144
+ public boolean equals(Object obj) {
145
+ if (obj instanceof Mybaseclass) {
146
+ return ((Mybaseclass) obj).getCode().equals(code) &&
147
+ ((Mybaseclass) obj).getMessage().equals(message) ;
148
+ }
149
+ return false;
150
+ }
151
+ @Override
152
+ public int hashCode(){
153
+ return (((Long)code).hashCode() +
154
+ ((String)message).hashCode());
155
+ }
156
+
157
+ @Override
158
+ public String toString() {
159
+ Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
160
+ return gson.toJson(this);
161
+ }
162
+ }
163
+ ```
164
+
165
+ ## Contributing
166
+
167
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jaspion/kilza. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
168
+
169
+
170
+ ## License
171
+
172
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kilza"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'highline'
5
+ require 'kilza'
6
+ require 'net/http'
7
+ require 'uri'
8
+
9
+ def read_url(url)
10
+ Net::HTTP.get(URI.parse(url))
11
+ end
12
+
13
+ cli = HighLine.new
14
+ cli.say "Hi <%= color('master', BOLD) %>. I can convert your JSON string to Object files.\n\n"
15
+
16
+ loop do
17
+ begin
18
+ cli.say "<%= color('What can I do for you?', BOLD) %>"
19
+ cli.choose do |menu|
20
+ menu.prompt = ""
21
+ menu.choice("I want some JSON objects.") {
22
+ }
23
+ menu.choice("Nothing.") {
24
+ break
25
+ }
26
+ end
27
+
28
+ json_is_url = false
29
+ cli.say "<%= color('\nWhere is your JSON string content?', BOLD) %>"
30
+ cli.choose do |menu|
31
+ menu.prompt = ""
32
+ menu.choice("It's online. I have an URL.") {
33
+ json_is_url = true
34
+ }
35
+ menu.choice("It's offline. I have the file path.") { cli.say("Ok.") }
36
+ end
37
+
38
+ json_string = ''
39
+ if (json_is_url)
40
+ url = cli.ask ("<%= color('\nPlease, what is the URL?', BOLD) %>")
41
+ cli.say("<%= color('\nOk. I\\'ll try to get the content right now.', BOLD) %>")
42
+ json_string = read_url(url)
43
+ else
44
+ file_path = cli.ask ("<%= color('\nPlease, what is the file path?', BOLD) %>")
45
+ json_string = File.read(file_path)
46
+ end
47
+
48
+ target_path = cli.ask "<%= color('\nMaster, where would you like to save?', BOLD) %>"
49
+ target_path = File.expand_path(target_path)
50
+
51
+ class_basename = cli.ask "<%= color('\nMaster, which name would like to call the first class?', BOLD) %>"
52
+
53
+ target_lang = ''
54
+ cli.say("<%= color('\nPlease choose your target programming language?', BOLD) %>")
55
+ cli.choose do |menu|
56
+ menu.prompt = ""
57
+ menu.choice("Objective-C") {
58
+ target_lang = "objc"
59
+ }
60
+ menu.choice("Java") {
61
+ target_lang = "java"
62
+ }
63
+ end
64
+
65
+ cli.say "<%= color('\nIs that correct?', BOLD) %>"
66
+ cli.say("Target: " + target_path)
67
+ cli.say("Base class name:" + class_basename)
68
+ cli.say("Target language:" + target_lang)
69
+
70
+ cli.choose do |menu|
71
+ menu.prompt = ""
72
+ menu.choice("Yes.") {
73
+ }
74
+ menu.choice("No.") {
75
+ break
76
+ }
77
+ end
78
+
79
+ cli.say("<%= color('\nYes master. Now I\\'m going to generate all files.', BOLD) %>")
80
+ cli.say("<%= color('Berebekan Katabamba Berebekan Katabamba Berebekan Katabamba', BOLD) %>")
81
+
82
+ if (target_lang == "objc")
83
+ objc = Kilza::Objc.new(json_string)
84
+ objc.classes(class_basename).each { |c|
85
+ c.sources.each{ |s|
86
+ File.write(File.join(target_path, s.file_name), s.source)
87
+ }
88
+ }
89
+ else
90
+ java = Kilza::Java.new(json_string)
91
+ java.classes(class_basename).each { |c|
92
+ c.sources.each{ |s|
93
+ File.write(File.join(target_path, s.file_name), s.source)
94
+ }
95
+ }
96
+ end
97
+
98
+ cli.say("<%= color('\nKikera!', BOLD) %>")
99
+
100
+ rescue EOFError # HighLine throws this if @input.eof?
101
+ break
102
+ rescue => e
103
+ cli.say("<%= color('\nOh master. Sorry, but I\\'ve encountered an error:', BOLD) %>")
104
+ puts e
105
+ puts "\n"
106
+ end
107
+ end
108
+
109
+ puts "Goodbye."
110
+ exit
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kilza/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kilza"
8
+ spec.version = Kilza::VERSION
9
+ spec.authors = ["Toshiro Sugii"]
10
+ spec.email = ["rtoshiro@printwtf.com"]
11
+
12
+ spec.summary = %q{Parses JSON objects and generates objects in other languages}
13
+ spec.description = %q{Parses JSON objects and generates objects in other languages}
14
+ spec.homepage = "https://github.com/Jaspion/Kilza"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "bin"
27
+ spec.executables = ["kilza"]
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "minitest"
33
+
34
+ spec.add_dependency "json"
35
+ spec.add_dependency "erubis"
36
+ spec.add_dependency "highline"
37
+ end
@@ -0,0 +1,27 @@
1
+ require "kilza/version"
2
+
3
+ require 'kilza/source'
4
+ require 'kilza/class'
5
+ require 'kilza/property'
6
+ require 'kilza/language'
7
+ require 'kilza/language/objc'
8
+ require 'kilza/language/java'
9
+
10
+ class String
11
+ def is_number?
12
+ true if Float(self) rescue false
13
+ end
14
+ end
15
+
16
+ module Kilza
17
+ def self.clean(str)
18
+ if str[0].is_number?
19
+ str = '_' + str
20
+ end
21
+ str = str.gsub(/[^a-zA-Z0-9]/, '_')
22
+ end
23
+
24
+ def self.normalize(str)
25
+ str = Kilza::clean(str).downcase
26
+ end
27
+ end
@@ -0,0 +1,46 @@
1
+ module Kilza
2
+ module Class
3
+ attr_accessor :name
4
+ attr_accessor :params
5
+ attr_accessor :imports
6
+ attr_accessor :properties
7
+
8
+ def initialize(name)
9
+ @name = Kilza::normalize(name).capitalize
10
+ @properties = []
11
+ @imports = []
12
+ @params = []
13
+ end
14
+
15
+ def find(name)
16
+ @properties.each { |p|
17
+ return p if (p.name == name)
18
+ }
19
+ nil
20
+ end
21
+
22
+ def push(property)
23
+ p = find(property.name)
24
+ if p.nil?
25
+ @properties.push(property)
26
+ end
27
+ end
28
+
29
+ def sources
30
+ end
31
+
32
+ def to_hash
33
+ properties = []
34
+ @properties.each { |p|
35
+ properties.push(p.to_hash)
36
+ }
37
+ hash = {
38
+ :name => @name,
39
+ :imports => @imports,
40
+ :params => @params,
41
+ :properties => properties
42
+ }
43
+ hash
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,80 @@
1
+ require 'json'
2
+ require 'erubis'
3
+
4
+ module Kilza
5
+ module Language
6
+ attr_accessor :classes
7
+ attr_accessor :base_name
8
+ attr_accessor :json_string
9
+
10
+ attr_accessor :reserved_words # words that will receive an undescore before property name
11
+ attr_accessor :equal_keys # array with all properties that will be used to compare other objects
12
+ attr_accessor :types # hash table with all language types mapped to target language
13
+
14
+ def initialize(json_string)
15
+ @json_string = json_string
16
+ @classes = []
17
+ @types = {}
18
+ @reserved_words = []
19
+ @equal_keys = []
20
+ end
21
+
22
+ def get_class(name)
23
+ name = "_" + name if not @reserved_words.index(name).nil?
24
+ Class.new(name)
25
+ end
26
+
27
+ def get_property(name, type, is_array, is_key)
28
+ name = "_" + name if not @reserved_words.index(name).nil?
29
+ Property.new(name, type, is_array, is_key.nil?)
30
+ end
31
+
32
+ def find(name)
33
+ @classes.each { |cl|
34
+ return cl if (cl.name == name)
35
+ }
36
+ @classes.push(get_class(name))
37
+ return @classes.last
38
+ end
39
+
40
+ def parse(hash, class_name)
41
+ current_class = find(class_name)
42
+ hash.each { |property_name, value|
43
+ type = value.class.name.split('::').last.downcase
44
+
45
+ case type
46
+ when "array"
47
+ if (value.length == 0)
48
+ current_class.push(get_property(property_name, 'object', true, @equal_keys.index(property_name)))
49
+ parse(value, property_name)
50
+ else
51
+ value.each { |el|
52
+ if (el.is_a?(Array) or el.is_a?(Hash))
53
+ parse(el, property_name)
54
+ current_class.push(get_property(property_name, 'object', true, @equal_keys.index(property_name)))
55
+ else
56
+ type = el.class.name.split('::').last.downcase
57
+ current_class.push(get_property(property_name, type, true, @equal_keys.index(property_name)))
58
+ end
59
+ }
60
+ end
61
+ when "hash"
62
+ current_class.push(get_property(property_name, 'object', false, @equal_keys.index(property_name)))
63
+ parse(value, property_name)
64
+ else
65
+ current_class.push(get_property(property_name, type, false, @equal_keys.index(property_name)))
66
+ end
67
+ }
68
+ end
69
+
70
+ def classes(base_name)
71
+ hash = JSON.parse(json_string)
72
+ if (hash.is_a?(Array))
73
+ hash = { [base_name + "Object"] => hash }
74
+ end
75
+ parse(hash, base_name)
76
+ return @classes
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,85 @@
1
+ require 'date'
2
+
3
+ module Kilza
4
+ class Java
5
+
6
+ class Class
7
+ include Kilza::Class
8
+
9
+ attr_accessor :package
10
+
11
+ def sources
12
+ cur_path = File.expand_path(__FILE__)
13
+ java_path = File.join(File.dirname(cur_path), File.basename(cur_path, '.rb'), "java.erb")
14
+ eruby_java = Erubis::Eruby.new(File.read(java_path))
15
+
16
+ java = Kilza::Source.new
17
+ java.source = eruby_java.result(binding)
18
+ java.file_name = @name.capitalize + ".java"
19
+
20
+ result = [
21
+ java
22
+ ]
23
+ return result
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+ module Kilza
31
+ class Java
32
+ include Kilza::Language
33
+
34
+ def initialize(json_string)
35
+ super(json_string)
36
+
37
+ @reserved_words = [
38
+ "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
39
+ "package", "synchronized", "boolean", "do", "if", "private", "this", "break", "double", "implements",
40
+ "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof",
41
+ "null", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static",
42
+ "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while"
43
+ ]
44
+
45
+ @types = {
46
+ "nilclass" => "Object",
47
+ "string" => "String",
48
+ "fixnum" => "Long",
49
+ "float" => "Double",
50
+ "falseclass" => "Boolean",
51
+ "trueclass" => "Boolean",
52
+ "object" => "Object"
53
+ }
54
+
55
+ @equal_keys = [
56
+ "id",
57
+ "identifier",
58
+ "uid"
59
+ ]
60
+ end
61
+
62
+ def get_class(name)
63
+ Kilza::Java::Class.new(name)
64
+ end
65
+
66
+ def parse(hash, class_name)
67
+ super(hash, class_name)
68
+
69
+ @classes.each { |cl|
70
+ cl.properties.each { |pr|
71
+ if pr.is_object?
72
+ pr.type = pr.name.capitalize
73
+ end
74
+
75
+ if pr.is_array?
76
+ cl.imports.push("import java.util.ArrayList;") if cl.imports.index("import java.util.ArrayList;").nil?
77
+ end
78
+
79
+ pr.type = @types[pr.type] if not @types[pr.type].nil?
80
+ }
81
+ }
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,125 @@
1
+ package <%= @package %>;
2
+
3
+ import org.json.*;
4
+ import java.io.Serializable;
5
+ <% for @import in @imports %>
6
+ <%= @import %>
7
+ <% end %>
8
+
9
+ import com.google.gson.Gson;
10
+ import com.google.gson.GsonBuilder;
11
+ import com.google.gson.annotations.SerializedName;
12
+ import com.google.gson.annotations.Expose;
13
+
14
+ public class <%= @name %> implements Serializable
15
+ {
16
+ <% for @property in @properties %>
17
+ private static final String FIELD_<%= @property.name.upcase %> = "<%= @property.original_name %>";
18
+ <% end %>
19
+
20
+ <% for @property in @properties %>
21
+ @Expose
22
+ @SerializedName(FIELD_<%= @property.name.upcase %>)
23
+ <% if @property.is_array? %>
24
+ private ArrayList<<%= @property.type %>> <%= @property.name %>;
25
+ <% else %>
26
+ private <%= @property.type %> <%= @property.name %>;
27
+ <% end %>
28
+ <% end %>
29
+
30
+ public <%= @name %>() {
31
+
32
+ }
33
+
34
+ public <%= @name %>(JSONObject jsonObject) {
35
+ parseObject(jsonObject);
36
+ }
37
+
38
+ public <%= @name %>(String jsonString) {
39
+ try {
40
+ parseString(jsonString);
41
+ } catch (JSONException e) {
42
+ e.printStackTrace();
43
+ }
44
+ }
45
+
46
+ protected void parseString(String jsonString) throws JSONException {
47
+ JSONObject jsonObject = new JSONObject(jsonString);
48
+ parseObject(jsonObject);
49
+ }
50
+
51
+ protected void parseObject(JSONObject object)
52
+ {
53
+ <% for @property in @properties %>
54
+ <% if @property.is_array? %>
55
+ if (object.optJSONArray(FIELD_<%= @property.name.upcase %>) != null)
56
+ {
57
+ this.<%= @property.name %> = new ArrayList<>();
58
+ JSONArray <%= @property.name %>JsonArray = object.optJSONArray(FIELD_<%= @property.name.upcase %>);
59
+ for (int i = 0; i < <%= @property.name %>JsonArray.length(); i++) {
60
+ <% if @property.is_object? %>
61
+ JSONObject <%= @property.name %> = <%= @property.name %>JsonArray.optJSONObject(i);
62
+ <% else %>
63
+ <%= @property.type %> <%= @property.name %> = <%= @property.name %>JsonArray.optJSON<%= @property.type %>(i);
64
+ <% end %>
65
+ this.<%= @property.name %>.add(new <%= @property.type %>(<%= @property.name %>));
66
+ }
67
+ }
68
+ <% else %>
69
+ <% if @property.is_object? %>
70
+ this.<%= @property.name %> = new <%= @property.type %>(object.optJSONObject(FIELD_<%= @property.name.upcase %>));
71
+ <% else %>
72
+ <% if @property.is_nil? %>
73
+ this.<%= @property.name %> = object.opt(FIELD_<%= @property.name.upcase %>);
74
+ <% else %>
75
+ this.<%= @property.name %> = object.opt<%= @property.type %>(FIELD_<%= @property.name.upcase %>);
76
+ <% end %>
77
+ <% end %>
78
+ <% end %>
79
+ <% end %>
80
+ }
81
+
82
+ <% for @property in @properties %>
83
+ public void set<%= @property.name.capitalize %>(<%= @property.type %> value) {
84
+ this.<%= @property.name %> = value;
85
+ }
86
+
87
+ <% if @property.is_boolean? %>
88
+ public <%= @property.type %> is<%= @property.name.capitalize %>() {
89
+ <% else %>
90
+ public <%= @property.type %> get<%= @property.name.capitalize %>() {
91
+ <% end %>
92
+ return this.<%= @property.name %>;
93
+ }
94
+
95
+ <% end %>
96
+ <%
97
+ @eq = []
98
+ @hs = []
99
+ for @property in @properties
100
+ @eq.push("((#{@name}) obj).get#{@property.name.capitalize}().equals(#{@property.name})") if (@property.is_key)
101
+ @hs.push("((#{@property.type})#{@property.name}).hashCode()") if (@property.is_key)
102
+ end
103
+ %>
104
+ <% if @eq.length > 0 %>
105
+ @Override
106
+ public boolean equals(Object obj) {
107
+ if (obj instanceof <%= @name %>) {
108
+ return <%= @eq.join(" &&\n ") %> ;
109
+ }
110
+ return false;
111
+ }
112
+ <% end %>
113
+ <% if @hs.length > 0 %>
114
+ @Override
115
+ public int hashCode(){
116
+ return (<%= @hs.join(" +\n ") %>);
117
+ }
118
+ <% end %>
119
+
120
+ @Override
121
+ public String toString() {
122
+ Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
123
+ return gson.toJson(this);
124
+ }
125
+ }
@@ -0,0 +1,94 @@
1
+ require 'date'
2
+
3
+ module Kilza
4
+ class Objc
5
+
6
+ class Class
7
+ include Kilza::Class
8
+
9
+ def sources
10
+ cur_path = File.expand_path(__FILE__)
11
+ m_path = File.join(File.dirname(cur_path), File.basename(cur_path, '.rb'), "m.erb")
12
+ h_path = File.join(File.dirname(cur_path), File.basename(cur_path, '.rb'), "h.erb")
13
+
14
+ eruby_m = Erubis::Eruby.new(File.read(m_path))
15
+ eruby_h = Erubis::Eruby.new(File.read(h_path))
16
+
17
+ m = Kilza::Source.new
18
+ m.source = eruby_m.result(binding)
19
+ m.file_name = @name.capitalize + ".m"
20
+
21
+ h = Kilza::Source.new
22
+ h.source = eruby_h.result(binding)
23
+ h.file_name = @name.capitalize + ".h"
24
+
25
+ result = [
26
+ h,m
27
+ ]
28
+ return result
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ module Kilza
36
+ class Objc
37
+ include Kilza::Language
38
+
39
+ def initialize(json_string)
40
+ super(json_string)
41
+
42
+ @reserved_words = [
43
+ "auto", "break", "case", "char", "const", "continue",
44
+ "class", "default", "do", "double", "else", "enum", "extern", "float", "for",
45
+ "goto", "if", "id", "implementation", "inline", "int", "interface", "long",
46
+ "nonatomic", "property", "protocol", "readonly", "readwrite", "register",
47
+ "restrict", "retain", "return", "short", "signed", "sizeof", "static", "strong",
48
+ "struct", "switch", "typedef", "union", "unsafe_unretained", "unsigned", "void",
49
+ "volatile", "weak", "while", "_bool", "_complex", "_imaginary", "sel", "imp",
50
+ "bool", "nil", "yes", "no", "self", "super", "__strong", "__weak", "oneway",
51
+ "in", "out", "inout", "bycopy", "byref"
52
+ ]
53
+
54
+ @types = {
55
+ "nilclass" => "id",
56
+ "string" => "NSString *",
57
+ "fixnum" => "NSNumber *",
58
+ "float" => "NSNumber *",
59
+ "falseclass" => "NSNumber *",
60
+ "trueclass" => "NSNumber *",
61
+ "object" => "NSObject *"
62
+ }
63
+
64
+ @equal_keys = [
65
+ "id",
66
+ "identifier",
67
+ "uid"
68
+ ]
69
+ end
70
+
71
+ def get_class(name)
72
+ Kilza::Objc::Class.new(name)
73
+ end
74
+
75
+ def parse(hash, class_name)
76
+ super(hash, class_name)
77
+
78
+ @classes.each { |cl|
79
+ cl.properties.each { |pr|
80
+ if pr.is_object?
81
+ cl.imports.push("#import \"" + pr.name.capitalize + ".h\"")
82
+ end
83
+
84
+ if pr.is_array?
85
+ pr.type = "NSMutableArray *"
86
+ end
87
+
88
+ pr.type = @types[pr.type] if not @types[pr.type].nil?
89
+ }
90
+ }
91
+ end
92
+
93
+ end
94
+ end
@@ -0,0 +1,29 @@
1
+ //
2
+ // <%= @name %>.h
3
+ //
4
+ // Created on <%= Time.now.strftime("%Y-%m-%d") %>
5
+ // Copyright (c) <%= Time.now.strftime("%Y") %>. All rights reserved.
6
+ //
7
+
8
+ #import <Foundation/Foundation.h>
9
+
10
+ <% for @property in @properties %>
11
+ <% if @property.is_object? %>
12
+ @class <%= @property.name.capitalize %>;
13
+ <% end %>
14
+ <% end %>
15
+
16
+ @interface <%= @name %> : NSObject <NSCoding, NSCopying>
17
+
18
+ <% for @property in @properties %>
19
+ @property (nonatomic, strong) <%= @property.type %> <%= @property.name %>;
20
+ <% end %>
21
+
22
+ + (<%= @name %> *)modelWithDictionary:(NSDictionary *)dict;
23
+ + (<%= @name %> *)modelWithString:(NSString *)json;
24
+
25
+ - (instancetype)initWithString:(NSString *)json;
26
+ - (instancetype)initWithDictionary:(NSDictionary *)dict;
27
+ - (NSDictionary *)dictionaryRepresentation;
28
+
29
+ @end
@@ -0,0 +1,162 @@
1
+ //
2
+ // <%= @name %>.m
3
+ //
4
+ // Created on <%= Time.now.strftime("%Y-%m-%d") %>
5
+ // Copyright (c) <%= Time.now.strftime("%Y") %>. All rights reserved.
6
+ //
7
+
8
+ #import "<%= @name %>.h"
9
+ <% for @import in @imports %>
10
+ <%= @import %>
11
+ <% end %>
12
+
13
+ // Original names
14
+ <% for @property in @properties %>
15
+ NSString * const k<%= @name %><%= @property.name.capitalize %> = @"<%= @property.original_name %>";
16
+ <% end %>
17
+
18
+ @interface <%= @name %> ()
19
+
20
+ - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
21
+
22
+ @end
23
+
24
+ @implementation <%= @name %>
25
+
26
+ + (<%= @name %> *)modelWithDictionary:(NSDictionary *)dict
27
+ {
28
+ <%= @name %> *instance = [[<%= @name %> alloc] initWithDictionary:dict];
29
+ return instance;
30
+ }
31
+
32
+ + (<%= @name %> *)modelWithString:(NSString *)json
33
+ {
34
+ <%= @name %> *instance = [[<%= @name %> alloc] initWithString:json];
35
+ return instance;
36
+ }
37
+
38
+ - (instancetype)initWithString:(NSString *)json
39
+ {
40
+ self = [super init];
41
+
42
+ NSError *jsonError = nil;
43
+ NSData *objectData = [json dataUsingEncoding:NSUTF8StringEncoding];
44
+ NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:objectData
45
+ options:NSJSONReadingMutableContainers
46
+ error:&jsonError];
47
+ if (!jsonError)
48
+ self = [self initWithDictionary:dict];
49
+
50
+ return self;
51
+ }
52
+
53
+ - (instancetype)initWithDictionary:(NSDictionary *)dict
54
+ {
55
+ self = [super init];
56
+
57
+ if (self && [dict isKindOfClass:[NSDictionary class]])
58
+ {
59
+ <% for @property in @properties %>
60
+ <% if @property.is_object? %>
61
+ NSObject *obj<%= @property.name.capitalize %> = [dict objectForKey:k<%= @name %><%= @property.name.capitalize %>];
62
+ <% if @property.is_array? %>
63
+ if ([obj<%= @property.name.capitalize %> isKindOfClass:[NSArray class]])
64
+ {
65
+ NSMutableArray *list<%= @property.name.capitalize %> = [NSMutableArray array];
66
+ for (NSDictionary *item in (NSArray *)obj<%= @property.name.capitalize %>) {
67
+ if ([item isKindOfClass:[NSDictionary class]]) {
68
+ [list<%= @property.name.capitalize %> addObject:[<%= @property.name.capitalize %> modelWithDictionary:(NSDictionary *)item]];
69
+ }
70
+ }
71
+ self.<%= @property.name %> = list<%= @property.name.capitalize %>;
72
+ }
73
+ <% else %>
74
+ {
75
+ self.<%= @property.name %> = [<%= @property.name.capitalize %> modelWithDictionary:(NSDictionary *)obj<%= @property.name.capitalize %>];
76
+ }
77
+ <% end %>
78
+ <% else %>
79
+ self.<%= @property.name %> = [self objectOrNilForKey:k<%= @name %><%= @property.name.capitalize %> fromDictionary:dict];
80
+ <% end %>
81
+ <% end %>
82
+ }
83
+ return self;
84
+ }
85
+
86
+ - (NSDictionary *)dictionaryRepresentation
87
+ {
88
+ NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
89
+
90
+ <% for @property in @properties %>
91
+ <% if @property.is_object? %>
92
+ <% if @property.is_array? %>
93
+ NSMutableArray *tempArray<%= @property.name.capitalize %> = [NSMutableArray array];
94
+ for (NSObject *subArray in self.<%= @property.name %>) {
95
+ if ([subArray respondsToSelector:@selector(dictionaryRepresentation)]) {
96
+ [tempArray<%= @property.name.capitalize %> addObject:[subArray performSelector:@selector(dictionaryRepresentation)]];
97
+ } else {
98
+ [tempArray<%= @property.name.capitalize %> addObject:subArray];
99
+ }
100
+ }
101
+ [mutableDict setValue:[NSArray arrayWithArray:tempArray<%= @property.name.capitalize %>] forKey:k<%= @name %><%= @property.name.capitalize %>];
102
+ <% else %>
103
+ if ([self.<%= @property.name %> respondsToSelector:@selector(dictionaryRepresentation)]) {
104
+ [mutableDict setValue:[self.<%= @property.name %> performSelector:@selector(dictionaryRepresentation)] forKey:k<%= @name %><%= @property.name.capitalize %>];
105
+ } else {
106
+ [mutableDict setValue:self.<%= @property.name %> forKey:k<%= @name %><%= @property.name.capitalize %>];
107
+ }
108
+ <% end %>
109
+ <% else %>
110
+ [mutableDict setValue:self.<%= @property.name %> forKey:k<%= @name %><%= @property.name.capitalize %>];
111
+ <% end %>
112
+ <% end %>
113
+
114
+ return [NSDictionary dictionaryWithDictionary:mutableDict];
115
+ }
116
+
117
+ - (NSString *)description
118
+ {
119
+ return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
120
+ }
121
+
122
+ #pragma mark - Helper Method
123
+ - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
124
+ {
125
+ id object = [dict objectForKey:aKey];
126
+ return [object isEqual:[NSNull null]] ? nil : object;
127
+ }
128
+
129
+ #pragma mark - NSCoding Methods
130
+
131
+ - (id)initWithCoder:(NSCoder *)aDecoder
132
+ {
133
+ self = [super init];
134
+
135
+ <% for @property in @properties %>
136
+ self.<%= @property.name %> = [aDecoder decodeObjectForKey:k<%= @name %><%= @property.name.capitalize %>];
137
+ <% end %>
138
+
139
+ return self;
140
+ }
141
+
142
+ - (void)encodeWithCoder:(NSCoder *)aCoder
143
+ {
144
+ <% for @property in @properties %>
145
+ [aCoder encodeObject:_<%= @property.name %> forKey:k<%= @name %><%= @property.name.capitalize %>];
146
+ <% end %>
147
+ }
148
+
149
+ - (id)copyWithZone:(NSZone *)zone
150
+ {
151
+ <%= @name %> *copy = [[<%= @name %> alloc] init];
152
+ if (copy)
153
+ {
154
+ <% for @property in @properties %>
155
+ copy.<%= @property.name %> = [self.<%= @property.name %> copyWithZone:zone];
156
+ <% end %>
157
+ }
158
+
159
+ return copy;
160
+ }
161
+
162
+ @end
@@ -0,0 +1,58 @@
1
+ module Kilza
2
+ class Property
3
+ attr_accessor :name
4
+ attr_accessor :original_name
5
+ attr_accessor :type
6
+ attr_accessor :params
7
+ attr_accessor :is_array
8
+ attr_accessor :is_key
9
+
10
+ def initialize(name, type, is_array, is_key)
11
+ @name = Kilza::normalize(name)
12
+ @original_name = name
13
+ @type = type
14
+ @is_array = is_array
15
+ @is_key = is_key
16
+ end
17
+
18
+ def is_array?
19
+ @is_array
20
+ end
21
+
22
+ def is_object?
23
+ @type == 'object'
24
+ end
25
+
26
+ def is_fixnum?
27
+ @type == 'fixnum'
28
+ end
29
+
30
+ def is_boolean?
31
+ @type == 'trueclass' or @type == 'falseclass'
32
+ end
33
+
34
+ def is_float?
35
+ @type == 'float'
36
+ end
37
+
38
+ def is_nil?
39
+ @type == 'nilclass'
40
+ end
41
+
42
+ def to_hash
43
+ hash = {
44
+ :name => @name,
45
+ :original_name => @original_name,
46
+ :type => @type,
47
+ :params => @params,
48
+ :is_array? => @is_array,
49
+ :is_object? => is_object?,
50
+ :is_fixnum? => is_fixnum?,
51
+ :is_boolean? => is_boolean?,
52
+ :is_float? => is_float?,
53
+ :is_nil? => is_nil?
54
+ }
55
+ hash
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,6 @@
1
+ module Kilza
2
+ class Source
3
+ attr_accessor :file_name
4
+ attr_accessor :source
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Kilza
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kilza
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Toshiro Sugii
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
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
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: erubis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: highline
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Parses JSON objects and generates objects in other languages
98
+ email:
99
+ - rtoshiro@printwtf.com
100
+ executables:
101
+ - kilza
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .travis.yml
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/kilza
114
+ - bin/setup
115
+ - kilza.gemspec
116
+ - lib/kilza.rb
117
+ - lib/kilza/class.rb
118
+ - lib/kilza/language.rb
119
+ - lib/kilza/language/java.rb
120
+ - lib/kilza/language/java/java.erb
121
+ - lib/kilza/language/objc.rb
122
+ - lib/kilza/language/objc/h.erb
123
+ - lib/kilza/language/objc/m.erb
124
+ - lib/kilza/property.rb
125
+ - lib/kilza/source.rb
126
+ - lib/kilza/version.rb
127
+ homepage: https://github.com/Jaspion/Kilza
128
+ licenses:
129
+ - MIT
130
+ metadata:
131
+ allowed_push_host: rubygems.org
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.0.14
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Parses JSON objects and generates objects in other languages
152
+ test_files: []