Class: Puppet::Pops::Serialization::JSON::Packer Private

Inherits:
Object
  • Object
show all
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

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, options = {})
  @io = io
  @io << '['
  @type_registry = {}
  @nested = []
  @verbose = options[:verbose]
  @verbose = false if @verbose.nil?
  @indent = options[:indent] || 0
end

Instance Method Details

#clear_ioObject

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

#flushObject

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_aObject

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
  ::Puppet::Util::Json.load(io_string)
end

#to_jsonObject

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
    ::Puppet::Util::Json.dump(to_a, { :pretty => true, :indent => ' ' * @indent })
  else
    io_string
  end
end

#to_sObject

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_nilObject

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 << Puppet::Util::Json.dump(obj) << ','
end