Sha256: 3fef0f44a2bc57ed470d88920ccb1132da28311539867783a0ca8481cc4e0caa

Contents?: true

Size: 901 Bytes

Versions: 1

Compression:

Stored size: 901 Bytes

Contents

# frozen_string_literal: false

require 'json' unless defined?(::JSON::JSON_LOADED) && ::JSON::JSON_LOADED

require 'dry/monads'

# Inspired by standard library implementation
# for Time serialization/deserialization see (json/lib/json/add/time.rb)
#
module Dry
  module Monads
    class Maybe
      # Deserializes JSON string by using Dry::Monads::Maybe#lift method
      def self.json_create(serialized)
        coerce(serialized.fetch('value'))
      end

      # Returns a hash, that will be turned into a JSON object and represent this
      # object.
      def as_json(*)
        {
          JSON.create_id => self.class.name,
          value: none? ? nil : @value
        }
      end

      # Stores class name (Dry::Monads::Maybe::Some or Dry::Monads::Maybe::None)
      # with the monad value as JSON string
      def to_json(*args)
        as_json.to_json(*args)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-monads-1.3.5 lib/json/add/dry/monads/maybe.rb