Class: Json::Merge::Emitter

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



18
19
20
# File 'lib/json/merge/emitter.rb', line 18

def needs_comma
  @needs_comma
end

Instance Method Details

#clear_subclass_stateObject

Clear subclass-specific state



26
27
28
# File 'lib/json/merge/emitter.rb', line 26

def clear_subclass_state
  @needs_comma = false
end

#emit_array_element(value, inline_comment: nil) ⇒ Object

Emit an array element

Parameters:

  • value (String)

    Value (already formatted)

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

    Optional inline comment



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

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

#emit_array_endObject

Emit array end



93
94
95
96
97
# File 'lib/json/merge/emitter.rb', line 93

def emit_array_end
  dedent
  @lines << "#{current_indent}]"
  @needs_comma = true
end

#emit_array_start(key = nil) ⇒ Object

Emit array start

Parameters:

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

    Key name if this array is a value in an object



81
82
83
84
85
86
87
88
89
90
# File 'lib/json/merge/emitter.rb', line 81

def emit_array_start(key = nil)
  add_comma_if_needed
  @lines << if key
    "#{current_indent}\"#{key}\": ["
  else
    "#{current_indent}["
  end
  indent
  @needs_comma = false
end

#emit_block_comment(text) ⇒ Object

Emit a block comment

Parameters:

  • text (String)

    Comment text



59
60
61
# File 'lib/json/merge/emitter.rb', line 59

def emit_block_comment(text)
  @lines << "#{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



45
46
47
48
49
50
51
52
53
54
# File 'lib/json/merge/emitter.rb', line 45

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
    @lines << "#{current_indent}// #{text}"
  end
end

#emit_nested_object_endObject

Emit closing brace for nested object



134
135
136
137
138
# File 'lib/json/merge/emitter.rb', line 134

def emit_nested_object_end
  dedent
  @lines << "#{current_indent}}"
  @needs_comma = true
end

#emit_nested_object_start(key) ⇒ Object

Emit a key with opening brace for nested object

Parameters:

  • key (String)

    Key name



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

def emit_nested_object_start(key)
  add_comma_if_needed
  @lines << "#{current_indent}\"#{key}\": {"
  indent
  @needs_comma = false
end

#emit_object_endObject

Emit object end



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

def emit_object_end
  dedent
  @lines << "#{current_indent}}"
  @needs_comma = true
end

#emit_object_startObject

Emit object start



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

def emit_object_start
  add_comma_if_needed
  @lines << "#{current_indent}{"
  indent
  @needs_comma = false
end

#emit_pair(key, value, inline_comment: 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



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

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

#emit_tracked_comment(comment) ⇒ Object

Emit a tracked comment from CommentTracker

Parameters:

  • comment (Hash)

    Comment with :text, :indent, :block



32
33
34
35
36
37
38
39
# File 'lib/json/merge/emitter.rb', line 32

def emit_tracked_comment(comment)
  indent = " " * (comment[:indent] || 0)
  @lines << 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)



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

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

#to_jsonString

Get the output as a JSON string

Returns:

  • (String)


143
144
145
# File 'lib/json/merge/emitter.rb', line 143

def to_json
  to_s
end