Class: JSON::Next::Commata

Inherits:
Object
  • Object
show all
Defined in:
lib/json/next/commata.rb

Overview

auto-add commas for objects and arrays

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(str, opts = {}) ⇒ Object

convenience helper



12
13
14
# File 'lib/json/next/commata.rb', line 12

def self.convert( str, opts={} )
   self.new.convert( str, opts )
end

Instance Method Details

#convert(str, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/json/next/commata.rb', line 17

def convert( str, opts={} )
   @debug  = opts.fetch( :debug, false )
   @out    = ""
   @buffer = StringScanner.new( str )

   skip_whitespaces
   parse_value
   @out
end

#debug?Boolean

Returns:

  • (Boolean)


28
# File 'lib/json/next/commata.rb', line 28

def debug?() @debug; end

#parse_arrayObject



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
# File 'lib/json/next/commata.rb', line 105

def parse_array

 if @buffer.peek(1) == '['
     @buffer.getch   # consume '['
     @out << ' [ '
     skip_whitespaces

    if @buffer.peek(1) == ']'    ## empty array?
      @buffer.getch   # consume ']'
      @out << ' ] '
      skip_whitespaces
      return
    end

    loop do
         parse_value
         if @buffer.peek(1) == ']'
           @buffer.getch   # consume ']'
           @out << ' ] '
           skip_whitespaces
           return   ## use break - why? why not?
         else
           if @buffer.peek(1) == ','
              @buffer.getch   # consume ','
              @out << ' , '
              skip_whitespaces
           else
             puts "array literal - auto-add comma for value" if debug?
             @out << ' , '
           end
         end
    end
 else
     puts "!! format error: array literal - expected square open bracket ([) - rest is >>#{@buffer.rest}<<"
 end
end

#parse_objectObject



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
# File 'lib/json/next/commata.rb', line 57

def parse_object

 if @buffer.peek(1) == '{'
     @buffer.getch   # consume '{'
     @out << ' { '
     skip_whitespaces

    if @buffer.peek(1) == '}'    ## empty object?
      @buffer.getch   # consume '{'
      @out << ' } '
      skip_whitespaces
      return
    end

    loop do
      parse_string
      if @buffer.peek(1) == ':'
         @buffer.getch   # consume ':'
         @out << ' : '
         skip_whitespaces

         parse_value
         if @buffer.peek(1) == '}'
           @buffer.getch   # consume '}'
           @out << ' } '
           skip_whitespaces
           return   ## use break - why? why not?
         else
           if @buffer.peek(1) == ','
              @buffer.getch   # consume ','
              @out << ' , '
              skip_whitespaces
           else
             puts "object literal - auto-add comma for key-value pair" if debug?
             @out << ' , '
           end
         end
      else
        puts "!! format error: object literal - expected colon (:) - rest is >>#{@buffer.rest}<<"
      end
    end
 else
     puts "!! format error: object literal - expected curly open bracket ({) - rest is >>#{@buffer.rest}<<"
 end
end

#parse_stringObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/json/next/commata.rb', line 37

def parse_string

   if @buffer.peek(1) == '"'   ## double quote
       @buffer.getch  # consume double quote
       value = @buffer.scan( /(\\.|[^"])*/ )
       @buffer.getch  # consume double quote

       puts %{string value >>#{value}<<} if debug?
       @out << ' "'
       @out << value
       @out << '" '

       skip_whitespaces
   else
     puts "!! format error: string literal - expected opening quote (\") - rest is >>#{@buffer.rest}<<"
   end
end

#parse_valueObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/json/next/commata.rb', line 144

def parse_value
  if @buffer.peek(1) == '{'
     parse_object
  elsif @buffer.peek(1) == '['
     parse_array
  elsif @buffer.peek(1) == '"'
     parse_string
  else
     ## assume number or literal/identifier
    value = @buffer.scan( /[_$a-zA-Z0-9.+\-]+/ )
    puts %{literal value >>#{value}<<}  if debug?
    @out << " "
    @out << value
    @out << " "

    skip_whitespaces
  end

  ## todo/fix: check if eof reached ?? if not report warning - more data available??
  ##  wrap in object ({}) or array ([])
end

#skip_whitespacesObject



32
33
34
# File 'lib/json/next/commata.rb', line 32

def skip_whitespaces
  @buffer.skip( /[ \t\n]*/ )    ## skip trailing WHITESPACE
end