Class: Composer::Json::JsonFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/composer/json/json_formatter.rb

Overview

  • 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 <[email protected]> Jordi Boggiano <[email protected]>

Ruby Authors: Ioannis Kappas <[email protected]>

Constant Summary collapse

JSON_HEX_TAG =
1
JSON_HEX_AMP =
2
JSON_HEX_APOS =
4
JSON_HEX_QUOT =
8
JSON_FORCE_OBJECT =
16
JSON_NUMERIC_CHECK =
32
JSON_UNESCAPED_SLASHES =
64
JSON_PRETTY_PRINT =
128
JSON_UNESCAPED_UNICODE =
256

Class Method Summary collapse

Class Method Details

.format(json, options) ⇒ Object

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 <[email protected]>

Parameters:

  • json

    string

  • unescape_unicode

    bool Un escape unicode

  • unescape_slashes

    bool Un escape slashes

Returns:

  • string



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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) ⇒ Object



148
149
150
# File 'lib/composer/json/json_formatter.rb', line 148

def unescape_slashes(s)
  s.gsub('\\/', '/')
end

.unescape_unicode(s) ⇒ Object



152
153
154
# 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