Class: OrientdbBinary::Parser::Deserializer
- Inherits:
-
Object
- Object
- OrientdbBinary::Parser::Deserializer
- Defined in:
- lib/orientdb_binary/parser/deserializer.rb
Instance Attribute Summary collapse
-
#record ⇒ Object
Returns the value of attribute record.
Instance Method Summary collapse
- #deserialize(document, params = {}) ⇒ Object
- #deserialize_document(serialized, document = {}, is_map = false) ⇒ Object
- #deserialize_field_value(value) ⇒ Object
-
#initialize ⇒ Deserializer
constructor
A new instance of Deserializer.
- #split(serialized, position) ⇒ Object
Constructor Details
#initialize ⇒ Deserializer
Returns a new instance of Deserializer.
6 7 8 |
# File 'lib/orientdb_binary/parser/deserializer.rb', line 6 def initialize() @record = {} end |
Instance Attribute Details
#record ⇒ Object
Returns the value of attribute record.
4 5 6 |
# File 'lib/orientdb_binary/parser/deserializer.rb', line 4 def record @record end |
Instance Method Details
#deserialize(document, params = {}) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/orientdb_binary/parser/deserializer.rb', line 16 def deserialize(document, params={}) @record = deserialize_document(document) params.each do |k,v| @record[k] = v end @record end |
#deserialize_document(serialized, document = {}, is_map = false) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/orientdb_binary/parser/deserializer.rb', line 24 def deserialize_document(serialized, document={}, is_map=false) serialized = serialized.strip class_index = serialized.index('@') colon_index = serialized.index(':') if class_index && (!colon_index || colon_index > class_index) document[:@class], serialized = split(serialized, class_index) end document[:@type] = "d" unless is_map while (serialized and field_index = serialized.index(':')) do field, serialized = split(serialized, field_index) if field[0] == "\"" and field[-1] == "\"" field = field[1..-2] end comma_index = look_for_comma_index(serialized) value, serialized = split(serialized, comma_index) value = deserialize_field_value(value) document[field.to_sym] = value end document end |
#deserialize_field_value(value) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/orientdb_binary/parser/deserializer.rb', line 48 def deserialize_field_value(value) return Orientdb::RecordId.new value if /^[#]?(?<cluster>-?\d+):(?<position>\d+)$/.match(value) return nil if value.empty? if ["true", "false"].include? value return value == "true" end first_char = value[0] last_char = value[-1] if "\"" == first_char val = value[1..-2] val = val.gsub(/\\"/, "\"") val = val.sub(/\\\\/, "\\") return val end # split for date and datetime if ["t", "a"].include? last_char date = DateTime.strptime(value[0..-1],'%s') date = date.to_date if last_char == "a" return date end if "(" == first_char return deserialize_document(value[1..-2]) end if "{" == first_char return deserialize_document(value[1..-2], {}, true) end if ["[", "<"].include? first_char ret = [] if first_char == "[" ret = Set.new if first_char == "<" values = split_values_from(value[1..-2]) values.each { |val| ret << deserialize_field_value(val) } return ret end if "b" == last_char return value[0..-2].to_i end # split for long/short? if ["l", "s"].include? last_char return value[0..-2].to_i end if "c" == last_char return BigDecimal.new(value[0..-2].to_i) end if ["f", "d"].include? last_char return value[0..-2].to_f end return value.to_i if value.to_i.to_s == value return value end |
#split(serialized, position) ⇒ Object
10 11 12 13 14 |
# File 'lib/orientdb_binary/parser/deserializer.rb', line 10 def split(serialized, position) first = serialized[0...position] second = serialized[position+1..-1] return first, second end |