File: json.rb

package info (click to toggle)
ruby-vcr 6.0.0%2Breally5.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,320 kB
  • sloc: ruby: 8,456; sh: 177; makefile: 7
file content (48 lines) | stat: -rw-r--r-- 1,200 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'multi_json'

module VCR
  class Cassette
    class Serializers
      # The JSON serializer. Uses `MultiJson` under the covers.
      #
      # @see Psych
      # @see Syck
      # @see YAML
      module JSON
        extend self
        extend EncodingErrorHandling

        # @private
        ENCODING_ERRORS = [MultiJson::DecodeError, ArgumentError]
        ENCODING_ERRORS << EncodingError if defined?(EncodingError)

        # The file extension to use for this serializer.
        #
        # @return [String] "json"
        def file_extension
          "json"
        end

        # Serializes the given hash using `MultiJson`.
        #
        # @param [Hash] hash the object to serialize
        # @return [String] the JSON string
        def serialize(hash)
          handle_encoding_errors do
            MultiJson.encode(hash)
          end
        end

        # Deserializes the given string using `MultiJson`.
        #
        # @param [String] string the JSON string
        # @return [Hash] the deserialized object
        def deserialize(string)
          handle_encoding_errors do
            MultiJson.decode(string)
          end
        end
      end
    end
  end
end