Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow :indentation to be configured for JSON output.

The :indentation parameter should be a whitespace string, and defaults to two spaces.
Setting it to nil/false disables formatting.
  • Loading branch information...
commit 8d6d5c03f59fbc6cba6f480ff38ee82ad7409d24 1 parent 3f21874
@mdub authored
Showing with 68 additions and 41 deletions.
  1. +19 −8 lib/representative/json.rb
  2. +49 −33 spec/representative/json_spec.rb
View
27 lib/representative/json.rb
@@ -7,12 +7,14 @@ module Representative
class Json < Base
DEFAULT_ATTRIBUTE_PREFIX = "@".freeze
+ DEFAULT_INDENTATION = " " # two spaces
def initialize(subject = nil, options = {})
super(subject, options)
@buffer = []
@indent_level = 0
- @attribute_prefix = options[:attribute_prefix] || DEFAULT_ATTRIBUTE_PREFIX
+ @attribute_prefix = options.fetch(:attribute_prefix, DEFAULT_ATTRIBUTE_PREFIX)
+ @indentation = options.fetch(:indentation, DEFAULT_INDENTATION)
now_at :beginning_of_buffer
yield self if block_given?
end
@@ -82,7 +84,7 @@ def comment(text)
end
def to_json
- @buffer << "\n"
+ emit("\n") if indenting?
@buffer.join
end
@@ -100,20 +102,27 @@ def encode(data)
ActiveSupport::JSON.encode(data)
end
- def indentation
- (" " * @indent_level)
+ def indenting?
+ !!@indentation
+ end
+
+ def current_indentation
+ (@indentation * @indent_level)
end
def label(name)
return false if @indent_level == 0
new_item
- emit(format_name(name).inspect + ": ")
+ emit(format_name(name).inspect + ":")
+ emit(" ") if indenting?
end
def new_item
emit(",") if at? :end_of_item
- emit("\n") unless at? :beginning_of_buffer
- emit(indentation)
+ if indenting?
+ emit("\n") unless at? :beginning_of_buffer
+ emit(current_indentation)
+ end
@pending_comma = ","
end
@@ -123,7 +132,9 @@ def inside(opening_char, closing_char)
now_at :beginning_of_block
yield
@indent_level -= 1
- emit("\n" + indentation) unless at? :beginning_of_block
+ if indenting?
+ emit("\n" + current_indentation) unless at? :beginning_of_block
+ end
emit(closing_char)
now_at :end_of_item
end
View
82 spec/representative/json_spec.rb
@@ -33,9 +33,9 @@ def resulting_json
end
end
-
+
describe "with a nil value" do
-
+
it "generates null" do
r.element :flavour, nil
resulting_json.should == "null\n"
@@ -51,13 +51,13 @@ def resulting_json
end
end
-
+
end
-
+
describe "with attributes" do
-
+
describe "and a block" do
-
+
it "generates labelled fields for the attributes" do
@book = OpenStruct.new(:lang => "fr", :title => "En Edge")
r.element :book, @book, :lang => :lang do
@@ -70,20 +70,20 @@ def resulting_json
}
JSON
end
-
+
end
describe "and an explicit value" do
-
+
it "ignores the attributes" do
r.element :review, "Blah de blah", :lang => "fr"
resulting_json.should == undent(<<-JSON)
"Blah de blah"
JSON
end
-
+
end
-
+
end
describe "without an explicit value" do
@@ -144,7 +144,7 @@ def resulting_json
}
JSON
end
-
+
end
describe "with a block" do
@@ -176,9 +176,9 @@ def resulting_json
]
JSON
end
-
+
end
-
+
describe "with item attributes" do
it "adds the attributes with an @ sign to child elements" do
@authors = [
@@ -198,7 +198,7 @@ def resulting_json
"age": 3
},
{
- "@about": "Dewey is 4 years old",
+ "@about": "Dewey is 4 years old",
"name": "Dewey",
"age": 4
},
@@ -211,19 +211,19 @@ def resulting_json
JSON
end
end
-
+
describe "with list attributes" do
it "raises an ArgumentError" do
@authors = []
lambda{ r.list_of(:authors, @authors, :list_attributes => {}) {} }.should raise_exception(ArgumentError)
end
end
-
+
describe "with unnecessary arguments" do
it "raises an ArgumentError" do
@authors = []
- lambda{
- r.list_of(:authors, @authors, :unecessary_arg_should_cause_failure, :item_attributes => {}){}
+ lambda{
+ r.list_of(:authors, @authors, :unecessary_arg_should_cause_failure, :item_attributes => {}){}
}.should raise_exception(ArgumentError)
end
end
@@ -231,16 +231,16 @@ def resulting_json
end
describe "#comment" do
-
+
it "inserts a comment" do
r.comment "now pay attention"
resulting_json.should == undent(<<-JSON)
// now pay attention
JSON
end
-
+
end
-
+
end
describe "within an element block" do
@@ -269,7 +269,7 @@ def resulting_json
r.element :age
end
resulting_json.should == undent(<<-JSON)
- {
+ {
"name": "Fred",
"age": 36
}
@@ -281,7 +281,7 @@ def resulting_json
end
describe "#attribute" do
-
+
it "generates labelled values, with a label prefix" do
r.element :author, Object.new do
r.attribute :href, "http://example.com/authors/1"
@@ -294,11 +294,11 @@ def resulting_json
}
JSON
end
-
+
end
-
+
describe "#comment" do
-
+
it "inserts a comment" do
@author = OpenStruct.new(:name => "Fred", :age => 36)
r.element :author, @author do
@@ -307,33 +307,33 @@ def resulting_json
r.element :age
end
resulting_json.should == undent(<<-JSON)
- {
+ {
"name": "Fred",
// age is irrelevant
"age": 36
}
JSON
end
-
+
end
-
+
end
context "by default" do
-
+
it "does not tranform element names" do
r.element :user, Object.new do
r.element :full_name, "Fred Bloggs"
end
resulting_json.should == undent(<<-JSON)
- {
+ {
"full_name": "Fred Bloggs"
}
JSON
end
end
-
+
context "with naming_strategy :camelcase" do
it "generates camelCased element and attribute names" do
@@ -343,7 +343,7 @@ def resulting_json
r.element :full_name
end
resulting_json.should == undent(<<-JSON)
- {
+ {
"@altUrl": "http://xyz.com",
"fullName": "Fred Bloggs"
}
@@ -352,4 +352,20 @@ def resulting_json
end
+ context "with indentation false" do
+
+ it "does not indent, or wrap" do
+ @authors = [
+ OpenStruct.new(:name => "Hewey", :age => 3),
+ OpenStruct.new(:name => "Dewey", :age => 4)
+ ]
+ r(:indentation => false).list_of :authors, @authors do
+ r.element :name
+ r.element :age
+ end
+ resulting_json.should == %([{"name":"Hewey","age":3},{"name":"Dewey","age":4}])
+ end
+
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.