json2pojo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/json2pojo +16 -0
  3. data/lib/pojo_creator.rb +61 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5cdc12103ca7334f1c9250bc36eb3dd368ee22e1
4
+ data.tar.gz: ea5675a9db7721d7557e11bf29bc6ea1c47adc46
5
+ SHA512:
6
+ metadata.gz: 568d2e1c6b2b75a1760cd6d07a0c4b6f1cb5864f5d0a2608d023877eb268be9d1c8434ea7c627f3445c5d0992857fbab0cd4628188cdf3e8bf81335e8b4e78a6
7
+ data.tar.gz: 0110145c0dbf164384852a1d4857a53310723f805bf474ed01631e67a2d8fbccd21d2c0261df6ff47ddfc409288fbab205ce0f9b5dce117068b73f3a2dc51324
data/bin/json2pojo ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/pojo_creator'
4
+
5
+ if (ARGV.size != 2)
6
+ puts "Usage is json2pojo <json_file> <class_name>"
7
+ exit
8
+ end
9
+
10
+ json_file = File.open(ARGV[0])
11
+ json = ""
12
+ while(line = json_file.gets)
13
+ json += line + "\n"
14
+ end
15
+
16
+ puts PojoCreator.new(ARGV[1]).create_pojo(json)
@@ -0,0 +1,61 @@
1
+ require "json"
2
+
3
+ class PojoCreator
4
+
5
+ VERSION = '0.1.0'
6
+
7
+ def initialize(class_name)
8
+ @class_name = get_class_name(class_name)
9
+ @additional_classes = ""
10
+ end
11
+
12
+ def create_pojo(json_string)
13
+ json = JSON.parse(json_string)
14
+ pojo = "public class #{@class_name} {#{get_properties(json)}}" + @additional_classes
15
+ end
16
+
17
+ private
18
+
19
+ def get_properties(json)
20
+ prop_string = ""
21
+
22
+ json.keys.each do |property|
23
+ if (json[property].class.to_s == "Hash")
24
+ @additional_classes << "class #{get_class_name(property)} {" + get_properties(json[property]) + "}"
25
+ prop_string += "\t@JsonProperty(\"#{property}\")\tprivate #{get_class_name(property)} #{get_field_name(property)};"
26
+ elsif(json[property].class.to_s == "Array")
27
+ @additional_classes << "class #{get_class_name(property)} {" + get_properties(json[property][0]) + "}"
28
+ prop_string += "\t@JsonProperty(\"#{property}\")\tprivate List<#{get_class_name(property)}> #{get_field_name(property)};"
29
+ else
30
+ prop_string += "\t@JsonProperty(\"#{property}\")\tprivate #{get_type(json[property])} #{get_field_name(property)};"
31
+ end
32
+ end
33
+ prop_string
34
+
35
+ end
36
+
37
+ def get_field_name(property)
38
+ snake_case_to_field_name(property)
39
+ end
40
+
41
+ def snake_case_to_class_name(string)
42
+ snake_case_to_field_name(string).gsub(/(^.)/, &:upcase)
43
+ end
44
+
45
+ def snake_case_to_field_name(string)
46
+ string.gsub(/(_.)/, &:upcase).gsub("_", "")
47
+ end
48
+
49
+ def get_class_name(property)
50
+ snake_case_to_class_name(property)
51
+ end
52
+
53
+ def get_type(value)
54
+ if value.class.to_s == "Fixnum"
55
+ return "Integer"
56
+ else
57
+ return "String"
58
+ end
59
+ end
60
+
61
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json2pojo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Balazs Mester
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Creates a POJO from an example JSON file.
14
+ email: balazs.mester@gmail.com
15
+ executables:
16
+ - json2pojo
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/json2pojo
21
+ - lib/pojo_creator.rb
22
+ homepage: http://github.com/mestahh/json2pojo
23
+ licenses:
24
+ - MIT License
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: JSON to POJO converter.
46
+ test_files: []