Class: OpenToken::KeyValueSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/opentoken-newrelic-rails23/key_value_serializer.rb

Constant Summary collapse

LINE_START =
0
EMPTY_SPACE =
1
VALUE_START =
2
LINE_END =
3
IN_KEY =
4
IN_VALUE =
5
IN_QUOTED_VALUE =
6

Class Method Summary collapse

Class Method Details

.deserialize(string) ⇒ Object



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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/opentoken-newrelic-rails23/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) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/opentoken-newrelic-rails23/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