Compute the number of object properties to serialize.
parent
3ab21c9616
commit
5f838d36cc
|
@ -136,6 +136,7 @@ module CBOR
|
|||
end
|
||||
|
||||
private def self.new_from_cbor_decoder(decoder : ::CBOR::Decoder)
|
||||
# puts "self.new_from_cbor_decoder"
|
||||
instance = allocate
|
||||
instance.initialize(__decoder_for_cbor_serializable: decoder)
|
||||
GC.add_finalizer(instance) if instance.responds_to?(:finalize)
|
||||
|
@ -268,7 +269,25 @@ module CBOR
|
|||
{% end %}
|
||||
{% end %}
|
||||
|
||||
cbor.object do
|
||||
# Compute the size of the final list of properties to serialize.
|
||||
# This allows a more compact encoding, and a faster decoding.
|
||||
nb_properties_to_serialize = 0
|
||||
{% for name, value in properties %}
|
||||
_{{name}} = @{{name}}
|
||||
{% unless value[:emit_null] %}
|
||||
unless _{{name}}.nil?
|
||||
nb_properties_to_serialize += 1
|
||||
end
|
||||
{% else %}
|
||||
nb_properties_to_serialize += 1
|
||||
{% end %} # macro unless value[:emit_null]
|
||||
{% end %} # macro for properties
|
||||
|
||||
|
||||
|
||||
{% if properties.size > 0 %}
|
||||
cbor.write_object_start nb_properties_to_serialize
|
||||
|
||||
{% for name, value in properties %}
|
||||
_{{name}} = @{{name}}
|
||||
|
||||
|
@ -285,17 +304,17 @@ module CBOR
|
|||
else
|
||||
cbor.write(nil, use_undefined: value[:nil_as_undefined])
|
||||
end
|
||||
{% else %}
|
||||
{% else %} # macro if value[:converter]
|
||||
_{{name}}.to_cbor(cbor)
|
||||
{% end %}
|
||||
{% end %} # macro if value[:converter]
|
||||
|
||||
{% unless value[:emit_null] %}
|
||||
end
|
||||
{% end %}
|
||||
{% end %}
|
||||
end # unless _{{name}}.nil?
|
||||
{% end %} # macro unless value[:emit_null]
|
||||
{% end %} # macro for properties
|
||||
on_to_cbor(cbor)
|
||||
end
|
||||
{% end %}
|
||||
{% end %} # macro if properties.size > 0
|
||||
{% end %} # begin
|
||||
end
|
||||
|
||||
module Unmapped
|
||||
|
|
Loading…
Reference in New Issue