diff --git a/README.md b/README.md index 9848a0314..afa0c530a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/jsi/base.rb b/lib/jsi/base.rb index 5ab6b4941..f71f49c0f 100644 --- a/lib/jsi/base.rb +++ b/lib/jsi/base.rb @@ -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 diff --git a/test/base_test.rb b/test/base_test.rb index 60251adf5..b1a4ffb29 100644 --- a/test/base_test.rb +++ b/test/base_test.rb @@ -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) diff --git a/test/schema_instance_json_coder_test.rb b/test/schema_instance_json_coder_test.rb index 2e78d1ee3..d3086b3da 100644 --- a/test/schema_instance_json_coder_test.rb +++ b/test/schema_instance_json_coder_test.rb @@ -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([[]])) @@ -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([])) @@ -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('[[]]')) @@ -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('[]')) diff --git a/test/util_test.rb b/test/util_test.rb index e4c06b9a3..c5df97264 100644 --- a/test/util_test.rb +++ b/test/util_test.rb @@ -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}, [])) @@ -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