safe_pretty_json 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +12 -0
- data/ext/safe_pretty_json/safe_pretty_json.cpp +20 -14
- data/lib/safe_pretty_json/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 261e3cc178f0011f64b2727da9a8239cb7b1032c7669a5b7ee8ca66a6c135c75
|
4
|
+
data.tar.gz: b905f32d0690e67f6944bb4ae64ac942b3da4aebcd33cf9f9a231dd9ee8b2407
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 004a23c456612ae969cdb073f1a63ebdf491c03a674d9365da6cbd2b523799daf415fde32318c9d6ff36b342335e7b68318f571471c48ac8cb8403b0e8dd270e
|
7
|
+
data.tar.gz: 0d63466d5a08ed1aa880236b2960d9e20c899825ad2054629c7b18d4f4bbce84f0c26b390cb08c32c0c0a1868d59e74c6a52544551e93bb30977fe397c3ff83a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -20,13 +20,25 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
### Basic
|
24
|
+
|
23
25
|
```ruby
|
26
|
+
require 'safe_pretty_json'
|
24
27
|
SafePrettyJson.prettify('{ "a": 1 }')
|
25
28
|
```
|
26
29
|
|
27
30
|
SafePrettyJson.prettify doesn't validate if the input string is valid json.
|
28
31
|
If the input is invalid, prettify returns a string whose contents are undefined.
|
29
32
|
|
33
|
+
### Rails (Rack Middleware)
|
34
|
+
|
35
|
+
Add following to application.rb.
|
36
|
+
This enable response body json prettification only when content-type is application/json.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
config.middleware.use SafePrettyJson::RackMiddleware
|
40
|
+
```
|
41
|
+
|
30
42
|
## Benchmark
|
31
43
|
|
32
44
|
```bash
|
@@ -1,4 +1,3 @@
|
|
1
|
-
#include <vector>
|
2
1
|
#include <ruby.h>
|
3
2
|
#include "extconf.h"
|
4
3
|
|
@@ -6,13 +5,15 @@ namespace {
|
|
6
5
|
|
7
6
|
class Prettifier {
|
8
7
|
public:
|
9
|
-
Prettifier(const char *input): input_(input), nest_count_(0) {
|
8
|
+
Prettifier(const char *input): input_(input), nest_count_(0), output_(0), output_size_(0), output_memory_size_(0) {
|
10
9
|
Parse();
|
11
10
|
}
|
11
|
+
~Prettifier() {
|
12
|
+
xfree(output_);
|
13
|
+
}
|
12
14
|
|
13
|
-
const char *
|
14
|
-
|
15
|
-
size_t output_size() const { return output_.size(); }
|
15
|
+
const char *output() const { return output_; }
|
16
|
+
size_t output_size() const { return output_size_; }
|
16
17
|
private:
|
17
18
|
void Parse() {
|
18
19
|
ParseSpace();
|
@@ -129,24 +130,29 @@ private:
|
|
129
130
|
}
|
130
131
|
|
131
132
|
void Write(char c) {
|
132
|
-
|
133
|
+
// http://clalance.blogspot.com/2011/01/writing-ruby-extensions-in-c-part-12.html
|
134
|
+
if (output_size_ == output_memory_size_) {
|
135
|
+
output_memory_size_ = 2 * output_memory_size_ + 1024;
|
136
|
+
REALLOC_N(output_, char, output_memory_size_);
|
137
|
+
}
|
138
|
+
if (output_) {
|
139
|
+
output_[output_size_] = c;
|
140
|
+
output_size_++;
|
141
|
+
} else {
|
142
|
+
output_size_ = 0;
|
143
|
+
}
|
133
144
|
}
|
134
145
|
|
135
146
|
const char *input_;
|
136
147
|
int nest_count_;
|
137
|
-
|
138
|
-
|
148
|
+
char *output_;
|
149
|
+
size_t output_size_;
|
150
|
+
size_t output_memory_size_;
|
139
151
|
};
|
140
152
|
|
141
153
|
VALUE prettify(VALUE _self, VALUE val) {
|
142
|
-
Check_Type(val, T_STRING);
|
143
154
|
const char *input = StringValueCStr(val);
|
144
|
-
|
145
155
|
Prettifier prettifier(input);
|
146
|
-
if (prettifier.error()) {
|
147
|
-
rb_raise(rb_eRuntimeError, "%s", prettifier.error());
|
148
|
-
}
|
149
|
-
|
150
156
|
return rb_str_new(prettifier.output(), prettifier.output_size());
|
151
157
|
}
|
152
158
|
|