crust_json_encoder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1074ad503624b1f4bc2e36af78552fbabc586a74ac593bcef796c1bd82002408
4
+ data.tar.gz: 98546f2092608a44ebb2f3925fb59aa69540a2accaeab600c1b4d10a49a781b9
5
+ SHA512:
6
+ metadata.gz: a19672a0b533f6d08121ba46287ac8f09ea4288e43262543b2fdbf10ff5d930c2bf53cf250e169410987fe6fd8f1197bf11f85819eb41646b830b2fbd3d373cc
7
+ data.tar.gz: f2ed5e4b761ee2cffc3b154b27f9e178c10cbf23d23c9b9315b3e34b4ca86a42fcddfa703a5f13e15cacb389f5f6c3f9b3971b8061c436937ca2108eb7b2670a
@@ -0,0 +1,7 @@
1
+ /target
2
+ **/*.rs.bk
3
+ Cargo.lock
4
+ Gemfile.lock
5
+ *.so
6
+ *.gem
7
+ mkmf.log
@@ -0,0 +1,13 @@
1
+ [package]
2
+ name = "crust_json_encoder"
3
+ version = "0.0.1"
4
+ authors = ["y-yagi <yuuji.yaginuma@gmail.com>"]
5
+ edition = "2018"
6
+
7
+ [lib]
8
+ name = "native"
9
+ crate-type = ["cdylib"]
10
+
11
+ [dependencies]
12
+ rutie = "0.7.0"
13
+ phf = { version = "0.8", features = ["macros"] }
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2019 Yuji Yaginuma
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,24 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "thermite/tasks"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ t.verbose = true
10
+ t.warning = true
11
+ end
12
+
13
+ Thermite::Tasks.new
14
+
15
+ task :build_lib do
16
+ system("cargo build --release", exception: true)
17
+ end
18
+
19
+ task performance: ["thermite:build"] do
20
+ system("bundle exec ruby performance/benchmark.rb", exception: true)
21
+ end
22
+
23
+ task :default => :test
24
+ task :test => ['thermite:build']
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "crust_json_encoder"
5
+ require "active_support"
6
+ require "byebug"
7
+
8
+ require "irb"
9
+
10
+ ActiveSupport::JSON::Encoding.json_encoder = CrustJSONEncoder
11
+ IRB.start(__FILE__)
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "crust_json_encoder"
6
+ spec.authors = ["Yuji Yaginuma"]
7
+ spec.email = ["yuuji.yaginuma@gmail.com"]
8
+ spec.version = "0.0.1"
9
+ spec.homepage = "https://github.com/y-yagi/crust_json_encoder"
10
+
11
+ spec.summary = %q{CrustJSONEncoder is a JSON encoder for Ruby on Rails.}
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|performance)/}) }
15
+ spec.bindir = "exe"
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+ spec.extensions = ["ext/Rakefile"]
19
+
20
+ spec.add_dependency "activesupport"
21
+ spec.add_dependency "rutie"
22
+ spec.add_dependency "thermite"
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "benchmark-ips"
27
+ spec.add_development_dependency "byebug"
28
+ spec.add_development_dependency "activerecord"
29
+ spec.add_development_dependency "sqlite3"
30
+ end
@@ -0,0 +1,5 @@
1
+ require 'thermite/tasks'
2
+
3
+ project_dir = File.dirname(File.dirname(__FILE__))
4
+ Thermite::Tasks.new(cargo_project_path: project_dir, ruby_project_path: project_dir)
5
+ task default: %w(thermite:build)
@@ -0,0 +1,17 @@
1
+ require "rutie"
2
+ require "active_support"
3
+ require "active_support/json"
4
+
5
+ class CrustJSONEncoder < ActiveSupport::JSON::Encoding::JSONGemEncoder
6
+ Rutie.new(:native).init "Init_native", __dir__
7
+
8
+ private
9
+
10
+ def jsonify(value)
11
+ value.is_a?(String) ? value : super
12
+ end
13
+
14
+ def stringify(jsonified)
15
+ jsonified.is_a?(String) ? JSONEscaper.escape(super) : super
16
+ end
17
+ end
@@ -0,0 +1,53 @@
1
+ #[macro_use]
2
+ extern crate rutie;
3
+
4
+ use phf::phf_map;
5
+ use rutie::{Class, Object, RString, VM};
6
+
7
+ static JSON_ESCAPE: phf::Map<&'static str, &'static str> = phf_map! {
8
+ "\u{2028}" => r"\u2028",
9
+ "\u{2029}" => r"\u2029",
10
+ ">" => r"\u003e",
11
+ "<" => r"\u003c",
12
+ "&" => r"\u0026",
13
+ };
14
+
15
+ class!(JSONEscaper);
16
+
17
+ methods!(
18
+ JSONEscaper,
19
+ _itself,
20
+ fn escape(input: RString) -> RString {
21
+ let mut result = input.map_err(|e| VM::raise_ex(e)).unwrap().to_string();
22
+
23
+ for escape in &JSON_ESCAPE {
24
+ if result.contains(escape.0) {
25
+ result = result.replace(escape.0, escape.1)
26
+ }
27
+ }
28
+
29
+ RString::new_utf8(&result)
30
+ }
31
+ );
32
+
33
+ #[allow(non_snake_case)]
34
+ #[no_mangle]
35
+ pub extern "C" fn Init_native() {
36
+ Class::new("JSONEscaper", None).define(|itself| {
37
+ itself.def_self("escape", escape);
38
+ });
39
+ }
40
+
41
+ #[cfg(test)]
42
+ mod tests {
43
+ use crate::escape;
44
+
45
+ #[test]
46
+ fn contains_special_chars() {
47
+ let input = "this is the <string> & \u{2028} \u{2029}";
48
+ assert_eq!(
49
+ escape(input.to_string()),
50
+ "this is the \\u003cstring\\u003e \\u0026 \\u2028 \\u2029"
51
+ );
52
+ }
53
+ }
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crust_json_encoder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuji Yaginuma
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rutie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thermite
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: benchmark-ips
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activerecord
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: sqlite3
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description:
154
+ email:
155
+ - yuuji.yaginuma@gmail.com
156
+ executables: []
157
+ extensions:
158
+ - ext/Rakefile
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".gitignore"
162
+ - Cargo.toml
163
+ - Gemfile
164
+ - Gemfile.lock
165
+ - LICENSE
166
+ - Rakefile
167
+ - bin/console
168
+ - crust_json_encoder.gemspec
169
+ - ext/Rakefile
170
+ - lib/crust_json_encoder.rb
171
+ - src/lib.rs
172
+ homepage: https://github.com/y-yagi/crust_json_encoder
173
+ licenses:
174
+ - MIT
175
+ metadata: {}
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubygems_version: 3.0.3
192
+ signing_key:
193
+ specification_version: 4
194
+ summary: CrustJSONEncoder is a JSON encoder for Ruby on Rails.
195
+ test_files: []