Sha256: 1b89d0e0fa02b673bb082bf3a6e9648a85ccc65c4a8d75419893da17224102a5

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

# File: json_minifier.rb


# A JSON minifier, it removes unnecessary whitespaces in a JSON expression.
# It typically reduces size by half.
class JSONMinifier
  # The IO output stream in which the formatter's result will be sent.
  attr_reader(:output)

  # Constructor.
  # @param anIO [IO] an output IO where the formatter's result will
  # be placed.
  def initialize(anIO)
    @output = anIO
  end

  # Given a parse tree visitor, perform the visit
  # and render the visit events in the output stream.
  # @param aVisitor [ParseTreeVisitor]
  def render(aVisitor)
    aVisitor.subscribe(self)
    aVisitor.start
    aVisitor.unsubscribe(self)
  end

  # Method called by a ParseTreeVisitor to which the formatter subscribed.
  # Notification of a visit event: the visitor is about to visit
  # a terminal node. The only thing the JSON minifier has to do is
  # to render the input tokens almost as they appear initially.
  # @param aTerm [TerminalNode]
  def before_terminal(aTerm)
    # Lexeme is the original text representation of the token
    lexeme = aTerm.token.lexeme
    literal = if aTerm.symbol.name == 'string'
                # String values are delimited by double quotes
                '"' + lexeme + '"'
              else
                lexeme
              end

    output << literal
  end
end # class

# End of file

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rley-0.7.07 examples/data_formats/JSON/json_minifier.rb
rley-0.7.06 examples/data_formats/JSON/json_minifier.rb
rley-0.7.05 examples/data_formats/JSON/json_minifier.rb
rley-0.7.04 examples/data_formats/JSON/json_minifier.rb