lib/tjson.rb in tjson-0.4.0 vs lib/tjson.rb in tjson-0.5.0
- old
+ new
@@ -10,10 +10,20 @@
require "base64"
require "tjson/datatype"
require "tjson/object"
+require "tjson/datatype/array"
+require "tjson/datatype/boolean"
+require "tjson/datatype/binary"
+require "tjson/datatype/float"
+require "tjson/datatype/integer"
+require "tjson/datatype/object"
+require "tjson/datatype/set"
+require "tjson/datatype/string"
+require "tjson/datatype/timestamp"
+
# Tagged JSON with Rich Types
module TJSON
# Base class of all TJSON errors
Error = Class.new(StandardError)
@@ -30,10 +40,35 @@
DuplicateNameError = Class.new(ParseError)
# Maximum allowed nesting (TODO: use TJSON-specified maximum)
MAX_NESTING = 100
+ # Reopen the DataType class (defined in tjson/datatype.rb)
+ class DataType
+ # Table of tag identifiers supported by TJSON
+ TAGS = {
+ # Object (non-scalar with self-describing types)
+ "O" => TJSON::DataType::Object.new(nil).freeze,
+
+ # Non-scalars
+ "A" => TJSON::DataType::Array,
+ "S" => TJSON::DataType::Set,
+
+ # Scalars
+ "b" => TJSON::DataType::Boolean.new.freeze,
+ "d" => TJSON::DataType::Binary64.new.freeze,
+ "d16" => TJSON::DataType::Binary16.new.freeze,
+ "d32" => TJSON::DataType::Binary32.new.freeze,
+ "d64" => TJSON::DataType::Binary64.new.freeze,
+ "f" => TJSON::DataType::Float.new.freeze,
+ "i" => TJSON::DataType::SignedInt.new.freeze,
+ "s" => TJSON::DataType::String.new.freeze,
+ "t" => TJSON::DataType::Timestamp.new.freeze,
+ "u" => TJSON::DataType::UnsignedInt.new.freeze
+ }.freeze
+ end
+
# 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
@@ -78,17 +113,17 @@
#
# @param obj [Hash] Ruby Hash 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))
+ JSON.generate(TJSON::DataType.encode(obj))
end
# Generate TJSON from a Ruby Hash (TJSON only allows objects as toplevel values)
#
# @param obj [Hash] Ruby Hash to serialize as TJSON
# @return [String] serialized TJSON
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))
+ JSON.pretty_generate(TJSON::DataType.encode(obj))
end
end