Class: Couchbase::JsonTranscoder

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/json_transcoder.rb

Instance Method Summary collapse

Instance Method Details

#decode(blob, flags) ⇒ Object

Returns decoded document.

Parameters:

  • blob (String)

    string of bytes, containing encoded representation of the document

  • flags (Integer, :json)

    bit field, describing how the data encoded

Returns:

  • (Object)

    decoded document

Raises:



34
35
36
37
38
39
# File 'lib/couchbase/json_transcoder.rb', line 34

def decode(blob, flags)
  format = TranscoderFlags.decode(flags).format
  raise Error::DecodingFailure, "Unable to decode #{format} with the JsonTranscoder" unless format == :json || format.nil?

  JSON.parse(blob) unless blob&.empty?
end

#encode(document) ⇒ Array<String, Integer>

Returns pair of encoded document and flags.

Parameters:

  • document (Object)

Returns:

  • (Array<String, Integer>)

    pair of encoded document and flags

Raises:



25
26
27
28
29
# File 'lib/couchbase/json_transcoder.rb', line 25

def encode(document)
  raise Error::EncodingFailure, "The JsonTranscoder does not support binary data" if document.is_a?(String) && !document.valid_encoding?

  [JSON.generate(document), TranscoderFlags.new(format: :json, lower_bits: 6).encode]
end