rapidjson 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +110 -0
- data/Rakefile +20 -0
- data/ext/rapidjson/buffer.hh +66 -0
- data/ext/rapidjson/cext.cc +77 -0
- data/ext/rapidjson/cext.hh +20 -0
- data/ext/rapidjson/encoder.hh +150 -0
- data/ext/rapidjson/extconf.rb +19 -0
- data/ext/rapidjson/parser.hh +149 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/allocators.h +692 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/cursorstreamwrapper.h +78 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/document.h +3027 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/encodedstream.h +299 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/encodings.h +716 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/error/en.h +122 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/error/error.h +216 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/filereadstream.h +99 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/filewritestream.h +104 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/fwd.h +151 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/biginteger.h +297 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/clzll.h +71 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/diyfp.h +261 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/dtoa.h +249 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/ieee754.h +78 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/itoa.h +308 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/meta.h +186 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/pow10.h +55 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/regex.h +739 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/stack.h +232 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/strfunc.h +83 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/strtod.h +293 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/internal/swap.h +46 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/istreamwrapper.h +128 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/memorybuffer.h +70 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/memorystream.h +71 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/msinttypes/inttypes.h +316 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/msinttypes/stdint.h +300 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/ostreamwrapper.h +81 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/pointer.h +1482 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/prettywriter.h +277 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/rapidjson.h +741 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/reader.h +2246 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/schema.h +2795 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/stream.h +223 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/stringbuffer.h +121 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/uri.h +481 -0
- data/ext/rapidjson/rapidjson/include/rapidjson/writer.h +710 -0
- data/lib/rapidjson/json_gem.rb +36 -0
- data/lib/rapidjson/version.rb +5 -0
- data/lib/rapidjson.rb +9 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9be62f9ee39623db68d4be3b18f92bc53ddfc3ec53f7719f217eaf27949b5991
|
4
|
+
data.tar.gz: b7128693f67aaf766976de8d9548831792cde06229f73af848aa859a660d002f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 93193a3fe5f14387422078bc2186363f3898916d5615b6c927dbb04df2f7d19dc7d14ac87de4acd69eebc051818db0d040eac3eb8e084a9289a00e598f79aa2b
|
7
|
+
data.tar.gz: 24a1eb05a39403bcf6b06c38049b52227b06cdd1147d314caeab8566ceef449a42aafa4545e731996d5a069539f40edb528b9643037f40d5528e2cdb7a1a5482
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at john@hawthorn.email. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 John Hawthorn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# RapidJSON
|
2
|
+
|
3
|
+
(Maybe) Ruby's fastest JSON library! Built using the [RapidJSON C++ library](https://rapidjson.org/)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rapidjson'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rapidjson
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
RapidJSON.parse <<JSON
|
25
|
+
{
|
26
|
+
"foo":"bar"
|
27
|
+
}
|
28
|
+
JSON
|
29
|
+
# => {"foo" => "bar"}
|
30
|
+
```
|
31
|
+
|
32
|
+
```
|
33
|
+
RapidJSON.encode(json_string)
|
34
|
+
# => '{"foo":"bar"}'
|
35
|
+
```
|
36
|
+
|
37
|
+
```
|
38
|
+
RapidJSON.pretty_encode(json_string)
|
39
|
+
# =>
|
40
|
+
# {
|
41
|
+
# "foo": "bar"
|
42
|
+
# }
|
43
|
+
```
|
44
|
+
|
45
|
+
## Performance
|
46
|
+
|
47
|
+
Your current JSON parser/encoder is probably fine.
|
48
|
+
|
49
|
+
Unless there's good reason, it's probably best sticking with the standard `json` gem, which ships with Ruby. It become much faster in version 2.3, so try it again if you haven't recently!
|
50
|
+
|
51
|
+
However this library has a few performance advantages:
|
52
|
+
|
53
|
+
* JSON parsing
|
54
|
+
* Performance is achieved mostly through using RapidJSON one of the fastest open source JSON parsing libraries. It supports SIMD (SSE2, SSE4.2, NEON), avoids allocated memory, and has been honed to be if not the fastest library (that honour likely going to simdjson) the library to beat for JSON performance.
|
55
|
+
* Object allocation
|
56
|
+
* Wherever possible we avoid allocating objects. When generating JSON, RapidJSON will write the emitted JSON directly into the buffer of a Ruby string. (This is an optimization most Ruby JSON libraries will have)
|
57
|
+
* When parsing JSON we parse directly form the source string with a single copy
|
58
|
+
* When building a Hash for a JSON object, we use an fstrings (dedup'd and frozen strings) as the key
|
59
|
+
* Whenever possible we build Ruby objects from C types (int, char \*, double) rather than constructing intermediate Ruby string objects.
|
60
|
+
|
61
|
+
Many of these optimization can be found in all popular Ruby JSON libraries
|
62
|
+
|
63
|
+
```
|
64
|
+
== Encoding canada.json (2090234 bytes)
|
65
|
+
yajl 13.957 (± 0.0%) i/s - 70.000 in 5.015358s
|
66
|
+
json 13.912 (± 0.0%) i/s - 70.000 in 5.032247s
|
67
|
+
oj 20.821 (± 0.0%) i/s - 106.000 in 5.090981s
|
68
|
+
rapidjson 84.110 (± 2.4%) i/s - 424.000 in 5.042792s
|
69
|
+
```
|
70
|
+
|
71
|
+
```
|
72
|
+
== Parsing canada.json (2251051 bytes)
|
73
|
+
yajl 35.510 (± 2.8%) i/s - 180.000 in 5.070803s
|
74
|
+
json 22.105 (± 0.0%) i/s - 112.000 in 5.067063s
|
75
|
+
oj 15.163 (± 6.6%) i/s - 76.000 in 5.042864s
|
76
|
+
fast_jsonparser 10.791 (± 0.0%) i/s - 54.000 in 5.004290s
|
77
|
+
rapidjson 148.263 (± 2.0%) i/s - 742.000 in 5.006370s
|
78
|
+
```
|
79
|
+
Notes: oj seems unusually bad at this test, and is usually faster than yajl and
|
80
|
+
json, and comparable to rapidjson.
|
81
|
+
`fast_jsonparser` has been slow since Ruby 3.0, likely a bug, if fixed I'd
|
82
|
+
expect it to be the fasest JSON parser.
|
83
|
+
|
84
|
+
Other libraries may include modes to avoid constructing all objects. Currently
|
85
|
+
RapidJSON only focuses on the patterns and APIs users are likely to actually
|
86
|
+
use.
|
87
|
+
|
88
|
+
## Why another JSON library
|
89
|
+
|
90
|
+
I spent a week working on YAJL/yajl-ruby, and though I really liked the library, it hasn't kept up with the performance of the modern JSON libraries, specifically simdjson (C++), serde-json (Rust), and RapidJSON (C++). I was interested in how those libraries would integrate into Ruby. Of these, RapidJSON was the simplest fit for a Ruby extension. It's in C++ (clean Rust/Ruby bindings is unfortunately a work in progress), fast, uses SIMD instructions, supports encoding and decoding, and has a nice API to work with.
|
91
|
+
|
92
|
+
However, if you're happy with your current Ruby JSON library (including `json`) you should keep using it. They're all very good.
|
93
|
+
|
94
|
+
## Development
|
95
|
+
|
96
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
97
|
+
|
98
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jhawthorn/rapidjson. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/jhawthorn/rapidjson/blob/main/CODE_OF_CONDUCT.md).
|
103
|
+
|
104
|
+
## License
|
105
|
+
|
106
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
107
|
+
|
108
|
+
## Code of Conduct
|
109
|
+
|
110
|
+
Everyone interacting in the Rapidjson project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jhawthorn/rapidjson/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"].grep_v(/\/JSONTestSuite\//i)
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rake/extensiontask"
|
13
|
+
|
14
|
+
task build: :compile
|
15
|
+
|
16
|
+
Rake::ExtensionTask.new("rapidjson") do |ext|
|
17
|
+
ext.lib_dir = "lib/rapidjson"
|
18
|
+
end
|
19
|
+
|
20
|
+
task default: %i[clobber compile test]
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#include <iostream>
|
2
|
+
|
3
|
+
using namespace std;
|
4
|
+
|
5
|
+
class RubyStringBuffer {
|
6
|
+
public:
|
7
|
+
typedef char Ch;
|
8
|
+
|
9
|
+
RubyStringBuffer() : used(0), capacity(INITIAL_SIZE) {
|
10
|
+
ruby_string = rb_str_buf_new(INITIAL_SIZE);
|
11
|
+
mem = RSTRING_PTR(ruby_string);
|
12
|
+
}
|
13
|
+
|
14
|
+
void Reserve(size_t size) {
|
15
|
+
if (capacity - used < size) {
|
16
|
+
resize(capacity * 2);
|
17
|
+
}
|
18
|
+
}
|
19
|
+
|
20
|
+
void PutUnsafe(Ch c) {
|
21
|
+
mem[used++] = c;
|
22
|
+
}
|
23
|
+
|
24
|
+
void Put(Ch c) {
|
25
|
+
Reserve(1);
|
26
|
+
PutUnsafe(c);
|
27
|
+
}
|
28
|
+
|
29
|
+
void Flush() {
|
30
|
+
rb_str_set_len(ruby_string, used);
|
31
|
+
}
|
32
|
+
|
33
|
+
VALUE GetRubyString() {
|
34
|
+
return ruby_string;
|
35
|
+
}
|
36
|
+
|
37
|
+
private:
|
38
|
+
void resize(size_t newcap) {
|
39
|
+
rb_str_modify_expand(ruby_string, newcap);
|
40
|
+
mem = RSTRING_PTR(ruby_string);
|
41
|
+
capacity = newcap;
|
42
|
+
}
|
43
|
+
|
44
|
+
const size_t INITIAL_SIZE = 2048;
|
45
|
+
|
46
|
+
VALUE ruby_string;
|
47
|
+
|
48
|
+
size_t used;
|
49
|
+
size_t capacity;
|
50
|
+
char *mem;
|
51
|
+
|
52
|
+
RubyStringBuffer(const RubyStringBuffer&);
|
53
|
+
RubyStringBuffer& operator=(const RubyStringBuffer&);
|
54
|
+
};
|
55
|
+
|
56
|
+
inline void PutReserve(RubyStringBuffer &stream, size_t count) {
|
57
|
+
stream.Reserve(count);
|
58
|
+
}
|
59
|
+
|
60
|
+
inline void PutUnsafe(RubyStringBuffer &stream, char c) {
|
61
|
+
stream.PutUnsafe(c);
|
62
|
+
}
|
63
|
+
|
64
|
+
//inline void PutN(RubyStringBuffer &stream, char c, size_t n) {
|
65
|
+
// std::memset(stream..Push<char>(n), c, n * sizeof(c));
|
66
|
+
//}
|
@@ -0,0 +1,77 @@
|
|
1
|
+
#include "cext.hh"
|
2
|
+
|
3
|
+
#include "rapidjson/writer.h"
|
4
|
+
#include "rapidjson/prettywriter.h"
|
5
|
+
#include "rapidjson/error/en.h"
|
6
|
+
|
7
|
+
static VALUE rb_mRapidjson;
|
8
|
+
static VALUE rb_eParseError;
|
9
|
+
static VALUE rb_eEncodeError;
|
10
|
+
|
11
|
+
static ID id_to_json;
|
12
|
+
static ID id_to_s;
|
13
|
+
|
14
|
+
#include "encoder.hh"
|
15
|
+
#include "parser.hh"
|
16
|
+
|
17
|
+
using namespace rapidjson;
|
18
|
+
|
19
|
+
typedef RubyStringBuffer DefaultBuffer;
|
20
|
+
|
21
|
+
VALUE encode(VALUE _self, VALUE obj) {
|
22
|
+
RubyObjectEncoder<DefaultBuffer, Writer<DefaultBuffer>> encoder;
|
23
|
+
return encoder.encode(obj);
|
24
|
+
}
|
25
|
+
|
26
|
+
VALUE pretty_encode(VALUE _self, VALUE obj) {
|
27
|
+
RubyObjectEncoder<DefaultBuffer, PrettyWriter<DefaultBuffer>> encoder;
|
28
|
+
return encoder.encode(obj);
|
29
|
+
}
|
30
|
+
|
31
|
+
VALUE parse(VALUE _self, VALUE string) {
|
32
|
+
RubyObjectHandler handler;
|
33
|
+
Reader reader;
|
34
|
+
char *cstring = StringValueCStr(string); // fixme?
|
35
|
+
StringStream ss(cstring);
|
36
|
+
ParseResult ok = reader.Parse(ss, handler);
|
37
|
+
|
38
|
+
if (!ok) {
|
39
|
+
rb_raise(rb_eParseError, "JSON parse error: %s (%lu)",
|
40
|
+
GetParseError_En(ok.Code()), ok.Offset());
|
41
|
+
}
|
42
|
+
|
43
|
+
return handler.GetRoot();
|
44
|
+
}
|
45
|
+
|
46
|
+
VALUE valid_json_p(VALUE _self, VALUE string) {
|
47
|
+
NullHandler handler;
|
48
|
+
Reader reader;
|
49
|
+
char *cstring = StringValueCStr(string); // fixme?
|
50
|
+
StringStream ss(cstring);
|
51
|
+
ParseResult ok = reader.Parse(ss, handler);
|
52
|
+
|
53
|
+
if (!ok) {
|
54
|
+
return Qfalse;
|
55
|
+
}
|
56
|
+
|
57
|
+
return Qtrue;
|
58
|
+
}
|
59
|
+
|
60
|
+
extern "C" void
|
61
|
+
Init_rapidjson(void)
|
62
|
+
{
|
63
|
+
id_to_s = rb_intern("to_s");
|
64
|
+
id_to_json = rb_intern("to_json");
|
65
|
+
|
66
|
+
rb_mRapidjson = rb_define_module("RapidJSON");
|
67
|
+
rb_define_module_function(rb_mRapidjson, "encode", encode, 1);
|
68
|
+
rb_define_module_function(rb_mRapidjson, "pretty_encode", pretty_encode, 1);
|
69
|
+
rb_define_module_function(rb_mRapidjson, "dump", encode, 1);
|
70
|
+
|
71
|
+
rb_define_module_function(rb_mRapidjson, "parse", parse, 1);
|
72
|
+
rb_define_module_function(rb_mRapidjson, "load", parse, 1);
|
73
|
+
rb_define_module_function(rb_mRapidjson, "valid_json?", valid_json_p, 1);
|
74
|
+
|
75
|
+
rb_eParseError = rb_define_class_under(rb_mRapidjson, "ParseError", rb_eStandardError);
|
76
|
+
rb_eEncodeError = rb_define_class_under(rb_mRapidjson, "EncodeError", rb_eStandardError);
|
77
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#ifndef RAPIDJSON_H
|
2
|
+
#define RAPIDJSON_H 1
|
3
|
+
|
4
|
+
#include "ruby.h"
|
5
|
+
#include "ruby/encoding.h"
|
6
|
+
|
7
|
+
#ifdef __SSE2__
|
8
|
+
#define RAPIDJSON_SSE2
|
9
|
+
#endif
|
10
|
+
|
11
|
+
/* fixme: compilation fails? */
|
12
|
+
#if 0
|
13
|
+
#ifdef __SSE4_2__
|
14
|
+
#define RAPIDJSON_SSE42
|
15
|
+
#endif
|
16
|
+
#endif
|
17
|
+
|
18
|
+
#include "rapidjson/rapidjson.h"
|
19
|
+
|
20
|
+
#endif /* RAPIDJSON_H */
|
@@ -0,0 +1,150 @@
|
|
1
|
+
#pragma once
|
2
|
+
|
3
|
+
#include "rapidjson/writer.h"
|
4
|
+
#include "buffer.hh"
|
5
|
+
|
6
|
+
using namespace rapidjson;
|
7
|
+
|
8
|
+
template <typename B = RubyStringBuffer, typename W=Writer<B>>
|
9
|
+
class RubyObjectEncoder {
|
10
|
+
B buf;
|
11
|
+
W writer;
|
12
|
+
|
13
|
+
void encode_array(VALUE ary) {
|
14
|
+
writer.StartArray();
|
15
|
+
int length = RARRAY_LEN(ary);
|
16
|
+
RARRAY_PTR_USE(ary, ptr, {
|
17
|
+
for (int i = 0; i < length; i++) {
|
18
|
+
encode_any(ptr[i]);
|
19
|
+
}
|
20
|
+
});
|
21
|
+
writer.EndArray();
|
22
|
+
}
|
23
|
+
|
24
|
+
void encode_key(VALUE key) {
|
25
|
+
switch(rb_type(key)) {
|
26
|
+
case T_SYMBOL:
|
27
|
+
key = rb_sym2str(key);
|
28
|
+
/* FALLTHRU */
|
29
|
+
case T_STRING:
|
30
|
+
writer.Key(RSTRING_PTR(key), RSTRING_LEN(key), false);
|
31
|
+
return;
|
32
|
+
default:
|
33
|
+
{
|
34
|
+
VALUE str = rb_funcall(key, id_to_s, 0);
|
35
|
+
Check_Type(str, T_STRING);
|
36
|
+
encode_string(str);
|
37
|
+
}
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
static int encode_hash_i_cb(VALUE key, VALUE val, VALUE ctx) {
|
42
|
+
RubyObjectEncoder *encoder = (RubyObjectEncoder *)ctx;
|
43
|
+
encoder->encode_hash_i(key, val);
|
44
|
+
return ST_CONTINUE;
|
45
|
+
}
|
46
|
+
|
47
|
+
void encode_hash_i(VALUE key, VALUE val) {
|
48
|
+
encode_key(key);
|
49
|
+
encode_any(val);
|
50
|
+
}
|
51
|
+
|
52
|
+
void encode_hash(VALUE hash) {
|
53
|
+
writer.StartObject();
|
54
|
+
rb_hash_foreach(hash, encode_hash_i_cb, (VALUE)this);
|
55
|
+
writer.EndObject();
|
56
|
+
}
|
57
|
+
|
58
|
+
void encode_fixnum(VALUE v) {
|
59
|
+
writer.Int64(FIX2LONG(v));
|
60
|
+
}
|
61
|
+
|
62
|
+
void encode_bignum(VALUE b) {
|
63
|
+
b = rb_big_norm(b);
|
64
|
+
if (FIXNUM_P(b)) {
|
65
|
+
return encode_fixnum(b);
|
66
|
+
}
|
67
|
+
|
68
|
+
bool negative = rb_big_cmp(b, INT2FIX(0)) == INT2FIX(-1);
|
69
|
+
if (negative) {
|
70
|
+
long long ll = rb_big2ll(b);
|
71
|
+
writer.Int64(ll);
|
72
|
+
} else {
|
73
|
+
unsigned long long ull = rb_big2ull(b);
|
74
|
+
writer.Uint64(ull);
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
void encode_float(VALUE v) {
|
79
|
+
double f = rb_float_value(v);
|
80
|
+
writer.Double(f);
|
81
|
+
}
|
82
|
+
|
83
|
+
void encode_string(VALUE v) {
|
84
|
+
writer.String(RSTRING_PTR(v), RSTRING_LEN(v), false);
|
85
|
+
}
|
86
|
+
|
87
|
+
void encode_symbol(VALUE v) {
|
88
|
+
encode_string(rb_sym2str(v));
|
89
|
+
}
|
90
|
+
|
91
|
+
void encode_raw_json_str(VALUE s) {
|
92
|
+
const char *cstr = RSTRING_PTR(s);
|
93
|
+
size_t len = RSTRING_LEN(s);
|
94
|
+
|
95
|
+
writer.RawValue(cstr, len, kObjectType);
|
96
|
+
}
|
97
|
+
|
98
|
+
void encode_generic(VALUE obj) {
|
99
|
+
if (rb_respond_to(obj, id_to_json)) {
|
100
|
+
VALUE str = rb_funcall(obj, id_to_json, 0);
|
101
|
+
Check_Type(str, T_STRING);
|
102
|
+
encode_raw_json_str(str);
|
103
|
+
} else {
|
104
|
+
VALUE str = rb_funcall(obj, id_to_s, 0);
|
105
|
+
Check_Type(str, T_STRING);
|
106
|
+
encode_string(str);
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
void encode_any(VALUE v) {
|
111
|
+
switch(rb_type(v)) {
|
112
|
+
case T_NIL:
|
113
|
+
writer.Null();
|
114
|
+
return;
|
115
|
+
case T_FALSE:
|
116
|
+
writer.Bool(false);
|
117
|
+
return;
|
118
|
+
case T_TRUE:
|
119
|
+
writer.Bool(true);
|
120
|
+
return;
|
121
|
+
case T_FIXNUM:
|
122
|
+
return encode_fixnum(v);
|
123
|
+
case T_BIGNUM:
|
124
|
+
return encode_bignum(v);
|
125
|
+
case T_FLOAT:
|
126
|
+
return encode_float(v);
|
127
|
+
case T_HASH:
|
128
|
+
return encode_hash(v);
|
129
|
+
case T_ARRAY:
|
130
|
+
return encode_array(v);
|
131
|
+
case T_STRING:
|
132
|
+
return encode_string(v);
|
133
|
+
case T_SYMBOL:
|
134
|
+
return encode_symbol(v);
|
135
|
+
default:
|
136
|
+
encode_generic(v);
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
public:
|
141
|
+
RubyObjectEncoder(): buf(), writer(buf), depth(0) {
|
142
|
+
};
|
143
|
+
|
144
|
+
int depth;
|
145
|
+
|
146
|
+
VALUE encode(VALUE obj) {
|
147
|
+
encode_any(obj);
|
148
|
+
return buf.GetRubyString();
|
149
|
+
}
|
150
|
+
};
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "mkmf"
|
4
|
+
|
5
|
+
submodule = "#{__dir__}/rapidjson"
|
6
|
+
$CXXFLAGS += " -O3 -I#{submodule}/include/ -Wall "
|
7
|
+
|
8
|
+
if ENV["DEBUG"]
|
9
|
+
$CXXFLAGS += " -Og -g -Wall -Wextra "
|
10
|
+
end
|
11
|
+
|
12
|
+
if ENV["SANITIZE"]
|
13
|
+
$CXXFLAGS += " -fsanitize=address"
|
14
|
+
$LDFLAGS += " -fsanitize=address"
|
15
|
+
end
|
16
|
+
|
17
|
+
have_func("rb_interned_str", "ruby.h")
|
18
|
+
|
19
|
+
create_makefile("rapidjson/rapidjson")
|