Module: TJSON

Defined in:
lib/tjson.rb,
lib/tjson/object.rb,
lib/tjson/version.rb,
lib/tjson/datatype.rb,
lib/tjson/datatype/set.rb,
lib/tjson/datatype/array.rb,
lib/tjson/datatype/float.rb,
lib/tjson/datatype/value.rb,
lib/tjson/datatype/binary.rb,
lib/tjson/datatype/object.rb,
lib/tjson/datatype/string.rb,
lib/tjson/datatype/integer.rb,
lib/tjson/datatype/timestamp.rb

Overview

Tagged JSON with Rich Types

Defined Under Namespace

Classes: DataType, Object

Constant Summary collapse

Error =

Base class of all TJSON errors

Class.new(StandardError)
EncodingError =

Invalid string encoding

Class.new(Error)
ParseError =

Failure to parse TJSON document

Class.new(Error)
TypeError =

Invalid types

Class.new(ParseError)
DuplicateNameError =

Duplicate object name

Class.new(ParseError)
MAX_NESTING =

Maximum allowed nesting (TODO: use TJSON-specified maximum)

100
VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.generate(obj) ⇒ String

Generate TJSON from a Ruby Hash (TJSON only allows objects as toplevel values)

Raises:



81
82
83
84
# File 'lib/tjson.rb', line 81

def self.generate(obj)
  raise TypeError, "toplevel type must be a Hash" unless obj.is_a?(Hash)
  JSON.generate(TJSON::DataType.generate(obj))
end

.load_file(filename) ⇒ Object

Load data from a file containing TJSON

Raises:



73
74
75
# File 'lib/tjson.rb', line 73

def self.load_file(filename)
  load(File.read(filename))
end

.parse(string) ⇒ Object Also known as: load

Parse the given UTF-8 string as TJSON

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tjson.rb', line 40

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

.pretty_generate(obj) ⇒ String

Generate TJSON from a Ruby Hash (TJSON only allows objects as toplevel values)

Raises:



90
91
92
93
# File 'lib/tjson.rb', line 90

def self.pretty_generate(obj)
  raise TypeError, "toplevel type must be a Hash" unless obj.is_a?(Hash)
  JSON.pretty_generate(TJSON::DataType.generate(obj))
end