Module: Jsoning
- Defined in:
- lib/jsoning.rb,
lib/jsoning/version.rb
Defined Under Namespace
Classes: Error, ForDsl, Mapper, Protocol
Constant Summary collapse
- PROTOCOLS =
{}
- TYPE_EXTENSIONS =
if type is defined here, we will use it to extract its value for the key
{}
- VERSION =
"0.5.0"- @@type_extension_initialized =
false
Class Method Summary collapse
- .[](object) ⇒ Object
- .add_type(klass, options = {}) ⇒ Object
-
.clear ⇒ Object
clearing the protocols.
-
.for(klass, &block) ⇒ Object
define the protocol.
-
.generate(object, options = {}) ⇒ Object
generate the json document.
-
.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) ⇒ Object
parse the JSON String to Hash.
-
.protocol_for(klass) ⇒ Object
returns a protocol, or create one if none exists.
-
.protocol_for!(klass) ⇒ Object
retrieve the protocol or raise an error when the protocol is not defined yet.
Class Method Details
.[](object) ⇒ Object
87 88 89 90 91 |
# File 'lib/jsoning.rb', line 87 def [](object) Jsoning.initialize_type_extensions protocol = protocol_for!(object.class) protocol.retrieve_values_from(object) end |
.add_type(klass, options = {}) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/jsoning.rb', line 93 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
35 36 37 |
# File 'lib/jsoning.rb', line 35 def clear PROTOCOLS.clear end |
.for(klass, &block) ⇒ Object
define the protocol
40 41 42 |
# File 'lib/jsoning.rb', line 40 def for(klass, &block) Jsoning::ForDsl.new(protocol_for(klass)).instance_eval(&block) end |
.generate(object, options = {}) ⇒ Object
generate the json document
45 46 47 48 |
# File 'lib/jsoning.rb', line 45 def generate(object, = {}) protocol = protocol_for!(object.class) protocol.generate(object, ) end |
.initialize_type_extensions ⇒ Object
define value extractor/interpreter for commonly used ruby datatypes that are not part of standard types supported by JSON
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/jsoning.rb', line 53 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) ⇒ Object
parse the JSON String to Hash
111 112 113 114 115 |
# File 'lib/jsoning.rb', line 111 def parse(json_string, klass) Jsoning.initialize_type_extensions protocol = protocol_for!(klass) protocol.construct_hash_from(json_string) end |
.protocol_for(klass) ⇒ Object
returns a protocol, or create one if none exists
18 19 20 21 22 23 24 25 |
# File 'lib/jsoning.rb', line 18 def protocol_for(klass) protocol = PROTOCOLS[klass.to_s] if protocol.nil? protocol = Jsoning::Protocol.new(klass) PROTOCOLS[klass.to_s] = protocol end protocol end |
.protocol_for!(klass) ⇒ Object
retrieve the protocol or raise an error when the protocol is not defined yet
28 29 30 31 32 |
# File 'lib/jsoning.rb', line 28 def protocol_for!(klass) protocol = PROTOCOLS[klass.to_s] raise Jsoning::Error, "Undefined Jsoning protocol for #{klass.to_s}" if protocol.nil? protocol end |