Sha256: d62276fe06a0ac98433b97b1de85f64716ae09301e65bff8a74253e3c581fcea
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 KB
Contents
# frozen_string_literal: true module Karafka # Module for all supported by default parsers for incoming/outgoing data module Parsers # Default Karafka Json parser for serializing and deserializing data class Json # @param content [String] content based on which we want to get our hash # @return [Hash] hash with parsed JSON data # @example # Json.parse("{\"a\":1}") #=> { 'a' => 1 } def self.parse(content) ::Yajl::Parser.parse(content) rescue ::Yajl::ParseError => e raise ::Karafka::Errors::ParserError, e end # @param content [Object] any object that we want to convert to a json string # @return [String] Valid JSON string containing serialized data # @raise [Karafka::Errors::ParserError] raised when we don't have a way to parse # given content to a json string format # @note When string is passed to this method, we assume that it is already a json # string and we don't serialize it again. This allows us to serialize data before # it is being forwarded to a parser if we want to have a custom (not that simple) # json serialization # # @example From an ActiveRecord object # Json.generate(Repository.first) #=> "{\"repository\":{\"id\":\"04b504e0\"}}" # @example From a string (no changes) # Json.generate("{\"a\":1}") #=> "{\"a\":1}" def self.generate(content) return content if content.is_a?(String) return content.to_json if content.respond_to?(:to_json) raise Karafka::Errors::ParserError, content end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
karafka-0.6.0.rc1 | lib/karafka/parsers/json.rb |