Module: Jsoning
- Defined in:
- lib/jsoning.rb,
lib/jsoning/version.rb
Defined Under Namespace
Classes: Error, ForDsl, Mapper, Protocol, Version
Constant Summary collapse
- PROTOCOLS =
{}
- TYPE_EXTENSIONS =
if type is defined here, we will use it to extract its value for the key
{}
- VERSION =
"0.8.0"- @@type_extension_initialized =
false
Class Method Summary collapse
-
.[](object) ⇒ Object
using [] will generate using default schema.
-
.add_type(klass, options = {}) ⇒ Object
add custom type explaining to Jsoning how Jsoning should extract value from this kind of class.
-
.clear ⇒ Object
clearing the protocols.
-
.for(klass, &block) ⇒ Object
define the protocol.
-
.generate(object, options = {}) ⇒ Object
generate the json document options: - hash: specify if the return is a hash - pretty: only for when hash is set to flash, print JSON pretty - version: specify the version to be used for the processing.
- .generate_hash(object, protocol, options) ⇒ Object
-
.generate_json(object, protocol, options) ⇒ Object
generate a JSON object options: - pretty: pretty print json data.
-
.initialize_type_extensions ⇒ Object
define value extractor/interpreter for commonly used ruby datatypes that are not part of standard types supported by JSON.
-
.parse(json_string, klass, version_name = :default) ⇒ Object
parse the JSON String to Hash.
-
.protocol_for!(klass) ⇒ Object
retrieve the protocol or raise an error when the protocol is not defined yet.
-
.protocol_for_or_create(klass) ⇒ Object
returns a protocol, or create one if none exists.
Class Method Details
.[](object) ⇒ Object
using [] will generate using default schema
123 124 125 |
# File 'lib/jsoning.rb', line 123 def [](object) generate(object, hash: true, version: :default) end |
.add_type(klass, options = {}) ⇒ Object
add custom type explaining to Jsoning how Jsoning should extract value from this kind of class
129 130 131 132 133 134 |
# File 'lib/jsoning.rb', line 129 def add_type(klass, = {}) processor = [:processor] raise Jsoning::Error, "Pass in processor that is a proc explaining how to extract the value" unless processor.is_a?(Proc) TYPE_EXTENSIONS[klass.to_s] = processor nil end |
.clear ⇒ Object
clearing the protocols
36 37 38 |
# File 'lib/jsoning.rb', line 36 def clear PROTOCOLS.clear end |
.for(klass, &block) ⇒ Object
define the protocol
41 42 43 |
# File 'lib/jsoning.rb', line 41 def for(klass, &block) Jsoning::ForDsl.new(protocol_for_or_create(klass)).instance_eval(&block) end |
.generate(object, options = {}) ⇒ Object
generate the json document options:
-
hash: specify if the return is a hash
-
pretty: only for when hash is set to flash, print JSON pretty
-
version: specify the version to be used for the processing
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/jsoning.rb', line 50 def generate(object, = {}) Jsoning.initialize_type_extensions protocol = protocol_for!(object.class) # use default version if version is unspecified [:version] = :default if [:version].nil? if [:hash] == true return generate_hash(object, protocol, ) else return generate_json(object, protocol, ) end end |
.generate_hash(object, protocol, options) ⇒ Object
81 82 83 |
# File 'lib/jsoning.rb', line 81 def generate_hash(object, protocol, ) as_hash = protocol.retrieve_values_from(object, ) end |
.generate_json(object, protocol, options) ⇒ Object
generate a JSON object options:
-
pretty: pretty print json data
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jsoning.rb', line 67 def generate_json(object, protocol, ) pretty = [:pretty] pretty = ["pretty"] if pretty.nil? pretty = false if pretty.nil? data = protocol.retrieve_values_from(object, ) if pretty JSON.pretty_generate(data) else JSON.generate(data) end end |
.initialize_type_extensions ⇒ Object
define value extractor/interpreter for commonly used ruby datatypes that are not part of standard types supported by JSON
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/jsoning.rb', line 88 def initialize_type_extensions @@type_extension_initialized = true if !!@@type_extension_initialized return if @@type_extension_initialized begin ::Time self.add_type ::Time, processor: proc { |time| time.strftime("%FT%T%z") } rescue end begin # try to define value extractor for ActiveSupport::TimeWithZone which is in common use # for AR model ::ActiveSupport::TimeWithZone self.add_type ActiveSupport::TimeWithZone, processor: proc { |time| time.strftime("%FT%T%z") } rescue # nothing, don't add end begin ::DateTime self.add_type ::DateTime, processor: proc { |date| date.strftime("%FT%T%z") } rescue => e # nothing, don't add end begin ::Date self.add_type ::Date, processor: proc { |date| date.strftime("%FT%T%z") } rescue # nothing, don't add end end |
.parse(json_string, klass, version_name = :default) ⇒ Object
parse the JSON String to Hash
146 147 148 149 150 |
# File 'lib/jsoning.rb', line 146 def parse(json_string, klass, version_name = :default) Jsoning.initialize_type_extensions protocol = protocol_for!(klass) protocol.construct_hash_from(json_string, version_name) end |
.protocol_for!(klass) ⇒ Object
retrieve the protocol or raise an error when the protocol is not defined yet
29 30 31 32 33 |
# File 'lib/jsoning.rb', line 29 def protocol_for!(klass) protocol = PROTOCOLS[klass.to_s] raise Jsoning::Error, "Undefined Jsoning protocol for #{klass.to_s}" if protocol.nil? protocol end |
.protocol_for_or_create(klass) ⇒ Object
returns a protocol, or create one if none exists
19 20 21 22 23 24 25 26 |
# File 'lib/jsoning.rb', line 19 def protocol_for_or_create(klass) protocol = PROTOCOLS[klass.to_s] if protocol.nil? protocol = Jsoning::Protocol.new(klass) PROTOCOLS[klass.to_s] = protocol end protocol end |