Module: FIRM::Serializable::JSON

Defined in:
lib/firm/serializer/json.rb

Defined Under Namespace

Modules: ContainerPatch, SerializeClassMethods, SerializeInstanceMethods Classes: ObjectHash

Constant Summary collapse

CREATE_ID =
'rbklass'.freeze

Class Method Summary collapse

Class Method Details

.dump(obj, io = nil, pretty: false) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/firm/serializer/json.rb', line 165

def self.dump(obj, io=nil, pretty: false)
  # obj.extend(HashInstancePatch) if obj.is_a?(::Hash)
  begin
    # initialize anchor registry
    Serializable::Aliasing.start_anchor_object_registry
    # set custom (more compact) create_id
    ::JSON.create_id = Serializable::JSON::CREATE_ID
    for_json = obj.respond_to?(:as_json) ? obj.as_json : obj
    if pretty
      if io || io.respond_to?(:write)
        io.write(::JSON.pretty_generate(for_json))
        io
      else
        ::JSON.pretty_generate(for_json)
      end
    else
      ::JSON.dump(for_json, io)
    end
  ensure
    # reset anchor registry
    Serializable::Aliasing.clear_anchor_object_registry
  end
end

.end_parseObject



154
155
156
157
158
# File 'lib/firm/serializer/json.rb', line 154

def end_parse
  unless (val = parse_stack.pop).nil?
    safe_deserialize.push(val)
  end
end

.end_safe_deserializeObject



141
142
143
# File 'lib/firm/serializer/json.rb', line 141

def end_safe_deserialize
  safe_deserialize.pop
end

.load(source) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/firm/serializer/json.rb', line 189

def self.load(source)
  begin
    # initialize alias anchor restoration map
    Serializable::Aliasing.start_anchor_references
    # enable safe deserializing
    self.start_safe_deserialize
    # set custom (more compact) create_id
    ::JSON.create_id = Serializable::JSON::CREATE_ID
    ::JSON.parse!(source,
                  create_additions: true,
                  object_class: Serializable::JSON::ObjectHash)
  ensure
    # reset safe deserializing
    self.end_safe_deserialize
    # reset alias anchor restoration map
    Serializable::Aliasing.clear_anchor_references
  end
end

.parse_stackObject



145
146
147
# File 'lib/firm/serializer/json.rb', line 145

def parse_stack
  Serializable.tls_vars[TLS_PARSE_STACK_KEY] ||= []
end

.safe_deserializeObject



132
133
134
# File 'lib/firm/serializer/json.rb', line 132

def safe_deserialize
  Serializable.tls_vars[TLS_SAFE_DESERIALIZE_KEY] ||= []
end

.safe_parsing?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/firm/serializer/json.rb', line 160

def safe_parsing?
  !!parse_stack.last
end

.serializablesObject



118
119
120
121
122
123
124
# File 'lib/firm/serializer/json.rb', line 118

def serializables
  set = ::Set.new( [::NilClass, ::TrueClass, ::FalseClass, ::Integer, ::Float, ::String, ::Array, ::Hash,
             ::Date, ::DateTime, ::Range, ::Rational, ::Complex, ::Regexp, ::Struct, ::Symbol, ::Time, ::Set])
  set << ::OpenStruct if ::Object.const_defined?(:OpenStruct)
  set << ::BigDecimal if ::Object.const_defined?(:BigDecimal)
  set
end

.start_parseObject



150
151
152
# File 'lib/firm/serializer/json.rb', line 150

def start_parse
  parse_stack.push(safe_deserialize.pop)
end

.start_safe_deserializeObject



137
138
139
# File 'lib/firm/serializer/json.rb', line 137

def start_safe_deserialize
  safe_deserialize.push(true)
end