From bf042b7816d587d7e704218f1503a5cbd0c926d2 Mon Sep 17 00:00:00 2001 From: Yannis Weishaupt Date: Mon, 3 Jun 2019 19:42:58 +0200 Subject: [PATCH] Fix symbol json encoding breaking compatibility with some gems --- CHANGELOG.md | 5 +++++ lib/zenaton/services/properties.rb | 24 +++++++++++++++++++----- spec/zenaton/services/serializer_spec.rb | 24 ++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7bbf33..2c33708 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/zenaton/services/properties.rb b/lib/zenaton/services/properties.rb index c629d08..0a3418d 100644 --- a/lib/zenaton/services/properties.rb +++ b/lib/zenaton/services/properties.rb @@ -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' @@ -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) diff --git a/spec/zenaton/services/serializer_spec.rb b/spec/zenaton/services/serializer_spec.rb index 0c5b189..8868878 100644 --- a/spec/zenaton/services/serializer_spec.rb +++ b/spec/zenaton/services/serializer_spec.rb @@ -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 } @@ -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 }