static AZ_NODISCARD bool _az_is_appending_value_valid()

in sdk/src/azure/core/az_json_writer.c [96:136]


static AZ_NODISCARD bool _az_is_appending_value_valid(az_json_writer const* json_writer)
{
  _az_PRECONDITION_NOT_NULL(json_writer);

  az_json_token_kind kind = json_writer->_internal.token_kind;

  if (_az_json_stack_peek(&json_writer->_internal.bit_stack))
  {
    // Cannot write a JSON value within an object without a property name first.
    // That includes writing the start of an object or array without a property name.
    if (kind != AZ_JSON_TOKEN_PROPERTY_NAME)
    {
      // Given we are within a JSON object, kind cannot be start of an array or none.
      _az_PRECONDITION(kind != AZ_JSON_TOKEN_NONE && kind != AZ_JSON_TOKEN_BEGIN_ARRAY);

      return false;
    }
  }
  else
  {
    // Adding a JSON value within a JSON array is allowed and it is also allowed to add a standalone
    // single JSON value. However, it is invalid to add multiple JSON values that aren't within a
    // container, or outside an existing closed object/array.

    // That includes writing the start of an object or array after a single JSON value or outside of
    // an existing closed object/array.

    // Given we are not within a JSON object, kind cannot be property name.
    _az_PRECONDITION(kind != AZ_JSON_TOKEN_PROPERTY_NAME && kind != AZ_JSON_TOKEN_BEGIN_OBJECT);

    // It is more likely for current_depth to not equal 0 when writing valid JSON, so check that
    // first to rely on short-circuiting and return quickly.
    if (json_writer->_internal.bit_stack._internal.current_depth == 0 && kind != AZ_JSON_TOKEN_NONE)
    {
      return false;
    }
  }

  // JSON writer state is valid and a primitive value or start of a container can be appended.
  return true;
}