safe_pretty_json 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aeb2e8186b19509836db8d56e0c484e9de036d15be33629ff9dfd68119f71aaf
4
- data.tar.gz: '059c580ff86069a5609c517413b950a710947e03167f5199f420552059611c1e'
3
+ metadata.gz: 261e3cc178f0011f64b2727da9a8239cb7b1032c7669a5b7ee8ca66a6c135c75
4
+ data.tar.gz: b905f32d0690e67f6944bb4ae64ac942b3da4aebcd33cf9f9a231dd9ee8b2407
5
5
  SHA512:
6
- metadata.gz: e0f01c59ad5b2a029aa0ab613287b7fb129c7d1f4c0bce70ab0a0d60db9af6a0d61a220d4cf99c3b3858be7b144fa123ecea6c62e8e31fdd6837a4d0e73e501b
7
- data.tar.gz: 153f3f53e3393a7b4a1ff7e78b1af3bb99c1cc41fad70e1f80dce63d4f35b6b5980b68f0219b286ebadad26bf9022cde9896db8e0dc6ed046e1ef8e272ce9aeb
6
+ metadata.gz: 004a23c456612ae969cdb073f1a63ebdf491c03a674d9365da6cbd2b523799daf415fde32318c9d6ff36b342335e7b68318f571471c48ac8cb8403b0e8dd270e
7
+ data.tar.gz: 0d63466d5a08ed1aa880236b2960d9e20c899825ad2054629c7b18d4f4bbce84f0c26b390cb08c32c0c0a1868d59e74c6a52544551e93bb30977fe397c3ff83a
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- safe_pretty_json (0.2.0)
4
+ safe_pretty_json (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 *error() const { return error_.size() ? error_.data() : nullptr; }
14
- const char *output() const { return output_.data(); }
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
- output_.push_back(c);
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
- std::vector<char> error_;
138
- std::vector<char> output_;
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SafePrettyJson
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safe_pretty_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - contribu