Method: Jsoning.initialize_type_extensions

Defined in:
lib/jsoning.rb

.initialize_type_extensionsObject

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