Skip to content
This repository

Replace Hash#to_json and ActiveSupport::JSON to MultiJson #48

Merged
merged 1 commit into from almost 2 years ago

2 participants

Hirohisa Mitsuishi Ian Leitch
Hirohisa Mitsuishi

To be able to select more fast and more space-saving JSON engine.

Space-saving is important for non-ascii utf8 string.
See the following examples.

  • ActiveSupport::JSON
    ActiveSupport::JSON.encode( { "aps" => { "alert" => "あいうえお" } } ) #=> {"aps":{"alert":"\u3042\u3044\u3046\u3048\u304a"}} # 50 bytes

  • MultiJSON with oj engine
    MultiJson.dump( { "aps" => { "alert" => "あいうえお" } ) #=> {"aps":{"alert":"あいうえお"}} # 35 bytes

Ian Leitch ileitch merged commit cc3b460 into from June 20, 2012
Ian Leitch ileitch closed this June 20, 2012
Ian Leitch
Owner

Thank you.

Ian Leitch
Owner

This is failing on REE: http://travis-ci.org/#!/ileitch/rapns/jobs/1666415

Any idea why?

Hirohisa Mitsuishi

Because of ok_json, MultiJson's default JSON engine, converting ActiveSupport::OrderedHash into Hash in internal.
To resolve the failure add 'gem "yajl-ruby"' to Gemfile .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 20, 2012
Hirohisa Mitsuishi Replaced Hash#to_json and ActiveSurpport::JSON to MultiJson 955bf7b
This page is out of date. Refresh to see the latest.
3  Gemfile
@@ -9,6 +9,7 @@ gem 'mysql2'
9 9
 gem 'mysql'
10 10
 gem 'database_cleaner'
11 11
 gem 'simplecov'
  12
+gem 'multi_json'
12 13
 #gem 'perftools.rb'
13 14
 
14  
-gemspec
  15
+gemspec
3  lib/rapns.rb
... ...
@@ -1,8 +1,9 @@
1 1
 require 'active_record'
  2
+require 'multi_json'
2 3
 
3 4
 require 'rapns/version'
4 5
 require 'rapns/binary_notification_validator'
5 6
 require 'rapns/device_token_format_validator'
6 7
 require 'rapns/notification'
7 8
 require 'rapns/feedback'
8  
-require 'rapns/app'
  9
+require 'rapns/app'
14  lib/rapns/notification.rb
@@ -17,7 +17,7 @@ def device_token=(token)
17 17
 
18 18
     def alert=(alert)
19 19
       if alert.is_a?(Hash)
20  
-        write_attribute(:alert, ActiveSupport::JSON.encode(alert))
  20
+        write_attribute(:alert, MultiJson.dump(alert))
21 21
         self.alert_is_json = true if has_attribute?(:alert_is_json)
22 22
       else
23 23
         write_attribute(:alert, alert)
@@ -30,22 +30,22 @@ def alert
30 30
 
31 31
       if has_attribute?(:alert_is_json)
32 32
         if alert_is_json?
33  
-          ActiveSupport::JSON.decode(string_or_json)
  33
+          MultiJson.load(string_or_json)
34 34
         else
35 35
           string_or_json
36 36
         end
37 37
       else
38  
-        ActiveSupport::JSON.decode(string_or_json) rescue string_or_json
  38
+        MultiJson.load(string_or_json) rescue string_or_json
39 39
       end
40 40
     end
41 41
 
42 42
     def attributes_for_device=(attrs)
43 43
       raise ArgumentError, "attributes_for_device must be a Hash" if !attrs.is_a?(Hash)
44  
-      write_attribute(:attributes_for_device, ActiveSupport::JSON.encode(attrs))
  44
+      write_attribute(:attributes_for_device, MultiJson.dump(attrs))
45 45
     end
46 46
 
47 47
     def attributes_for_device
48  
-      ActiveSupport::JSON.decode(read_attribute(:attributes_for_device)) if read_attribute(:attributes_for_device)
  48
+      MultiJson.load(read_attribute(:attributes_for_device)) if read_attribute(:attributes_for_device)
49 49
     end
50 50
 
51 51
     MDM_OVERIDE_KEY = '__rapns_mdm__'
@@ -70,7 +70,7 @@ def as_json
70 70
     end
71 71
 
72 72
     def payload
73  
-      as_json.to_json
  73
+      MultiJson.dump(as_json)
74 74
     end
75 75
 
76 76
     def payload_size
@@ -84,4 +84,4 @@ def to_binary(options = {})
84 84
       [1, id_for_pack, expiry, 0, 32, device_token, 0, payload_size, payload].pack("cNNccH*cca*")
85 85
     end
86 86
   end
87  
-end
  87
+end
2  rapns.gemspec
@@ -15,4 +15,6 @@ Gem::Specification.new do |s|
15 15
   s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
16 16
   s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17 17
   s.require_paths = ["lib"]
  18
+
  19
+  s.add_dependency "multi_json", "~> 1.0"
18 20
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.