Skip to content
This repository has been archived by the owner on Jul 28, 2020. It is now read-only.

Commit

Permalink
Fix symbol json encoding breaking compatibility with some gems
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYawe committed Jun 3, 2019
1 parent ca18973 commit bf042b7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changes
- Fix symbol json encoding breaking compatibility with some gems

## [0.4.0] - 2019-03-25
### Added
- Calling `#dispatch` on tasks now allows to process tasks asynchronously
Expand Down
24 changes: 19 additions & 5 deletions lib/zenaton/services/properties.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# frozen_string_literal: true

require 'singleton'
require 'json/add/core'
require 'json/add/date'
require 'json/add/date_time'
require 'json/add/exception'
require 'json/add/range'
require 'json/add/regexp'
require 'json/add/struct'
require 'json/add/time'
require 'json/add/rational'
require 'json/add/complex'
require 'json/add/bigdecimal'
Expand Down Expand Up @@ -97,14 +103,22 @@ def valid_object(object, super_class)
end

def from_complex_type(object)
JSON.parse(object.to_json).tap do |attributes|
attributes.delete('json_class')
if object.is_a?(Symbol)
{ 's' => object.to_s }
else
JSON.parse(object.to_json).tap do |attributes|
attributes.delete('json_class')
end
end
end

def set_complex_type(object, props)
props['json_class'] = object.class.name
JSON(props.to_json)
if object.is_a?(Symbol)
props['s'].to_sym
else
props['json_class'] = object.class.name
JSON(props.to_json)
end
end

def special_case?(object)
Expand Down
24 changes: 24 additions & 0 deletions spec/zenaton/services/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
end
end

context 'with a symbol' do
let(:data) { :foobar }

it 'represents the symbol as a data' do
expect(parsed_json).to eq(
'o' => '@zenaton#0',
's' => [{ 'n' => 'Symbol', 'p' => { 's' => 'foobar' } }]
)
end
end

context 'with an integer' do
let(:data) { 1 }

Expand Down Expand Up @@ -325,6 +336,19 @@
end
end

context 'with a symbol do' do
let(:json) do
{
'o' => '@zenaton#0',
's' => [{ 'n' => 'Symbol', 'p' => { 's' => 'foobar' } }]
}.to_json
end

it 'returns the symbol' do
expect(decoded).to eq(:foobar)
end
end

context 'with an integer do' do
let(:json) { { 'd' => 1, 's' => [] }.to_json }

Expand Down

0 comments on commit bf042b7

Please sign in to comment.