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
- .dump(obj, io = nil, pretty: false) ⇒ Object
- .end_parse ⇒ Object
- .end_safe_deserialize ⇒ Object
- .load(source) ⇒ Object
- .parse_stack ⇒ Object
- .safe_deserialize ⇒ Object
- .safe_parsing? ⇒ Boolean
- .serializables ⇒ Object
- .start_parse ⇒ Object
- .start_safe_deserialize ⇒ Object
Class Method Details
.dump(obj, io = nil, pretty: false) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/firm/serializer/json.rb', line 162 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_parse ⇒ Object
151 152 153 154 155 |
# File 'lib/firm/serializer/json.rb', line 151 def end_parse unless (val = parse_stack.pop).nil? safe_deserialize.push(val) end end |
.end_safe_deserialize ⇒ Object
138 139 140 |
# File 'lib/firm/serializer/json.rb', line 138 def end_safe_deserialize safe_deserialize.pop end |
.load(source) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/firm/serializer/json.rb', line 186 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_stack ⇒ Object
142 143 144 |
# File 'lib/firm/serializer/json.rb', line 142 def parse_stack Serializable.tls_vars[TLS_PARSE_STACK_KEY] ||= [] end |
.safe_deserialize ⇒ Object
129 130 131 |
# File 'lib/firm/serializer/json.rb', line 129 def safe_deserialize Serializable.tls_vars[TLS_SAFE_DESERIALIZE_KEY] ||= [] end |
.safe_parsing? ⇒ Boolean
157 158 159 |
# File 'lib/firm/serializer/json.rb', line 157 def safe_parsing? !!parse_stack.last end |
.serializables ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/firm/serializer/json.rb', line 115 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_parse ⇒ Object
147 148 149 |
# File 'lib/firm/serializer/json.rb', line 147 def start_parse parse_stack.push(safe_deserialize.pop) end |
.start_safe_deserialize ⇒ Object
134 135 136 |
# File 'lib/firm/serializer/json.rb', line 134 def start_safe_deserialize safe_deserialize.push(true) end |