Skip to content

Commit

Permalink
Merge remote-tracking branches 'origin/doc' and 'origin/no_stringify_…
Browse files Browse the repository at this point in the history
…base' into HEAD

#11
#12
  • Loading branch information
notEthan committed Sep 19, 2018
2 parents 98d80b3 + b1fcafe commit 1b5f459
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -177,7 +177,7 @@ Now `user.contacts` will return an array of Contact instances, from the json typ

## Keying Hashes (JSON Objects)

Unlike Ruby, JSON only supports string keys. JSI converts symbols to strings for its internal hash keys (much like ActiveSupport::HashWithIndifferentAccess). JSI accepts symbols to refer to its string hash keys for instantiation, but does not currently transform symbols to strings everywhere else, e.g. `bill[:name]` is `nil` whereas `bill['name']` is `"bill"`.
Unlike Ruby, JSON only supports string keys. It is recommended to use strings as hash keys for all JSI instances, but JSI does not enforce this, nor does it do any key conversion. It should be possible to use ActiveSupport::HashWithIndifferentAccess as the instance of a JSI in order to gain the benefits that offers over a plain hash. This is not tested behavior, but JSI should behave correctly with any instance that responds to #to_hash.

## Contributing

Expand Down
6 changes: 3 additions & 3 deletions lib/jsi/base.rb
Expand Up @@ -209,11 +209,11 @@ def instance=(thing)
end
if thing.is_a?(Base)
warn "assigning instance to a Base instance is incorrect. received: #{thing.pretty_inspect.chomp}"
@instance = JSI.deep_stringify_symbol_keys(thing.instance)
@instance = thing.instance
elsif thing.is_a?(JSI::JSON::Node)
@instance = JSI.deep_stringify_symbol_keys(thing)
@instance = thing
else
@instance = JSI::JSON::Node.new_doc(JSI.deep_stringify_symbol_keys(thing))
@instance = JSI::JSON::Node.new_doc(thing)
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/base_test.rb
Expand Up @@ -168,8 +168,8 @@
end
end
describe '#parents, #parent' do
let(:schema_content) { {properties: {foo: {properties: {bar: {properties: {baz: {}}}}}}} }
let(:document) { {foo: {bar: {baz: {}}}} }
let(:schema_content) { {'properties' => {'foo' => {'properties' => {'bar' => {'properties' => {'baz' => {}}}}}}} }
let(:document) { {'foo' => {'bar' => {'baz' => {}}}} }
describe 'no parents' do
it 'has none' do
assert_equal([], subject.parents)
Expand Down
8 changes: 4 additions & 4 deletions test/schema_instance_json_coder_test.rb
Expand Up @@ -10,7 +10,7 @@
assert_nil(schema_instance_json_coder.load(nil))
end
it 'loads a hash' do
assert_equal(schema_instance_class.new(foo: 'bar'), schema_instance_json_coder.load({"foo" => "bar"}))
assert_equal(schema_instance_class.new('foo' => 'bar'), schema_instance_json_coder.load({"foo" => "bar"}))
end
it 'loads something else' do
assert_equal(schema_instance_class.new([[]]), schema_instance_json_coder.load([[]]))
Expand All @@ -19,7 +19,7 @@
let(:options) { {array: true} }
it 'loads an array of hashes' do
data = [{"foo" => "bar"}, {"foo" => "baz"}]
assert_equal([schema_instance_class.new(foo: 'bar'), schema_instance_class.new(foo: 'baz')], schema_instance_json_coder.load(data))
assert_equal([schema_instance_class.new('foo' => 'bar'), schema_instance_class.new('foo' => 'baz')], schema_instance_json_coder.load(data))
end
it 'loads an empty array' do
assert_equal([], schema_instance_json_coder.load([]))
Expand Down Expand Up @@ -65,7 +65,7 @@
assert_nil(schema_instance_json_coder.load(nil))
end
it 'loads a hash' do
assert_equal(schema_instance_class.new(foo: 'bar'), schema_instance_json_coder.load('{"foo": "bar"}'))
assert_equal(schema_instance_class.new('foo' => 'bar'), schema_instance_json_coder.load('{"foo": "bar"}'))
end
it 'loads something else' do
assert_equal(schema_instance_class.new([[]]), schema_instance_json_coder.load('[[]]'))
Expand All @@ -79,7 +79,7 @@
let(:options) { {string: true, array: true} }
it 'loads an array of hashes' do
data = '[{"foo": "bar"}, {"foo": "baz"}]'
assert_equal([schema_instance_class.new(foo: 'bar'), schema_instance_class.new(foo: 'baz')], schema_instance_json_coder.load(data))
assert_equal([schema_instance_class.new('foo' => 'bar'), schema_instance_class.new('foo' => 'baz')], schema_instance_json_coder.load(data))
end
it 'loads an empty array' do
assert_equal([], schema_instance_json_coder.load('[]'))
Expand Down
8 changes: 4 additions & 4 deletions test/util_test.rb
Expand Up @@ -10,7 +10,7 @@
expected = JSI::JSON::HashNode.new({'a' => 'b', 'c' => 'd', nil => 3}, [])
assert_equal(expected, actual)
end
it 'stringifies SchemaObjectBase hash keys' do
it 'stringifies JSI hash keys' do
klass = JSI.class_for_schema(type: 'object')
expected = JSI.stringify_symbol_keys(klass.new(JSI::JSON::HashNode.new({a: 'b', 'c' => 'd', nil => 3}, [])))
actual = klass.new(JSI::JSON::HashNode.new({'a' => 'b', 'c' => 'd', nil => 3}, []))
Expand Down Expand Up @@ -52,10 +52,10 @@
expected = JSI::JSON::HashNode.new({'a' => 'b', 'c' => {'d' => 0}, nil => 3}, [])
assert_equal(expected, actual)
end
it 'deep stringifies SchemaObjectBase instance on initialize' do
it 'deep stringifies JSI instance' do
klass = JSI.class_for_schema(type: 'object')
expected = klass.new(JSI::JSON::HashNode.new({a: 'b', 'c' => {d: 0}, nil => 3}, []))
actual = klass.new(JSI::JSON::HashNode.new({'a' => 'b', 'c' => {'d' => 0}, nil => 3}, []))
actual = JSI.deep_stringify_symbol_keys(klass.new(JSI::JSON::HashNode.new({a: 'b', 'c' => {d: 0}, nil => 3}, [])))
expected = klass.new(JSI::JSON::HashNode.new({'a' => 'b', 'c' => {'d' => 0}, nil => 3}, []))
assert_equal(expected, actual)
end
end
Expand Down

0 comments on commit 1b5f459

Please sign in to comment.