json_object_iterator.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. *******************************************************************************
  3. * @file json_object_iterator.c
  4. *
  5. * Copyright (c) 2009-2012 Hewlett-Packard Development Company, L.P.
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. *******************************************************************************
  11. */
  12. #include <stddef.h>
  13. #include "json.h"
  14. #include "json_object_private.h"
  15. #include "json_object_iterator.h"
  16. /**
  17. * How It Works
  18. *
  19. * For each JSON Object, json-c maintains a linked list of zero
  20. * or more lh_entry (link-hash entry) structures inside the
  21. * Object's link-hash table (lh_table).
  22. *
  23. * Each lh_entry structure on the JSON Object's linked list
  24. * represents a single name/value pair. The "next" field of the
  25. * last lh_entry in the list is set to NULL, which terminates
  26. * the list.
  27. *
  28. * We represent a valid iterator that refers to an actual
  29. * name/value pair via a pointer to the pair's lh_entry
  30. * structure set as the iterator's opaque_ field.
  31. *
  32. * We follow json-c's current pair list representation by
  33. * representing a valid "end" iterator (one that refers past the
  34. * last pair) with a NULL value in the iterator's opaque_ field.
  35. *
  36. * A JSON Object without any pairs in it will have the "head"
  37. * field of its lh_table structure set to NULL. For such an
  38. * object, json_object_iter_begin will return an iterator with
  39. * the opaque_ field set to NULL, which is equivalent to the
  40. * "end" iterator.
  41. *
  42. * When iterating, we simply update the iterator's opaque_ field
  43. * to point to the next lh_entry structure in the linked list.
  44. * opaque_ will become NULL once we iterate past the last pair
  45. * in the list, which makes the iterator equivalent to the "end"
  46. * iterator.
  47. */
  48. /// Our current representation of the "end" iterator;
  49. ///
  50. /// @note May not always be NULL
  51. static const void* kObjectEndIterValue = NULL;
  52. /**
  53. * ****************************************************************************
  54. */
  55. struct json_object_iterator
  56. json_object_iter_begin(struct json_object* obj)
  57. {
  58. struct json_object_iterator iter;
  59. struct lh_table* pTable;
  60. /// @note json_object_get_object will return NULL if passed NULL
  61. /// or a non-json_type_object instance
  62. pTable = json_object_get_object(obj);
  63. JASSERT(NULL != pTable);
  64. /// @note For a pair-less Object, head is NULL, which matches our
  65. /// definition of the "end" iterator
  66. iter.opaque_ = pTable->head;
  67. return iter;
  68. }
  69. /**
  70. * ****************************************************************************
  71. */
  72. struct json_object_iterator
  73. json_object_iter_end(const struct json_object* obj)
  74. {
  75. struct json_object_iterator iter;
  76. JASSERT(NULL != obj);
  77. JASSERT(json_object_is_type(obj, json_type_object));
  78. iter.opaque_ = kObjectEndIterValue;
  79. return iter;
  80. }
  81. /**
  82. * ****************************************************************************
  83. */
  84. void
  85. json_object_iter_next(struct json_object_iterator* iter)
  86. {
  87. JASSERT(NULL != iter);
  88. JASSERT(kObjectEndIterValue != iter->opaque_);
  89. iter->opaque_ = ((const struct lh_entry *)iter->opaque_)->next;
  90. }
  91. /**
  92. * ****************************************************************************
  93. */
  94. const char*
  95. json_object_iter_peek_name(const struct json_object_iterator* iter)
  96. {
  97. JASSERT(NULL != iter);
  98. JASSERT(kObjectEndIterValue != iter->opaque_);
  99. return (const char*)(((const struct lh_entry *)iter->opaque_)->k);
  100. }
  101. /**
  102. * ****************************************************************************
  103. */
  104. struct json_object*
  105. json_object_iter_peek_value(const struct json_object_iterator* iter)
  106. {
  107. JASSERT(NULL != iter);
  108. JASSERT(kObjectEndIterValue != iter->opaque_);
  109. return (struct json_object*)lh_entry_v((const struct lh_entry *)iter->opaque_);
  110. }
  111. /**
  112. * ****************************************************************************
  113. */
  114. json_bool
  115. json_object_iter_equal(const struct json_object_iterator* iter1,
  116. const struct json_object_iterator* iter2)
  117. {
  118. JASSERT(NULL != iter1);
  119. JASSERT(NULL != iter2);
  120. return (iter1->opaque_ == iter2->opaque_);
  121. }
  122. /**
  123. * ****************************************************************************
  124. */
  125. struct json_object_iterator
  126. json_object_iter_init_default(void)
  127. {
  128. struct json_object_iterator iter;
  129. /**
  130. * @note Make this a negative, invalid value, such that
  131. * accidental access to it would likely be trapped by the
  132. * hardware as an invalid address.
  133. */
  134. iter.opaque_ = NULL;
  135. return iter;
  136. }