You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_object_iterator.c 4.8 kB

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