Class: Puppet::Pops::Serialization::JSON::Packer Private
- Defined in:
- lib/puppet/pops/serialization/json.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
The JSON Packer. Modeled after the MessagePack::Packer
Instance Method Summary collapse
- #clear_io ⇒ Object private
- #empty? ⇒ Boolean private
- #flush ⇒ Object private
-
#initialize(io, options = {}) ⇒ Packer
constructor
private
A new instance of Packer.
- #register_type(type, klass, &block) ⇒ Object private
- #to_a ⇒ Object private
- #to_json ⇒ Object private
- #to_s ⇒ Object private
- #write(obj) ⇒ Object (also: #pack) private
- #write_array_header(n) ⇒ Object private
- #write_map_header(n) ⇒ Object private
- #write_nil ⇒ Object private
-
#write_pl(obj) ⇒ Object
private
Write a payload object.
Constructor Details
#initialize(io, options = {}) ⇒ Packer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Packer.
65 66 67 68 69 70 71 72 73 |
# File 'lib/puppet/pops/serialization/json.rb', line 65 def initialize(io, = {}) @io = io @io << '[' @type_registry = {} @nested = [] @verbose = [:verbose] @verbose = false if @verbose.nil? @indent = [:indent] || 0 end |
Instance Method Details
#clear_io ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
79 80 81 82 83 84 85 86 |
# File 'lib/puppet/pops/serialization/json.rb', line 79 def clear_io # Truncate everything except leading '[' if @io.is_a?(String) @io.slice!(1..-1) else @io.truncate(1) end end |
#empty? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
88 89 90 |
# File 'lib/puppet/pops/serialization/json.rb', line 88 def empty? @io.is_a?(String) ? io.length == 1 : @io.pos == 1 end |
#flush ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/puppet/pops/serialization/json.rb', line 92 def flush # Drop last comma unless just start marker present if @io.is_a?(String) @io.chop! unless @io.length == 1 @io << ']' else pos = @io.pos @io.pos = pos - 1 unless pos == 1 @io << ']' @io.flush end end |
#register_type(type, klass, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
75 76 77 |
# File 'lib/puppet/pops/serialization/json.rb', line 75 def register_type(type, klass, &block) @type_registry[klass] = [type, klass, block] end |
#to_a ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
148 149 150 |
# File 'lib/puppet/pops/serialization/json.rb', line 148 def to_a ::JSON.parse(io_string) end |
#to_json ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
152 153 154 155 156 157 158 |
# File 'lib/puppet/pops/serialization/json.rb', line 152 def to_json if @indent > 0 ::JSON.pretty_unparse(to_a, { :indent => ' ' * @indent }) else io_string end end |
#to_s ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
144 145 146 |
# File 'lib/puppet/pops/serialization/json.rb', line 144 def to_s to_json end |
#write(obj) ⇒ Object Also known as: pack
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/puppet/pops/serialization/json.rb', line 105 def write(obj) case obj when Array write_array_header(obj.size) obj.each { |x| write(x) } when Hash write_map_header(obj.size) obj.each_pair {|k, v| write(k); write(v) } when nil write_nil else write_scalar(obj) end end |
#write_array_header(n) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
121 122 123 124 125 126 127 128 |
# File 'lib/puppet/pops/serialization/json.rb', line 121 def write_array_header(n) if n < 1 @io << '[]' else @io << '[' @nested << [false, n] end end |
#write_map_header(n) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
130 131 132 133 134 135 136 137 |
# File 'lib/puppet/pops/serialization/json.rb', line 130 def write_map_header(n) if n < 1 @io << '{}' else @io << '{' @nested << [true, n * 2] end end |
#write_nil ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
139 140 141 142 |
# File 'lib/puppet/pops/serialization/json.rb', line 139 def write_nil @io << 'null' write_delim end |
#write_pl(obj) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Write a payload object. Not subject to extensions
161 162 163 |
# File 'lib/puppet/pops/serialization/json.rb', line 161 def write_pl(obj) @io << obj.to_json << ',' end |