crock 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/lib/crock.rb +80 -0
- metadata +67 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright © 2011, Fingertips, Manfred Stienstra <manfred@fngtps.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/lib/crock.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# Ruby implementation of JSON serialization.
|
2
|
+
#
|
3
|
+
# JSON.serialize([{'title' => 'Tron'}, 12]) # => "[{"title":"Tron"},12]"
|
4
|
+
class JSON
|
5
|
+
REPLACEMENTS = {
|
6
|
+
'\\' => '\\\\',
|
7
|
+
'"' => '\\"',
|
8
|
+
'/' => '\/',
|
9
|
+
"\b" => '\\b',
|
10
|
+
"\f" => '\\f',
|
11
|
+
"\n" => '\\n',
|
12
|
+
"\r" => '\\r',
|
13
|
+
"\t" => '\\t'
|
14
|
+
}
|
15
|
+
|
16
|
+
# Serializes a Ruby object to a valid JSON expression.
|
17
|
+
#
|
18
|
+
# JSON.serialize({'title' => 'Tron'}) # => '{"title":"Tron"}'
|
19
|
+
#
|
20
|
+
# You can serialize unsupported classes by defining the +to_json+ method
|
21
|
+
# on them.
|
22
|
+
#
|
23
|
+
# class Person
|
24
|
+
# attr_accessor :name
|
25
|
+
# def to_json
|
26
|
+
# JSON.serialize(name)
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
def self.serialize(object)
|
30
|
+
case object
|
31
|
+
when Hash
|
32
|
+
self.serialize_hash(object)
|
33
|
+
when Array
|
34
|
+
self.serialize_array(object)
|
35
|
+
when String
|
36
|
+
self.serialize_string(object)
|
37
|
+
when FalseClass
|
38
|
+
'false'
|
39
|
+
when TrueClass
|
40
|
+
'true'
|
41
|
+
when NilClass
|
42
|
+
'null'
|
43
|
+
else
|
44
|
+
object.respond_to?(:to_json) ? object.to_json : object.to_s
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class << self
|
49
|
+
alias generate serialize
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def self.serialize_string(object) #:nodoc:
|
55
|
+
escaped = object.gsub(/[\\\"\/\b\f\n\r\t]/) do |match|
|
56
|
+
REPLACEMENTS[match]
|
57
|
+
end
|
58
|
+
"\"#{escaped}\""
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.serialize_hash(object) #:nodoc:
|
62
|
+
out = '{'
|
63
|
+
first = true
|
64
|
+
for name, value in object
|
65
|
+
first ? first = false : out << ','
|
66
|
+
out << JSON.serialize(name) << ':' << JSON.serialize(value)
|
67
|
+
end
|
68
|
+
out << '}'
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.serialize_array(object) #:nodoc:
|
72
|
+
out = '['
|
73
|
+
first = true
|
74
|
+
for value in object
|
75
|
+
first ? first = false : out << ','
|
76
|
+
out << JSON.serialize(value)
|
77
|
+
end
|
78
|
+
out << ']'
|
79
|
+
end
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Manfred Stienstra
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-03-10 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: " Crock is a tiny implementation of JSON serialization in pure Ruby.\n"
|
22
|
+
email: manfred@fngtps.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
files:
|
30
|
+
- lib/crock.rb
|
31
|
+
- LICENSE
|
32
|
+
has_rdoc: true
|
33
|
+
homepage:
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --charset=utf-8
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.5.2
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: A tiny implementation of JSON serialization.
|
66
|
+
test_files: []
|
67
|
+
|