Class: Json::Merge::Emitter

Inherits:
Ast::Merge::EmitterBase
  • Object
show all
Includes:
Ast::Merge::EmitterLineMetadataSupport
Defined in:
lib/json/merge/emitter.rb

Overview

Custom JSON emitter that preserves comments and formatting. This class provides utilities for emitting JSON while maintaining the original structure, comments, and style choices.

Inherits common emitter functionality from Ast::Merge::EmitterBase.

Examples:

Basic usage

emitter = Emitter.new
emitter.emit_object_start
emitter.emit_pair("key", '"value"')
emitter.emit_object_end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#needs_commaBoolean (readonly)

Returns Whether next item needs a comma.

Returns:

  • (Boolean)

    Whether next item needs a comma



20
21
22
# File 'lib/json/merge/emitter.rb', line 20

def needs_comma
  @needs_comma
end

Instance Method Details

#clear_subclass_stateObject

Clear subclass-specific state



29
30
31
32
# File 'lib/json/merge/emitter.rb', line 29

def clear_subclass_state
  @needs_comma = false
  
end

#emit_array_element(value, inline_comment: nil, metadata: nil) ⇒ Object

Emit an array element

Parameters:

  • value (String)

    Value (already formatted)

  • inline_comment (String, nil) (defaults to: nil)

    Optional inline comment



127
128
129
130
131
132
133
# File 'lib/json/merge/emitter.rb', line 127

def emit_array_element(value, inline_comment: nil, metadata: nil)
  add_comma_if_needed
  line = "#{current_indent}#{value}"
  line += " // #{inline_comment}" if inline_comment
  append_line(line, )
  @needs_comma = true
end

#emit_array_end(metadata: nil) ⇒ Object

Emit array end



104
105
106
107
108
# File 'lib/json/merge/emitter.rb', line 104

def emit_array_end(metadata: nil)
  dedent
  append_line("#{current_indent}]", )
  @needs_comma = true
end

#emit_array_start(key = nil, inline_comment: nil, metadata: nil) ⇒ Object

Emit array start

Parameters:

  • key (String, nil) (defaults to: nil)

    Key name if this array is a value in an object

  • inline_comment (String, nil) (defaults to: nil)

    Optional inline comment for the opening line



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/json/merge/emitter.rb', line 90

def emit_array_start(key = nil, inline_comment: nil, metadata: nil)
  add_comma_if_needed
  line = if key
           "#{current_indent}\"#{key}\": ["
         else
           "#{current_indent}["
         end
  line += " // #{inline_comment}" if inline_comment
  append_line(line, )
  indent
  @needs_comma = false
end

#emit_blank_lineObject



34
35
36
# File 'lib/json/merge/emitter.rb', line 34

def emit_blank_line
  append_line('')
end

#emit_block_comment(text) ⇒ Object

Emit a block comment

Parameters:

  • text (String)

    Comment text



67
68
69
# File 'lib/json/merge/emitter.rb', line 67

def emit_block_comment(text)
  append_line("#{current_indent}/* #{text} */")
end

#emit_comment(text, inline: false) ⇒ Object

Emit a single-line comment

Parameters:

  • text (String)

    Comment text (without //)

  • inline (Boolean) (defaults to: false)

    Whether this is an inline comment



53
54
55
56
57
58
59
60
61
62
# File 'lib/json/merge/emitter.rb', line 53

def emit_comment(text, inline: false)
  if inline
    # Inline comments are appended to the last line
    return if @lines.empty?

    @lines[-1] = "#{@lines[-1]} // #{text}"
  else
    append_line("#{current_indent}// #{text}")
  end
end

#emit_nested_object_end(metadata: nil) ⇒ Object

Emit closing brace for nested object



148
149
150
151
152
# File 'lib/json/merge/emitter.rb', line 148

def emit_nested_object_end(metadata: nil)
  dedent
  append_line("#{current_indent}}", )
  @needs_comma = true
end

#emit_nested_object_start(key, inline_comment: nil, metadata: nil) ⇒ Object

Emit a key with opening brace for nested object

Parameters:

  • key (String)

    Key name

  • inline_comment (String, nil) (defaults to: nil)

    Optional inline comment for the opening line



138
139
140
141
142
143
144
145
# File 'lib/json/merge/emitter.rb', line 138

def emit_nested_object_start(key, inline_comment: nil, metadata: nil)
  add_comma_if_needed
  line = "#{current_indent}\"#{key}\": {"
  line += " // #{inline_comment}" if inline_comment
  append_line(line, )
  indent
  @needs_comma = false
end

#emit_object_end(metadata: nil) ⇒ Object

Emit object end



80
81
82
83
84
# File 'lib/json/merge/emitter.rb', line 80

def emit_object_end(metadata: nil)
  dedent
  append_line("#{current_indent}}", )
  @needs_comma = true
end

#emit_object_start(metadata: nil) ⇒ Object

Emit object start



72
73
74
75
76
77
# File 'lib/json/merge/emitter.rb', line 72

def emit_object_start(metadata: nil)
  add_comma_if_needed
  append_line("#{current_indent}{", )
  indent
  @needs_comma = false
end

#emit_pair(key, value, inline_comment: nil, metadata: nil) ⇒ Object

Emit a key-value pair

Parameters:

  • key (String)

    Key name (without quotes)

  • value (String)

    Value (already formatted, e.g., '"string"', '123', 'true')

  • inline_comment (String, nil) (defaults to: nil)

    Optional inline comment



115
116
117
118
119
120
121
# File 'lib/json/merge/emitter.rb', line 115

def emit_pair(key, value, inline_comment: nil, metadata: nil)
  add_comma_if_needed
  line = "#{current_indent}\"#{key}\": #{value}"
  line += " // #{inline_comment}" if inline_comment
  append_line(line, )
  @needs_comma = true
end

#emit_raw_fragment(fragment, metadata: nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/json/merge/emitter.rb', line 163

def emit_raw_fragment(fragment, metadata: nil)
  add_comma_if_needed
  fragment.to_s.lines(chomp: true).each_with_index do |line, idx|
    append_line(
      "#{current_indent}#{line}",
      (, idx)
    )
  end
  @needs_comma = true
end

#emit_raw_lines(raw_lines, metadata: nil) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/json/merge/emitter.rb', line 154

def emit_raw_lines(raw_lines, metadata: nil)
  raw_lines.each_with_index do |line, idx|
    append_line(
      line.chomp,
      (, idx)
    )
  end
end

#emit_tracked_comment(comment) ⇒ Object

Emit a tracked comment from CommentTracker

Parameters:

  • comment (Hash)

    Comment with :text, :indent, :block



40
41
42
43
44
45
46
47
# File 'lib/json/merge/emitter.rb', line 40

def emit_tracked_comment(comment)
  indent = ' ' * (comment[:indent] || 0)
  append_line(if comment[:block]
                "#{indent}/* #{comment[:text]} */"
              else
                "#{indent}// #{comment[:text]}"
              end)
end

#initialize_subclass_state(**_options) ⇒ Object

Initialize subclass-specific state (comma tracking for JSON)



23
24
25
26
# File 'lib/json/merge/emitter.rb', line 23

def initialize_subclass_state(**_options)
  @needs_comma = false
  
end

#to_json(*_args) ⇒ String

Get the output as a JSON string

Returns:

  • (String)


177
178
179
# File 'lib/json/merge/emitter.rb', line 177

def to_json(*_args)
  to_s
end