Sha256: 2acf646d66a7fe96d1fd5a0a45d970b7e89685302f1a69f31d15d87250fc1b41
Contents?: true
Size: 1.76 KB
Versions: 1
Compression:
Stored size: 1.76 KB
Contents
# frozen_string_literal: true require "tjson/version" require "json" require "time" require "base32" require "base64" require "tjson/datatype" require "tjson/object" # Tagged JSON with Rich Types module TJSON # Base class of all TJSON errors Error = Class.new(StandardError) # Invalid string encoding EncodingError = Class.new(Error) # Failure to parse TJSON document ParseError = Class.new(Error) # Invalid types TypeError = Class.new(ParseError) # Duplicate object name DuplicateNameError = Class.new(ParseError) # Maximum allowed nesting (TODO: use TJSON-specified maximum) MAX_NESTING = 100 # Parse the given UTF-8 string as TJSON # # @param string [String] TJSON string to be parsed # @raise [TJSON::ParseError] an error occurred parsing the given TJSON # @return [Object] parsed data def self.parse(string) begin utf8_string = string.encode(Encoding::UTF_8) rescue ::EncodingError => ex raise TJSON::EncodingError, ex.message, ex.backtrace end begin object = ::JSON.parse( utf8_string, max_nesting: MAX_NESTING, allow_nan: false, symbolize_names: false, create_additions: false, object_class: TJSON::Object ) rescue ::JSON::ParserError => ex raise TJSON::ParseError, ex.message, ex.backtrace end raise TJSON::TypeError, "invalid toplevel type: #{object.class}" unless object.is_a?(TJSON::Object) object end # Generate TJSON from a Ruby Hash or Array # # @param obj [Array, Hash] Ruby Hash or Array to serialize as TJSON # @return [String] serialized TJSON def self.generate(obj) raise TypeError, "toplevel type must be a Hash" unless obj.is_a?(Hash) JSON.generate(TJSON::DataType.generate(obj)) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
tjson-0.2.0 | lib/tjson.rb |