Class: Json::Merge::Emitter
- Inherits:
-
Ast::Merge::EmitterBase
- Object
- Ast::Merge::EmitterBase
- Json::Merge::Emitter
- 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.
Instance Attribute Summary collapse
-
#needs_comma ⇒ Boolean
readonly
Whether next item needs a comma.
Instance Method Summary collapse
-
#clear_subclass_state ⇒ Object
Clear subclass-specific state.
-
#emit_array_element(value, inline_comment: nil) ⇒ Object
Emit an array element.
-
#emit_array_end ⇒ Object
Emit array end.
-
#emit_array_start(key = nil) ⇒ Object
Emit array start.
-
#emit_block_comment(text) ⇒ Object
Emit a block comment.
-
#emit_comment(text, inline: false) ⇒ Object
Emit a single-line comment.
-
#emit_nested_object_end ⇒ Object
Emit closing brace for nested object.
-
#emit_nested_object_start(key) ⇒ Object
Emit a key with opening brace for nested object.
-
#emit_object_end ⇒ Object
Emit object end.
-
#emit_object_start ⇒ Object
Emit object start.
-
#emit_pair(key, value, inline_comment: nil) ⇒ Object
Emit a key-value pair.
-
#emit_tracked_comment(comment) ⇒ Object
Emit a tracked comment from CommentTracker.
-
#initialize_subclass_state(**options) ⇒ Object
Initialize subclass-specific state (comma tracking for JSON).
-
#to_json ⇒ String
Get the output as a JSON string.
Instance Attribute Details
#needs_comma ⇒ Boolean (readonly)
Returns 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_state ⇒ Object
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
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_end ⇒ Object
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
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
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
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_end ⇒ Object
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
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_end ⇒ Object
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_start ⇒ Object
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
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
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(**) @needs_comma = false end |
#to_json ⇒ String
Get the output as a JSON string
143 144 145 |
# File 'lib/json/merge/emitter.rb', line 143 def to_json to_s end |