lib/mutils/serialization/base_serializer.rb in mutils-0.2.25 vs lib/mutils/serialization/base_serializer.rb in mutils-0.2.27
- old
+ new
@@ -1,32 +1,49 @@
# frozen_string_literal: true
+
require 'json/ext'
# module Mutils
module Mutils
module Serialization
- # BaseSerializer
+ # BaseSerializer: inherit this class to get Serializer functionality
class BaseSerializer
include Mutils::Serialization::SerializationIncludes
include Mutils::Serialization::SerializationMethods
include Mutils::Serialization::SerializationResults
- attr_accessor :scope, :options, :mutex
def initialize(object, options = {})
+ options[:child] = false unless options[:child]
self.scope = object
self.options = options
self.mutex = Mutex.new
end
def as_json(_options = {})
- to_h
+ if options[:child] || !self.class.include_root
+ to_h
+ else
+ { class_name => to_h }
+ end
end
def to_h
generate_hash
end
def to_json(_options = {})
- JSON.generate(to_h)
+ JSON.generate(as_json, options)
end
+
+ def to_xml(_options = {})
+ to_h.to_xml(root: class_name, skip_instruct: true, indent: 2)
+ end
+
+ def as_xml(_options = {})
+ to_xml
+ end
+
+ private
+
+ attr_accessor :scope, :options, :mutex
end
end
-end
\ No newline at end of file
+end