class Composer::Json::JsonFormatter

* Formats json strings used for php < 5.4 because the json_encode doesn't
* supports the flags JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
* in these versions

PHP Authors: Konstantin Kudryashiv <ever.zet@gmail.com> Jordi Boggiano <j.boggiano@seld.be>

Ruby Authors: Ioannis Kappas <ikappas@devworks.gr>

Constants

JSON_FORCE_OBJECT
JSON_HEX_AMP
JSON_HEX_APOS
JSON_HEX_QUOT
JSON_HEX_TAG
JSON_NUMERIC_CHECK
JSON_PRETTY_PRINT
JSON_UNESCAPED_SLASHES
JSON_UNESCAPED_UNICODE

Public Class Methods

format(json, options) click to toggle source

This code is based on the function found at: recursive-design.com/blog/2008/03/11/format-json-with-php/

Originally licensed under MIT by Dave Perrett <mail@recursive-design.com>

@param json string @param unescape_unicode bool Un escape unicode @param unescape_slashes bool Un escape slashes @return string

# File lib/composer/json/json_formatter.rb, line 56
def format(json, options)

  result = ''
  pos = 0
  str_len = json.length
  indent_str = '    '
  new_line = "\n"
  out_of_quotes = true
  buffer = ''
  no_escape = true

  for i in 0..(str_len - 1)

    # Grab the next character in the string
    char = json[i]

    # Are we inside a quoted string?
    if '"' === char && no_escape
      out_of_quotes = !out_of_quotes
    end

    if !out_of_quotes
      buffer << char
      no_escape = '\\' === char ? !no_escape : true
      next
    elsif buffer != ''
      if options & JSON_HEX_TAG === JSON_HEX_TAG
        buffer.gsub!('<', '\\u003C')
        buffer.gsub!('>', '\\u003E')
      end
      if options & JSON_HEX_AMP === JSON_HEX_AMP
        buffer.gsub!('&', '\\u0026')
      end
      if options & JSON_HEX_APOS === JSON_HEX_APOS
        buffer.gsub!('\'', '\\u0027')
      end
      if options & JSON_HEX_QUOT === JSON_HEX_QUOT
        buffer.gsub!('\"', '\\u0022')
      end
      if options & JSON_UNESCAPED_SLASHES === JSON_UNESCAPED_SLASHES
        buffer.gsub!('\\/', '/')
      end
      if options & JSON_UNESCAPED_UNICODE === JSON_UNESCAPED_UNICODE
        buffer.gsub!(/\\u([\da-fA-F]{4})/) {|m| [$1].pack('H*').unpack('n*').pack('U*')}
      end

      result << buffer + char
      buffer = ''
      next
    end

    if options & JSON_PRETTY_PRINT === JSON_PRETTY_PRINT
      if char === ':'
      # Add a space after the : character
      char << ' '
      elsif char === '}' || char === ']'
        pos -= 1
        prev_char = json[i - 1] #substr(json, i - 1, 1)

        if prev_char != '{' &&  prev_char != '['
          # If this character is the end of an element,
          # output a new line and indent the next line
          result << new_line

          for j in 0..(pos - 1)
            result << indent_str
          end
        else
          # Collapse empty {} and []
          result.rstrip!
        end
      end
    end

    result << char

    if options & JSON_PRETTY_PRINT === JSON_PRETTY_PRINT
      # If the last character was the beginning of an element,
      # output a new line and indent the next line
      if char === ',' || char === '{' || char === '['
        result << new_line
        pos += 1 if char === '{' || char === '['
        for j in 0..(pos - 1)
          result << indent_str
        end
      end
    end
  end

  result
end
unescape_slashes(s) click to toggle source
# File lib/composer/json/json_formatter.rb, line 148
def unescape_slashes(s)
  s.gsub('\\/', '/')
end
unescape_unicode(s) click to toggle source
# File lib/composer/json/json_formatter.rb, line 152
def unescape_unicode(s)
  s.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack('H*').unpack('n*').pack('U*')}
end