Class: OrientdbBinary::Parser::Serializer
- Inherits:
-
Object
- Object
- OrientdbBinary::Parser::Serializer
- Defined in:
- lib/orientdb_binary/parser/serializer.rb
Instance Method Summary collapse
-
#initialize ⇒ Serializer
constructor
A new instance of Serializer.
- #serialize_document(document, is_map = false) ⇒ Object
- #serialize_field_value(value) ⇒ Object
Constructor Details
#initialize ⇒ Serializer
Returns a new instance of Serializer.
4 5 |
# File 'lib/orientdb_binary/parser/serializer.rb', line 4 def initialize() end |
Instance Method Details
#serialize_document(document, is_map = false) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/orientdb_binary/parser/serializer.rb', line 65 def serialize_document(document, is_map=false) klass = "" result = [] document.each do |key, value| unless [:@version, :@rid, :@type, :@cluster].include? key if key == :@class klass = value else field_wrap = is_map ? "\"" : "" result << "#{key.to_s}:#{serialize_field_value(value)}" end end end return klass.empty? ? result.join(',') : klass + "@" + result.join(',') end |
#serialize_field_value(value) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/orientdb_binary/parser/serializer.rb', line 7 def serialize_field_value(value) if value.is_a? String val = /^#\-{0,1}[\d]+:\-{0,1}[\d]+$/.match(value) return value if val value = value.sub(/\\/, "\\\\") value = value.gsub(/"/, "\\\"") return "\"#{value}\"" end if value.is_a? Integer return value.to_s end if value.is_a? Float return value.to_s + "f" end if value.is_a? Bignum return value.to_s + "l" end if value.is_a? BigDecimal return value.to_s + "d" end if value.is_a? Array or value.is_a? Set type = value.class.to_s.downcase == "set" ? "<$>" : "[$]" result = [] value.each do |el| result << serialize_field_value(el) end return type.gsub("$", result.join(',')) end if value.is_a?(TrueClass) or value.is_a?(FalseClass) return value.to_s end if value.is_a? Hash type = value[:@type] == "d" ? "($)" : "{$}" return type.gsub("$", serialize_document(value, value[:@type] != "d")) end if value.is_a? Date value.to_datetime.to_time.to_i + "a" end if value.is_a? DateTime value.to_time.to_i + "t" end if value.is_a? Time value.to_i + "t" end end |