logstash-output-scalyr 0.2.0.beta → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -1
- data/Gemfile +3 -0
- data/README.md +6 -4
- data/lib/scalyr/constants.rb +1 -1
- data/logstash-output-scalyr.gemspec +1 -1
- data/spec/benchmarks/set_session_level_serverhost_on_events.rb +0 -2
- data/spec/logstash/outputs/scalyr_integration_spec.rb +5 -0
- data/spec/logstash/outputs/scalyr_spec.rb +61 -0
- data/spec/scalyr/common/util_spec.rb +4 -0
- data/vendor/bundle/jruby/2.5.0/cache/json-1.8.6-java.gem +0 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/bigdecimal.rb +28 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/complex.rb +28 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/core.rb +11 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/date.rb +34 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/date_time.rb +50 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/exception.rb +31 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/ostruct.rb +31 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/range.rb +29 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/rational.rb +27 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/regexp.rb +30 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/struct.rb +30 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/symbol.rb +25 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/add/time.rb +38 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/common.rb +484 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/ext/generator.jar +0 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/ext/parser.jar +0 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/ext.rb +21 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/generic_object.rb +70 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/pure/generator.rb +530 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/pure/parser.rb +359 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/pure.rb +21 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json/version.rb +8 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/lib/json.rb +62 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail1.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail10.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail11.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail12.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail13.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail14.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail18.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail19.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail2.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail20.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail21.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail22.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail23.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail24.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail25.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail27.json +2 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail28.json +2 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail3.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail4.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail5.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail6.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail7.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail8.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/fail9.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/pass1.json +56 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/pass15.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/pass16.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/pass17.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/pass2.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/pass26.json +1 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/fixtures/pass3.json +6 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/setup_variant.rb +11 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/test_json.rb +519 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/test_json_addition.rb +196 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/test_json_encoding.rb +65 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/test_json_fixtures.rb +35 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/test_json_generate.rb +348 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/test_json_generic_object.rb +75 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/test_json_string_matching.rb +39 -0
- data/vendor/bundle/jruby/2.5.0/gems/json-1.8.6-java/tests/test_json_unicode.rb +72 -0
- data/vendor/bundle/jruby/2.5.0/specifications/json-1.8.6-java.gemspec +37 -0
- metadata +69 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17884743262e123a7719335dd18559236505f4bfbf50ff8b2e2ca24e9416c35f
|
4
|
+
data.tar.gz: 34db0e1f15cc4f9a207fb5a681a5f4d6a1337f1187e01dc6a0f9467440ab53d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c55678d99d117c2dd08fe48c4505b2da39a57553cf8cc36a84cab7aa38dbf539bb63b564772cf5a2e575c334b617bdb3b5b61d91af13c5cff0c4ea4c18738834
|
7
|
+
data.tar.gz: d8e819de557a452f4129bd94eb581ff40dbdd902a1eba07ef6e7ec838f72ee5dfdd266827100232f03f9269cd933329426846081dcfaf5b3e66084075b79989c
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
[![CircleCI](https://circleci.com/gh/scalyr/logstash-output-scalyr.svg?style=svg)](https://circleci.com/gh/scalyr/logstash-output-scalyr)
|
2
2
|
|
3
|
-
# [Scalyr output plugin for Logstash
|
3
|
+
# [Scalyr output plugin for Logstash]
|
4
4
|
|
5
5
|
This plugin implements a Logstash output plugin that uploads data to [Scalyr](http://www.scalyr.com).
|
6
6
|
|
@@ -9,7 +9,9 @@ You can view documentation for this plugin [on the Scalyr website](https://app.s
|
|
9
9
|
# Quick start
|
10
10
|
|
11
11
|
1. Build the gem, run `gem build logstash-output-scalyr.gemspec`
|
12
|
-
2. Install the gem into a Logstash installation, run `/usr/share/logstash/bin/logstash-plugin install logstash-output-scalyr-0.2.0.
|
12
|
+
2. Install the gem into a Logstash installation, run `/usr/share/logstash/bin/logstash-plugin install logstash-output-scalyr-0.2.0.gem`
|
13
|
+
or follow the latest official instructions on working with plugins from Logstash. As an alternative, you can directly install latest
|
14
|
+
stable version from RubyGems - ``/usr/share/logstash/bin/logstash-plugin --version 0.2.0 logstash-output-scalyr``
|
13
15
|
3. Configure the output plugin (e.g. add it to a pipeline .conf)
|
14
16
|
4. Restart Logstash
|
15
17
|
|
@@ -41,7 +43,7 @@ In the above example, the Logstash pipeline defines a file input that reads from
|
|
41
43
|
|
42
44
|
## Notes on serverHost attribute handling
|
43
45
|
|
44
|
-
> Some of this functionality has been fixed and changed in the v0.2.
|
46
|
+
> Some of this functionality has been fixed and changed in the v0.2.0 release. In previous
|
45
47
|
versions, plugin added ``serverHost`` attribute with a value of ``Logstash`` to each event and
|
46
48
|
this attribute was not handled correctly - it was treated as a regular event level attribute
|
47
49
|
and not a special attribute which can be used for Source functionality and filtering.
|
@@ -460,7 +462,7 @@ git clone https://github.com/Kami/logstash-config-tester ~/
|
|
460
462
|
gem build logstash-output-scalyr.gemspec
|
461
463
|
|
462
464
|
# 2. Copy it to the config test repo
|
463
|
-
cp logstash-output-scalyr-0.2.0.
|
465
|
+
cp logstash-output-scalyr-0.2.0.gem ~/logstash-config-test/logstash-output-scalyr.gem
|
464
466
|
|
465
467
|
# 3. Build docker image with the latest dev version of the plugin (may take a while)
|
466
468
|
docker-compose build
|
data/lib/scalyr/constants.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'logstash-output-scalyr'
|
3
|
-
s.version = '0.2.0
|
3
|
+
s.version = '0.2.0'
|
4
4
|
s.licenses = ['Apache-2.0']
|
5
5
|
s.summary = "Scalyr output plugin for Logstash"
|
6
6
|
s.description = "Sends log data collected by Logstash to Scalyr (https://www.scalyr.com)"
|
@@ -86,7 +86,6 @@ puts "==============================="
|
|
86
86
|
data = generate_events(100)
|
87
87
|
run_benchmark_and_print_results(data, method(:run_func))
|
88
88
|
|
89
|
-
|
90
89
|
# 500 events in a batch
|
91
90
|
data = generate_events(500)
|
92
91
|
run_benchmark_and_print_results(data, method(:run_func))
|
@@ -105,5 +104,4 @@ run_benchmark_and_print_results(data, method(:run_func))
|
|
105
104
|
|
106
105
|
# 5000 events in a batch
|
107
106
|
data = generate_events(5000)
|
108
|
-
puts data.size
|
109
107
|
run_benchmark_and_print_results(data, method(:run_func))
|
@@ -5,6 +5,11 @@ require "logstash/codecs/plain"
|
|
5
5
|
require "logstash/event"
|
6
6
|
require "json"
|
7
7
|
require 'webmock/rspec'
|
8
|
+
|
9
|
+
# Require the specific version of `json` used in logstash
|
10
|
+
gem 'json', '1.8.6'
|
11
|
+
require 'json'
|
12
|
+
|
8
13
|
WebMock.allow_net_connect!
|
9
14
|
|
10
15
|
RSpec.configure do |rspec|
|
@@ -6,6 +6,10 @@ require "logstash/event"
|
|
6
6
|
require "json"
|
7
7
|
require "quantile"
|
8
8
|
|
9
|
+
# Require the specific version of `json` used in logstash
|
10
|
+
gem 'json', '1.8.6'
|
11
|
+
require 'json'
|
12
|
+
|
9
13
|
NODE_HOSTNAME = Socket.gethostname
|
10
14
|
|
11
15
|
class MockClientSession
|
@@ -764,6 +768,62 @@ describe LogStash::Outputs::Scalyr do
|
|
764
768
|
expect(body['events'][3]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
|
765
769
|
end
|
766
770
|
|
771
|
+
it "no serverHost defined, event level serverHost defined via non-default serverhost_field - event level value should be used" do
|
772
|
+
config = {
|
773
|
+
'api_write_token' => '1234',
|
774
|
+
'server_attributes' => {'attr1' => 'val1'},
|
775
|
+
'use_hostname_for_serverhost' => false,
|
776
|
+
'serverhost_field' => 'custom_server_host',
|
777
|
+
}
|
778
|
+
plugin = LogStash::Outputs::Scalyr.new(config)
|
779
|
+
|
780
|
+
allow(plugin).to receive(:send_status).and_return(nil)
|
781
|
+
plugin.register
|
782
|
+
|
783
|
+
|
784
|
+
expect(plugin.server_attributes['serverHost']).to eq(nil)
|
785
|
+
|
786
|
+
e1 = LogStash::Event.new
|
787
|
+
e1.set('a1', 'v1')
|
788
|
+
e1.set('custom_server_host', 'event-host-1')
|
789
|
+
|
790
|
+
e2 = LogStash::Event.new
|
791
|
+
e2.set('a2', 'v2')
|
792
|
+
e2.set('custom_server_host', 'event-host-2')
|
793
|
+
|
794
|
+
e3 = LogStash::Event.new
|
795
|
+
e3.set('a3', 'v3')
|
796
|
+
e3.set('custom_server_host', 'event-host-2')
|
797
|
+
|
798
|
+
e4 = LogStash::Event.new
|
799
|
+
e4.set('a4', 'v4')
|
800
|
+
e4.set('custom_server_host', 'event-host-2')
|
801
|
+
|
802
|
+
result = plugin.build_multi_event_request_array([e1, e2, e3, e4])
|
803
|
+
body = JSON.parse(result[0][:body])
|
804
|
+
expect(body['sessionInfo']['serverHost']).to eq(nil)
|
805
|
+
expect(body['sessionInfo']['attr1']).to eq('val1')
|
806
|
+
|
807
|
+
expect(body['logs'][0]['id']).to eq(1)
|
808
|
+
expect(body['logs'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-1')
|
809
|
+
expect(body['logs'][1]['id']).to eq(2)
|
810
|
+
expect(body['logs'][1]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-2')
|
811
|
+
|
812
|
+
expect(body['events'].size).to eq(4)
|
813
|
+
expect(body['events'][0]['log']).to eq(1)
|
814
|
+
expect(body['events'][0]['attrs']["serverHost"]).to eq(nil)
|
815
|
+
expect(body['events'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
|
816
|
+
expect(body['events'][1]['log']).to eq(2)
|
817
|
+
expect(body['events'][1]['attrs']["serverHost"]).to eq(nil)
|
818
|
+
expect(body['events'][1]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
|
819
|
+
expect(body['events'][2]['log']).to eq(2)
|
820
|
+
expect(body['events'][2]['attrs']["serverHost"]).to eq(nil)
|
821
|
+
expect(body['events'][2]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
|
822
|
+
expect(body['events'][3]['log']).to eq(2)
|
823
|
+
expect(body['events'][3]['attrs']["serverHost"]).to eq(nil)
|
824
|
+
expect(body['events'][3]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
|
825
|
+
end
|
826
|
+
|
767
827
|
it "no serverHost defined, event level serverHost defined - event level value should be used" do
|
768
828
|
config = {
|
769
829
|
'api_write_token' => '1234',
|
@@ -833,6 +893,7 @@ describe LogStash::Outputs::Scalyr do
|
|
833
893
|
result = plugin.build_multi_event_request_array([e])
|
834
894
|
body = JSON.parse(result[0][:body])
|
835
895
|
expect(body['events'].size).to eq(1)
|
896
|
+
expect(body['events'][0]['attrs']['bignumber']).to be_a_kind_of(String)
|
836
897
|
expect(plugin.instance_variable_get(:@logger)).to_not receive(:error)
|
837
898
|
end
|
838
899
|
end
|
Binary file
|
@@ -0,0 +1,28 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
defined?(::BigDecimal) or require 'bigdecimal'
|
5
|
+
|
6
|
+
class BigDecimal
|
7
|
+
# Import a JSON Marshalled object.
|
8
|
+
#
|
9
|
+
# method used for JSON marshalling support.
|
10
|
+
def self.json_create(object)
|
11
|
+
BigDecimal._load object['b']
|
12
|
+
end
|
13
|
+
|
14
|
+
# Marshal the object to JSON.
|
15
|
+
#
|
16
|
+
# method used for JSON marshalling support.
|
17
|
+
def as_json(*)
|
18
|
+
{
|
19
|
+
JSON.create_id => self.class.name,
|
20
|
+
'b' => _dump,
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
# return the JSON value
|
25
|
+
def to_json(*)
|
26
|
+
as_json.to_json
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
defined?(::Complex) or require 'complex'
|
5
|
+
|
6
|
+
class Complex
|
7
|
+
|
8
|
+
# Deserializes JSON string by converting Real value <tt>r</tt>, imaginary
|
9
|
+
# value <tt>i</tt>, to a Complex object.
|
10
|
+
def self.json_create(object)
|
11
|
+
Complex(object['r'], object['i'])
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
15
|
+
# object.
|
16
|
+
def as_json(*)
|
17
|
+
{
|
18
|
+
JSON.create_id => self.class.name,
|
19
|
+
'r' => real,
|
20
|
+
'i' => imag,
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
# Stores class name (Complex) along with real value <tt>r</tt> and imaginary value <tt>i</tt> as JSON string
|
25
|
+
def to_json(*)
|
26
|
+
as_json.to_json
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# This file requires the implementations of ruby core's custom objects for
|
2
|
+
# serialisation/deserialisation.
|
3
|
+
|
4
|
+
require 'json/add/date'
|
5
|
+
require 'json/add/date_time'
|
6
|
+
require 'json/add/exception'
|
7
|
+
require 'json/add/range'
|
8
|
+
require 'json/add/regexp'
|
9
|
+
require 'json/add/struct'
|
10
|
+
require 'json/add/symbol'
|
11
|
+
require 'json/add/time'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
# Date serialization/deserialization
|
7
|
+
class Date
|
8
|
+
|
9
|
+
# Deserializes JSON string by converting Julian year <tt>y</tt>, month
|
10
|
+
# <tt>m</tt>, day <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> to Date.
|
11
|
+
def self.json_create(object)
|
12
|
+
civil(*object.values_at('y', 'm', 'd', 'sg'))
|
13
|
+
end
|
14
|
+
|
15
|
+
alias start sg unless method_defined?(:start)
|
16
|
+
|
17
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
18
|
+
# object.
|
19
|
+
def as_json(*)
|
20
|
+
{
|
21
|
+
JSON.create_id => self.class.name,
|
22
|
+
'y' => year,
|
23
|
+
'm' => month,
|
24
|
+
'd' => day,
|
25
|
+
'sg' => start,
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
# Stores class name (Date) with Julian year <tt>y</tt>, month <tt>m</tt>, day
|
30
|
+
# <tt>d</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
|
31
|
+
def to_json(*args)
|
32
|
+
as_json.to_json(*args)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
# DateTime serialization/deserialization
|
7
|
+
class DateTime
|
8
|
+
|
9
|
+
# Deserializes JSON string by converting year <tt>y</tt>, month <tt>m</tt>,
|
10
|
+
# day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
|
11
|
+
# offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> to DateTime.
|
12
|
+
def self.json_create(object)
|
13
|
+
args = object.values_at('y', 'm', 'd', 'H', 'M', 'S')
|
14
|
+
of_a, of_b = object['of'].split('/')
|
15
|
+
if of_b and of_b != '0'
|
16
|
+
args << Rational(of_a.to_i, of_b.to_i)
|
17
|
+
else
|
18
|
+
args << of_a
|
19
|
+
end
|
20
|
+
args << object['sg']
|
21
|
+
civil(*args)
|
22
|
+
end
|
23
|
+
|
24
|
+
alias start sg unless method_defined?(:start)
|
25
|
+
|
26
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
27
|
+
# object.
|
28
|
+
def as_json(*)
|
29
|
+
{
|
30
|
+
JSON.create_id => self.class.name,
|
31
|
+
'y' => year,
|
32
|
+
'm' => month,
|
33
|
+
'd' => day,
|
34
|
+
'H' => hour,
|
35
|
+
'M' => min,
|
36
|
+
'S' => sec,
|
37
|
+
'of' => offset.to_s,
|
38
|
+
'sg' => start,
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
# Stores class name (DateTime) with Julian year <tt>y</tt>, month <tt>m</tt>,
|
43
|
+
# day <tt>d</tt>, hour <tt>H</tt>, minute <tt>M</tt>, second <tt>S</tt>,
|
44
|
+
# offset <tt>of</tt> and Day of Calendar Reform <tt>sg</tt> as JSON string
|
45
|
+
def to_json(*args)
|
46
|
+
as_json.to_json(*args)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
|
5
|
+
# Exception serialization/deserialization
|
6
|
+
class Exception
|
7
|
+
|
8
|
+
# Deserializes JSON string by constructing new Exception object with message
|
9
|
+
# <tt>m</tt> and backtrace <tt>b</tt> serialized with <tt>to_json</tt>
|
10
|
+
def self.json_create(object)
|
11
|
+
result = new(object['m'])
|
12
|
+
result.set_backtrace object['b']
|
13
|
+
result
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
17
|
+
# object.
|
18
|
+
def as_json(*)
|
19
|
+
{
|
20
|
+
JSON.create_id => self.class.name,
|
21
|
+
'm' => message,
|
22
|
+
'b' => backtrace,
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
# Stores class name (Exception) with message <tt>m</tt> and backtrace array
|
27
|
+
# <tt>b</tt> as JSON string
|
28
|
+
def to_json(*args)
|
29
|
+
as_json.to_json(*args)
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
# OpenStruct serialization/deserialization
|
7
|
+
class OpenStruct
|
8
|
+
|
9
|
+
# Deserializes JSON string by constructing new Struct object with values
|
10
|
+
# <tt>v</tt> serialized by <tt>to_json</tt>.
|
11
|
+
def self.json_create(object)
|
12
|
+
new(object['t'] || object[:t])
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
16
|
+
# object.
|
17
|
+
def as_json(*)
|
18
|
+
klass = self.class.name
|
19
|
+
klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!"
|
20
|
+
{
|
21
|
+
JSON.create_id => klass,
|
22
|
+
't' => table,
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
# Stores class name (OpenStruct) with this struct's values <tt>v</tt> as a
|
27
|
+
# JSON string.
|
28
|
+
def to_json(*args)
|
29
|
+
as_json.to_json(*args)
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED
|
2
|
+
require 'json'
|
3
|
+
end
|
4
|
+
|
5
|
+
# Range serialization/deserialization
|
6
|
+
class Range
|
7
|
+
|
8
|
+
# Deserializes JSON string by constructing new Range object with arguments
|
9
|
+
# <tt>a</tt> serialized by <tt>to_json</tt>.
|
10
|
+
def self.json_create(object)
|
11
|
+
new(*object['a'])
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns a hash, that will be turned into a JSON object and represent this
|
15
|
+
# object.
|
16
|
+
def as_json(*)
|
17
|
+
{
|
18
|
+
JSON.create_id => self.class.name,
|
19
|
+
'a' => [ first, last, exclude_end? ]
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
# Stores class name (Range) with JSON array of arguments <tt>a</tt> which
|
24
|
+
# include <tt>first</tt> (integer), <tt>last</tt> (integer), and
|
25
|
+
# <tt>exclude_end?</tt> (boolean) as JSON string.
|
26
|
+
def to_json(*args)
|
27
|
+
as_json.to_json(*args)
|
28
|
+
end
|
29
|
+
end
|