module GSONClassGenerator

Constants

GSON_SERIALIZED_NAME_IMPORT

Public Class Methods

generate(json_filename, class_name, output_directory, opts = {}) click to toggle source
# File lib/gson-class-generator.rb, line 7
def self.generate(json_filename, class_name, output_directory, opts = {})
        
        begin
        json = JSON.parse(File.read(File.expand_path(json_filename)))
        rescue Exception => e
                abort "Invalid JSON: #{e.message}"
        end
        
        java_class = parse(json_filename, class_name)
        output_options = OutputOptions.new(opts)
        
        output(java_class, output_directory, output_options)
end

Private Class Methods

output(java_class, output_directory, opts) click to toggle source
# File lib/gson-class-generator.rb, line 30
def self.output(java_class, output_directory, opts)
        
        filename = File.join(File.expand_path(output_directory), "#{java_class.class_name}.java")
        
        File.open(filename, "w") do |file|
                
                file << GSON_SERIALIZED_NAME_IMPORT
                file << "\n"
                file << "\n"
                
                java_class.write(file, opts)
        end
        
        filename
end
parse(json_filename, class_name) click to toggle source
# File lib/gson-class-generator.rb, line 25
def self.parse(json_filename, class_name)
        json = JSON.parse(File.read(File.expand_path(json_filename)))
        JavaClass.new(class_name, json)
end