/library/kernel/json_array.e

http://github.com/Eiffel-World/ejson-ise-svn · Specman e · 141 lines · 103 code · 28 blank · 10 comment · 1 complexity · 5854eeed95af408a6fc7f7c2aa873b92 MD5 · raw file

  1. note
  2. description: "[
  3. JSON_ARRAY represent an array in JSON.
  4. An array in JSON is an ordered set of names.
  5. Examples
  6. array
  7. []
  8. [elements]
  9. ]"
  10. author: "Javier Velilla"
  11. date: "2008/08/24"
  12. revision: "Revision 0.1"
  13. class
  14. JSON_ARRAY
  15. inherit
  16. JSON_VALUE
  17. DEBUG_OUTPUT
  18. create
  19. make_array
  20. feature {NONE} -- Initialization
  21. make_array
  22. -- Initialize JSON Array
  23. do
  24. create values.make (10)
  25. end
  26. feature -- Access
  27. i_th alias "[]" (i: INTEGER): JSON_VALUE
  28. -- Item at `i'-th position
  29. require
  30. is_valid_index: valid_index (i)
  31. do
  32. Result := values.i_th (i)
  33. end
  34. representation: STRING
  35. local
  36. i: INTEGER
  37. do
  38. Result := "["
  39. from
  40. i := 1
  41. until
  42. i > count
  43. loop
  44. Result.append (i_th (i).representation)
  45. i := i + 1
  46. if i <= count then
  47. Result.append_character (',')
  48. end
  49. end
  50. Result.append_character (']')
  51. end
  52. feature -- Visitor pattern
  53. accept (a_visitor: JSON_VISITOR)
  54. -- Accept `a_visitor'.
  55. -- (Call `visit_json_array' procedure on `a_visitor'.)
  56. do
  57. a_visitor.visit_json_array (Current)
  58. end
  59. feature -- Mesurement
  60. count: INTEGER
  61. -- Number of items.
  62. do
  63. Result := values.count
  64. end
  65. feature -- Status report
  66. valid_index (i: INTEGER): BOOLEAN
  67. -- Is `i' a valid index?
  68. do
  69. Result := (1 <= i) and (i <= count)
  70. end
  71. feature -- Change Element
  72. add (value: JSON_VALUE)
  73. require
  74. value_not_null: value /= void
  75. do
  76. values.extend (value)
  77. ensure
  78. has_new_value: old values.count + 1 = values.count and
  79. values.has (value)
  80. end
  81. feature -- Report
  82. hash_code: INTEGER
  83. -- Hash code value
  84. do
  85. from
  86. values.start
  87. Result := values.item.hash_code
  88. until
  89. values.off
  90. loop
  91. Result:= ((Result \\ 8388593) |<< 8) + values.item.hash_code
  92. values.forth
  93. end
  94. Result := Result \\ values.count
  95. end
  96. feature -- Conversion
  97. array_representation: ARRAYED_LIST [JSON_VALUE]
  98. -- Representation as a sequences of values
  99. do
  100. Result := values
  101. end
  102. feature -- Status report
  103. debug_output: STRING
  104. -- String that should be displayed in debugger to represent `Current'.
  105. do
  106. Result := count.out
  107. end
  108. feature {NONE} -- Implementation
  109. values: ARRAYED_LIST [JSON_VALUE]
  110. -- Value container
  111. invariant
  112. value_not_void: values /= Void
  113. end