Module: Locomotive::Steam::Liquid::Filters::Json

Defined in:
lib/locomotive/steam/liquid/filters/json.rb

Instance Method Summary collapse

Instance Method Details

#json(input, fields = []) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/locomotive/steam/liquid/filters/json.rb', line 7

def json(input, fields = [])
  if fields && fields.is_a?(String)
    fields = fields.split(',').map(&:strip)
  end

  if input.respond_to?(:each)
    '[' + input.map do |object|
      fields.size == 1 ? object[fields.first].to_json : object_to_json(object, fields)
    end.join(',') + ']'
  else
    object_to_json(input, fields)
  end
end

#open_json(input) ⇒ Object

without the leading and trailing braces/brackets useful to add a prperty to an object or an element to an array



23
24
25
26
27
28
29
# File 'lib/locomotive/steam/liquid/filters/json.rb', line 23

def open_json(input)
  if input =~ /\A[\{\[](.*)[\}\]]\Z/m
    $1
  else
    input
  end
end