Module: JSONX

Defined in:
lib/json/next/parser/jsonx.rb

Constant Summary collapse

BACKTICK_ML_QUOTE =
JSON::Next::BACKTICK_ML_QUOTE
SINGLE_QUOTE =
JSON::Next::SINGLE_QUOTE
DOUBLE_QUOTE =
JSON::Next::DOUBLE_QUOTE
CLANG_ML_COMMENT =
JSON::Next::CLANG_ML_COMMENT
CLANG_COMMENT =
JSON::Next::CLANG_COMMENT
SHELL_COMMENT =
JSON::Next::SHELL_COMMENT
KEYWORDS =
JSON::Next::KEYWORDS
IDENTIFIER =
JSON::Next::IDENTIFIER_EXTENDED
TRAILING_COMMA =
JSON::Next::TRAILING_COMMA
UNESCAPE_MAP =
JSON::Next::UNESCAPE_MAP
ML_ESCAPE_MAP =
JSON::Next::ML_ESCAPE_MAP

Class Method Summary collapse

Class Method Details

.convert(text) ⇒ Object

[View source]

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
# File 'lib/json/next/parser/jsonx.rb', line 68

def self.convert( text )

  ## pass 1: remove/strip comments

  text = strip_comments( text )

  ## pass 2: requote/normalize quotes

  text = normalize_quotes( text )

  ## pass 3: quote unquoted and remove trailing commas

  text = text.gsub( /#{KEYWORDS}|#{IDENTIFIER}|#{DOUBLE_QUOTE}|#{TRAILING_COMMA}/ox ) do |match|
     ## puts "match: >>#{match}<< : #{match.class.name}"


     m = Regexp.last_match
     if m[:identifier]
       ## puts "!!! identfier -- wrap in double quotes"

       '"' + m[:identifier] + '"'
     elsif m[:trailing_comma]
       ## puts "!!! trailing comma -- remove"

       ''
     else
       match
     end
  end

  ## pass 4 - auto-add (missing optional) commas

  text = JSON::Next::Commata.convert( text )
  text
end

.normalize_quotes(text) ⇒ Object

note: same as hanson (see parser/hanson.rb)

[View source]

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/json/next/parser/jsonx.rb', line 44

def self.normalize_quotes( text )  ## pass 2

   text.gsub( /#{BACKTICK_ML_QUOTE}|#{SINGLE_QUOTE}|#{DOUBLE_QUOTE}/ox ) do |match|
     ## puts "match: >>#{match}<< : #{match.class.name}"


     m = Regexp.last_match
     if m[:backtick_ml_quote]
       ## puts "!!! ml_quote -- convert to double quotes"

       str = m[:backtick_ml_quote]
       str = str.gsub( /\\./ ) {|r| UNESCAPE_MAP[r] || r }
       str = str.gsub( /[\n\r\t"]/ ) { |r| ML_ESCAPE_MAP[r] }
       '"' + str + '"'
     elsif m[:single_quote]
       ## puts "!!! single_quote -- convert to double quotes"

       str = m[:single_quote]
       str = str.gsub( /\\./ ) {|r| UNESCAPE_MAP[r] || r }
       str = str.gsub( /"/, %{\\"} )
       '"' + str + '"'
     else
       match
     end
  end
end

.parse(text) ⇒ Object

[View source]

98
99
100
# File 'lib/json/next/parser/jsonx.rb', line 98

def self.parse( text )
  JSON.parse( self.convert( text ) )
end

.strip_comments(text) ⇒ Object

pass 1

[View source]

30
31
32
33
34
35
36
37
38
39
40
# File 'lib/json/next/parser/jsonx.rb', line 30

def self.strip_comments( text )   ## pass 1

  text.gsub( /#{BACKTICK_ML_QUOTE}|#{SINGLE_QUOTE}|#{DOUBLE_QUOTE}|#{SHELL_COMMENT}|#{CLANG_ML_COMMENT}|#{CLANG_COMMENT}/ox ) do |match|
     ## puts "match: >>#{match}<< : #{match.class.name}"

     if match[0] == ?/ ||  match[0] == ?#    ## comments start with // or /* or  #

       ## puts "!!! removing comments"

       ''    ## remove / strip comments

     else
       match
     end
   end
end