class OpenToken::KeyValueSerializer

Constants

EMPTY_SPACE
IN_KEY
IN_QUOTED_VALUE
IN_VALUE
LINE_END
LINE_START
VALUE_START

Public Class Methods

deserialize(string) click to toggle source
# File lib/opentoken/key_value_serializer.rb, line 25
def deserialize(string)
  result = OpenToken::Token.new
  state = LINE_START
  open_quote_char = 0.chr
  currkey = ""
  token = ""
  nextval = ""

  string.split(//).each do |c|
    nextval = c

    case c
    when "\t"
      if state == IN_KEY
        # key ends
        currkey = token
        token = ""
        state = EMPTY_SPACE
      elsif state == IN_VALUE
        # non-quoted value ends
        result[currkey] = self.deserialize(token)
        token = ""
        state = LINE_END
      elsif state == IN_QUOTED_VALUE
        token += c
      end
    when " "
      if state == IN_KEY
        # key ends
        currkey = token
        token = ""
        state = EMPTY_SPACE
      elsif state == IN_VALUE
        # non-quoted value ends
        result[currkey] = self.deserialize(token)
        token = ""
        state = LINE_END
      elsif state == IN_QUOTED_VALUE
        token += c
      end
    when "\n"
      # newline
      if (state == IN_VALUE) || (state == VALUE_START)
        result[currkey] = unescape_value(token)
        token = ""
        state = LINE_START
      elsif state == LINE_END
        token = ""
        state = LINE_START
      elsif state == IN_QUOTED_VALUE
        token += c
      end
    when "="
      if state == IN_KEY
        currkey = token
        token = ""
        state = VALUE_START
      elsif (state == IN_QUOTED_VALUE) || (state == IN_VALUE)
        token += c
      end
    when "\""
      if state == IN_QUOTED_VALUE
        if (c == open_quote_char) && (token[token.size-1] != "\\"[0])
          result[currkey] = unescape_value(token)
          token = ""
          state = LINE_END
        else
          token += c
        end
      elsif state == VALUE_START
        state = IN_QUOTED_VALUE
        open_quote_char = c
      end
    when "'"
      if state == IN_QUOTED_VALUE
        if (c == open_quote_char) && (token[token.size-1] != "\\"[0])
          result[currkey] = unescape_value(token)
          token = ""
          state = LINE_END
        else
          token += c
        end
      else state == VALUE_START
        state = IN_QUOTED_VALUE
        open_quote_char = c
      end
    else
      if state == LINE_START
        state = IN_KEY
      elsif state == VALUE_START
        state = IN_VALUE
      end
      token += c
    end
  
    if (state == IN_QUOTED_VALUE) || (state == IN_VALUE)
      result[currkey] = unescape_value(token)
    end
  end
  result
end
serialize(hashmap) click to toggle source
# File lib/opentoken/key_value_serializer.rb, line 12
def serialize(hashmap)
  result = String.new
  count = 0;
  hashmap.each_pair do |key,value|
    if (count != 0)
      result = result + "\n"
    end
    count +=1
    result += key + "="
    result += escape_value(value)
  end
  result
end

Private Class Methods

escape_value(value) click to toggle source
# File lib/opentoken/key_value_serializer.rb, line 130
def escape_value(value)
  value.each_byte do |b|
    c = b.chr
    if c == "\n" or c == "\t" or c == " " or c == "'" or c == "\""
      value = "'" + value.gsub("'", "\'").gsub("\"", "\\\"") + "'"
      break
    end
  end
  value
end
unescape_value(value) click to toggle source
# File lib/opentoken/key_value_serializer.rb, line 127
def unescape_value(value)
  value.gsub("\\\"", "\"").gsub("\\\'", "'")
end